Modal Sheet without Spaghetti?
Modal Sheet without Spaghetti?
- Subject: Modal Sheet without Spaghetti?
- From: Jerry Krinock <email@hidden>
- Date: Tue, 27 Jan 2009 17:10:20 -0800
I often find myself in the quandary where the innermost of some nested
methods may run into a problem that requires a user decision before
processing can continue. So I use a modal dialog in there, blocking
the main thread, and everything works fine.
Butt if the possible problem involves a document, I believe it would
be a better user experience to use a sheet instead of a dialog. But,
arghhh, the method
-[NSApp
beginSheet:modalForWindow:modalDelegate:didEndSelector:contextInfo:]
returns immediately, sending its return message to a modal delegate
selector. So I must go back up the nest, all the way up to the event
which drove the process, and split all these methods into two methods,
the code "before" and "after" the sheet runs. Repeat this for all
other possible entry points into this innermost method. And there
goes another afternoon.
I believe the reason the reason why -[NSApp beginSheet:::::] returns
immediately is that one would not want a document-modal dialog to
block the main thread, preventing the app from doing other stuff.
Does anyone have an idiom or way of appreciating this problem which
does not produce such spaghetti and headaches?
Jerry Krinock
/********************************************/
/*** Using a modal dialog, nice and clean ***/
/********************************************/
-(IBAction)methodA:(id)sender {
// Some code here (A1)
[self methodB] ;
// Some code here (A2)
}
-(void)methodB {
// Some code here (B1)
[self methodC] ;
// Some code here (B2)
}
-(void)methodC {
// Some code here (C1)
NSRunAlertPanel(..., ..., ...);
// Some code here (C2)
}
/***************************************/
/*** Using a modal sheet, spaghetti! ***/
/***************************************/
-(IBAction)methodA1:(id)sender {
// Some code here (A1)
[self methodB] ;
}
-(void)methodB1 {
// Some code here (B1)
[self methodC] ;
}
-(void)methodC1 {
// Some code here (C1)
[NSApp beginSheet:...
modalForWindow:...
modalDelegate:self
didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
}
-(void)didEndSheet:(NSWindow*)sheet
returnCode:(NSInteger)returnCode
contextInfo:(void*)contextInfo {
[sheet orderOut:self] ;
// Some code here (C2)
[self methodB2];
}
-(void)methodB2 {
// Some code here (B2)
[self methodA2] ;
}
-(void)methodA2 {
// Some code here (A2)
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden