从UIActionSheet按下按钮后需要帮助调用方法

时间:2011-04-07 05:47:16

标签: iphone objective-c uiactionsheet

我有一个UIActionSheet,我可以从我的应用程序中的UIBarButtonItem成功调用。但是,当我单击UIActionSheet上的一个按钮时,我试图调用它的方法似乎没有被调用,并且UIActionSheet消失了。我的相关代码如下所示:

在我的MapViewController.h文件中:

@interface MapViewController : UIViewController<MKMapViewDelegate, UIActionSheetDelegate>{

在我的MapViewController.m文件中:

-(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)actionSheet:(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!");
}

谁能看到我哪里出错了?我个人的预感是它可能与我如何实现UIActionSheetDelegate接口有关。

3 个答案:

答案 0 :(得分:0)

你传递nil作为委托,但委托是你的视图控制器,所以传递自己:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share your favourite restaurant with your friends" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"EMail",@"Facebook",@"Twitter",nil];

答案 1 :(得分:0)

您可能忘记设置代理,在代码中添加以下内容

actionSheet.delegate = self ;

来自文档...... 接收方的代表,如果没有代表,则为零。

@property(nonatomic, assign) id<UIActionSheetDelegate> delegate

答案 2 :(得分:0)

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share your favourite restaurant with your friends" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"EMail",@"Facebook",@"Twitter",nil];

应该是

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share your favourite restaurant with your friends" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"EMail",@"Facebook",@"Twitter",nil];

并确保您的.h文件实现UIActionSheetDelegate协议