2011-04-07 107 views
0

我有一個UIActionSheet,我可以從我的應用程序中的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接口有關。

+0

非常感謝您所有的及時答覆的。你們都是對的!感謝您指出這種疏忽。保重。 – syedfa 2011-04-07 06:06:25

回答

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協議

0

你傳入零爲代表,但代表是您的視圖控制器,因此通過自我:

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

可能是你忘了設置代理,在你的代碼下面添加

actionSheet.delegate = self ; 

來自文檔... 接收者的委託或無,如果它沒有委託。

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