iphone - UIAlertView 代表

标签 iphone objective-c cocoa-touch

有人可以解释 UIAlertView 的委托(delegate)是如何工作的吗?它是自动调用还是我必须调用它?例如:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

最佳答案

假设您在代表是“自己”的地方显示了一个警报

- (void)showAlert {
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" 
                                                       message:@"Do you want to continue?"
                     delegate:self
                  cancelButtonTitle:nil
                  otherButtonTitles:@"No", @"Yes", nil];
        [myAlert show];
        [myAlert release];
}

为了使以下内容在您的 .m 文件中起作用:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

您的 .h 文件需要在实现语句中引用 UIAlertViewDelegate,如下所示:

@interface myViewController : UIViewController <UIAlertViewDelegate> {
}

这是允许您的 .m 文件响应 UIAlertViewDelegate 方法调用的原因。

关于iphone - UIAlertView 代表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3826659/

相关文章:

iphone - 如果无法访问数据,iOS 应用程序会崩溃

iphone - 单例 managedObjectContext

iphone - 应用程序未通过协同设计验证。我该怎么办?

ios - 如何通过滑动手势获取 IndexPath 和 UItableView

objective-c - 如果子对象在父对象的dealloc中被释放,为什么子对象对父对象的弱引用会为零*

iphone - 如何将 NSArray 字符串对象值分配给其他 NSString 变量

ios - 苹果手机 : How do I add a text view that the font size decreases to fit text view size when needed to

iphone - 如何确定用户是否按下 UITableViewCell 2 秒?

ios - 如何仅使用代码创建 UIImageView?

iphone - 如何在延迟后为 popViewController 设置动画?