2011-04-05 68 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)。