目标C:AlertView不起作用

时间:2011-04-05 05:22:41

标签: objective-c uialertview uiactionsheet

我在用户点击操作表中的'destructButton'后实现警报视图。代码段如下:

-(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文件中包含了UIActionSheetDelegate协议

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

}

但是当我点击动作表中的destructButton时,没有任何反应。我在这里错过了什么吗?

由于

2 个答案:

答案 0 :(得分: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 :(得分:1)

您已在.h文件中添加了UIActionSheetDelegate。不仅如此。您还必须为UIActionSheet对象设置委托。请参阅以下编码:

- (无效)showActionSheet { UIActionSheet * action = [[UIActionSheet alloc] initWithTitle:@“dsfs”delegate:self cancelButtonTitle:@“First Btn”destructiveButtonTitle:@“Dest Btn”otherButtonTitles:nil];     [action showInView:self.view];

}

在该代码中,我已将UIActionSheet的委托设置为self,如果您在ViewController(Control_FunViewController)上添加了此方法,则此方法有效。