首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在从UIActionSheet中按下按钮后调用方法时需要帮助

在从UIActionSheet中按下按钮后调用方法时需要帮助
EN

Stack Overflow用户
提问于 2011-04-07 13:47:16
回答 3查看 218关注 0票数 0

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

在我的MapViewController.h文件中:

代码语言:javascript
复制
@interface MapViewController : UIViewController<MKMapViewDelegate, UIActionSheetDelegate>{

在我的MapViewController.m文件中:

代码语言:javascript
复制
-(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接口的方式有关。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-04-07 13:56:25

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

应该是

代码语言:javascript
复制
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协议

票数 0
EN

Stack Overflow用户

发布于 2011-04-07 13:51:13

您正在传递nil作为代理,但该代理是您的视图控制器,因此传递self:

代码语言:javascript
复制
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share your favourite restaurant with your friends" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"EMail",@"Facebook",@"Twitter",nil];
票数 0
EN

Stack Overflow用户

发布于 2011-04-07 13:52:03

可能是您忘记设置委托,请在代码中添加以下内容

代码语言:javascript
复制
actionSheet.delegate = self ;

从文档中... 接收器的委托,如果它没有委托,则为nil。

代码语言:javascript
复制
@property(nonatomic, assign) id<UIActionSheetDelegate> delegate
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5576575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档