in my Alert View, there is two button, OK and Cancel. When the user click the OK button, the delegate method dismissWithClickedButtonIndex:animated get called, and if the index is 0, then i get called to a method to execute some code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Are you sure you want to exit"
delegate:self cancelButtonTitle: @"OK"
otherButtonTitles: @"Cancel",nil];
[alert show];
[alert release];//release the reference
Delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
[self aMethod];
}
}
-(void)aMethod{
//Some useful code
}
Now, what i want to instead of all this, is to execute the code of the aMethod method in the AlertView directly, without referring to A delegate method and a method which get called, something like that:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Are you sure you want to exit"
delegate:self cancelButtonTitle: @"OK" //Put here some useful code
otherButtonTitles: @"Cancel",nil];
Is it possible?
I made a pair of
UIAlertViewandUIActionSheetsubclasses that do exactly that. Grab them here:https://github.com/rydermackay/RMActionSheet
Use them like this:
EDIT:
From your comments it sounds like you’re not familiar with blocks. Read this now. Seriously.
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html
This is a good one too:
http://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.html