小编Ant*_*oni的帖子

如何在Objective-C中正确使用UIAlertAction处理程序

简单地说,我想在UIAlertAction处理程序中调用一个方法引用我的类中的变量.

我是否需要将块传递给此处理程序,还是有其他方法可以实现此目的?

@interface OSAlertAllView ()
@property (nonatomic, strong) NSString *aString;
@end

@implementation OSAlertAllView

+ (void)alertWithTitle:(NSString *)title message:(NSString *)msg cancelTitle:(NSString *)cancel inView:(UIViewController *)view
{
    __weak __typeof(self) weakSelf = self;

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:msg
                                                                preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleDefault
        handler:^(UIAlertAction * action) {
            // I'd like to reference a variable or method here
            weakSelf.aString = @"Apples"; // Not sure if this would be necessary
            self.aString = @"Apples"; // Member reference type 'struct objc_class *' …
Run Code Online (Sandbox Code Playgroud)

objective-c uialertcontroller

5
推荐指数
1
解决办法
7973
查看次数

标签 统计

objective-c ×1

uialertcontroller ×1