Need help calling a method after pressing a button from a UIActionSheet
I have a UIActionSheet that I am able to successfully call from a UIBarButtonItem in my app. However, when I click on a button that is on the UIActionSheet, the method that I am trying to get it to call does not appear to be called, and the UIActionSheet goes away. My relevant code looks like this:
In my MapViewController.h file:
@interface MapViewController : UIViewController<MKMapViewDelegate, UIActionSheetDelegate>{
In my MapViewController.m file:
-(IBAction)showActionSheet {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share your favourite restaurant with your friends" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"EMail",@"Facebook",@"Twitter",nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
-(void)actionS开发者_Go百科heet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0)
NSLog(@"You chose email!");
if(buttonIndex == 1)
NSLog(@"You chose facebook!");
if(buttonIndex == 2)
NSLog(@"You chose twitter!");
}
Can anyone see where I am going wrong? My personal hunch is that it may have something to do with how I am implementing the UIActionSheetDelegate interface.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share your favourite restaurant with your friends" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"EMail",@"Facebook",@"Twitter",nil];
should be
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share your favourite restaurant with your friends" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"EMail",@"Facebook",@"Twitter",nil];
And also make sure your .h file implements the UIActionSheetDelegate
protocol
You are passing nil as the delegate, but the delegate is your view controller, so pass self:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share your favourite restaurant with your friends" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"EMail",@"Facebook",@"Twitter",nil];
May be you forget to set the delegate , Add below in your code
actionSheet.delegate = self ;
From the Documentation ... The receiver’s delegate or nil if it doesn’t have a delegate.
@property(nonatomic, assign) id<UIActionSheetDelegate> delegate
精彩评论