【发布时间】:2015-11-20 17:14:05
【问题描述】:
简单地说,我想在 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 *' is a pointer Error
[self someMethod]; // No known class method Error
}];
[alert addAction:defaultAction];
[view presentViewController:alert animated:YES completion:nil];
}
- (void)someMethod {
}
【问题讨论】:
-
您的
alertWithTitle...方法是类方法,而不是实例方法。让它成为一个实例方法,你就可以做你想做的事了。
标签: objective-c uialertcontroller