My UIActionSheet acting like non-modal?
NSLog(@"Display Action Sheet");
UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle:@"Warning" delegate:self cancelButtonTitle:@"Proceed" destructiveButtonTitle:@"Cancel (current data will be discard)" otherButtonTitles:nil];
[alert showInView:[self view]];
[alert release];
NSLog(@"Action Sheet Released");
This is my code that creates an action sheet. Before I see the action sheet, both "Display Action Sheet" and "Action Sheet Released" get output to the debugger console. Actually other codes that I want to execute AFTER I receive input from user are all executed before I am presented the action sheet.
This is rather weird. I thought I could use action sheet to execute codes b开发者_StackOverflow中文版ased on user's input.
Action sheets aren't modal. Pretty much nothing in iOS is. You need to handle whatever the user chooses in the sheet in one of the UIActionSheetDelegate
methods, like -actionSheet:clickedButtonAtIndex:
.
Make sure you have the UIActionSheetDelegate
in your header.
If I want the answer to a question without the user allowing the option of cancel, I do something like this (key is : "if (buttonIndex == -1) performSelector")... Basically just loop till happy...
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (debug==1) {
NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));
}
if (buttonIndex == -1) {
[self performSelector:@selector(selectDb) withObject:nil afterDelay:0.1];
} else {
[DataLayer selectDatabase: buttonIndex];
}
}
- (void) selectDb {
if (![DataLayer hasSelectedDatabase]) {
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Filter" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Main", @"Training", @"Test 1", @"Test 2", @"Test 3", @"Test 4", nil];
[actionSheet showInView:[self view]];
}
}
精彩评论