我有一个能够从我的应用程序中的UIBarButtonItem成功调用的UIActionSheet。但是,当我单击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接口的方式有关。
发布于 2011-04-07 13:56:25
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协议
发布于 2011-04-07 13:51:13
您正在传递nil作为代理,但该代理是您的视图控制器,因此传递self:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share your favourite restaurant with your friends" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"EMail",@"Facebook",@"Twitter",nil];发布于 2011-04-07 13:52:03
可能是您忘记设置委托,请在代码中添加以下内容
actionSheet.delegate = self ;从文档中... 接收器的委托,如果它没有委托,则为nil。
@property(nonatomic, assign) id<UIActionSheetDelegate> delegatehttps://stackoverflow.com/questions/5576575
复制相似问题