2011-04-05 44 views
1

我在用戶單擊操作表單中的「destructButton」後實現了一個警報視圖。代碼片段如下:目標C:AlertView不起作用

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex != actionSheet.cancelButtonIndex) 
    { 
    UIAlertView *alert = [[UIAlertView alloc] 
    initWithTitle:@"Something happened!" 
    message:nil 
    delegate:nil 
    cancelButtonTitle:@"Phew!" 
    otherButtonTitles:nil]; 
    } 

    [alert show]; 
    [alert release]; 
} 

我已經包含在我的.h文件中

@interface Control_FunViewController : UIViewController <UIActionSheetDelegate> { 
    UITextField *nameField; 
    UITextField *numberField; 
    UILabel *sliderLabel; 
    UISwitch *leftSwitch; 
    UISwitch *rightSwitch; 
    UIButton *doSomethingButton; 

} 

的UIActionSheetDelegate協議,但是當我在actionsheet點擊destructButton,沒有任何反應。我在這裏錯過了什麼嗎?

感謝

+1

我不認爲你的代碼甚至可以編譯。爲什麼'alert'顯示在定義範圍之外? – kennytm 2011-04-05 05:26:09

回答

2

你的委託需要實現actionSheet:clickedButtonAtIndex:

行動表將在actionSheet:clickedButtonAtIndex:後被解僱,此後代表將收到actionSheet:didDismissWithButtonIndex:

Apple Action Sheet Delegate Protocol reference

...委託必須實現 actionSheet:clickedButtonAtIndex: 消息,這些按鈕 被點擊的時候做出反應;否則,你的自定義 按鈕什麼也不做。在 actionSheet:clickedButtonAtIndex: 調用了委託方法後,操作圖表 被自動解僱。

你可以看看使用UIActionSheet像GenericKeychain示例項目:

// Action sheet delegate method. 
    -  (void)actionSheet:(UIActionSheet *)actionSheet 
     clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
     // the user clicked one of the OK/Cancel buttons 
     if (buttonIndex == 0) 
     { 
      [passwordItem resetKeychainItem]; 
      [accountNumberItem resetKeychainItem]; 
      [self.tableView reloadData]; 
     } 
    } 
1

您已經添加UIActionSheetDelegate上的.h文件。不僅僅這樣。您還必須爲UIActionSheet對象設置委託。參閱此以下編碼:

- (無效)showActionSheet { UIActionSheet *行動= [[UIActionSheet的alloc] initWithTitle:@ 「的DSF」 代表:自cancelButtonTitle:@ 「第一BTN」 destructiveButtonTitle:@ 「目標BTN」 otherButtonTitles :零]; [action showInView:self.view];

}

在該代碼中,我已經UIActionSheet的委託設置爲self,這個工程如果你已經加入此方法對您的視圖控制器(Control_FunViewController)。