ios - 为通知创建委托(delegate)

标签 ios objective-c delegates notifications

假设我有一个整数 (hi),它是 0。我想要一个在消息中说 0 的通知。我的代码是:

-(IBAction) alert3;
{
    int hi = 0;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                            message:@"%d"
                                           delegate:hi
                                  cancelButtonTitle:@"Ok"
                                  otherButtonTitles: nil];

然后我得到一个错误:

“将‘int’发送到‘id’类型的参数的指针转换不兼容的整数”

最佳答案

您的实现不完整。您需要将消息添加为字符串。

这样做:

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                            message:[NSString stringWithFormat:@"%d", hi]
                                           delegate:self
                                  cancelButtonTitle:@"Ok"
                                  otherButtonTitles: nil];

%d 是一个 int 的参数格式化器,你必须在逗号之后传递

关于委托(delegate),委托(delegate)是将响应 UIAlertViewDelegate 中定义的消息的类(例如,当用户触摸按钮时)。

如果你不想控制它,只需将它设置为空:

delegate:nil

或者 self 控制。

关于ios - 为通知创建委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17654781/

相关文章:

ios - 如何在 iOS 中管理自定义单元格标签值?

ios - 如何在 Swift 中实例化 NSViewController?

javascript - 如何在 objective-c 中实现 back4app.com 上的 javascript 代码

c# - 跨线程操作无效: Control 'textbox' accessed from a thread other than the thread it was created on

ios - iOS 中的多个委托(delegate)

c# - 在匿名方法中捕获委托(delegate)

ios - 适用于 iOS 的 Dropbox 集成

ios - swift iOS - NSURLSession 无法正确下载多个文件

iOS - 在应用程序中跟踪飞行模式状态(我想在应用商店构建中使用它)

ios - 随机数语句不适用于我的 toInt 文本字段输入