Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标C:块和弧_Ios_Objective C - Fatal编程技术网

Ios 目标C:块和弧

Ios 目标C:块和弧,ios,objective-c,Ios,Objective C,我已经为UIAlerts创建了实用程序类。我在用积木 用户应该创建的我的代码如下所示: MyAlertMessage * a = [[MyAlertMessage alloc] initWithTitle:@"Hello" WithMessage:@"World"]; [a addButton:BUTTON_OK WithTitle:@"OK" WithAction:^(void *action) { NSLog(@"Button OK at index 0 click");

我已经为UIAlerts创建了实用程序类。我在用积木

用户应该创建的我的代码如下所示:

MyAlertMessage * a = [[MyAlertMessage alloc] initWithTitle:@"Hello" WithMessage:@"World"];

[a addButton:BUTTON_OK WithTitle:@"OK" WithAction:^(void *action) {
        NSLog(@"Button OK at index 0 click");
}];

[a addButton:BUTTON_CANCEL WithTitle:@"Cancel" WithAction:^(void *action) {
        NSLog(@"Button Cancel at index 1 click");
}];

[a show]
在这里可以看到完整的类:

现在,如果我这样做,在[a show]ARC破坏我的类之后,所以块不再工作,并给我错误。我通过创建一个singleton类来解决这个问题,该类包含对已创建MyAlertMessage的引用(消息广告并从此管理器中销毁自身)。这是正确的解决方案还是应该在没有单例管理器的情况下做得更好


可以在此处找到Manager和相应的类:

您发布的示例代码使用局部变量创建MyAlertMessage对象。因为它是一个局部变量,所以一旦执行离开当前的大括号集(方法、if语句等等),它就会超出范围。一旦发生这种情况,没有人对对象有强引用,因此它就会被释放


将MyAlertMessage变量设置为实例变量,并在处理完后将其设置为nil。

是否将UIAlertView或UIAlertController子类化?还是试图两者兼而有之?UIAlertView有一个可用于执行此操作的委托,UIAlertController仍支持块。您的消息命名不标准。请看@keithbhunter,他的代码(在GitHub上)引用了iOS 7,我不相信iOS 8.0以下的任何版本都支持UIAlertController。他根本不使用UIAlerts——而且经理看起来很不错,他不希望你的用户从git下载你的项目。在你的问题中张贴适当的代码。MyAlertMessage的标题看起来像什么?您发布的代码的范围是什么?