我创建了一个NSObject
类,在其中我创建了一些常用函数。我创建了显示UIAlertView
的函数。它工作正常。但是当我点击警告按钮时,didDismissWithButtonIndex
没有调用委托方法。
+ (void)showMessage:(NSString *)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:JMESSAGETITLE message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert postHideAlertNotification:0];
[alert show];
}
#pragma mark - UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"Hello");
}
我不知道问题所在。
答案 0 :(得分:9)
此行自我
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:JMESSAGETITLE message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
表示 NSObject 类不是对象。您必须将类方法更改为实例方法或创建 NSObject 类的对象,并将此对象设置为委托。
对于对象创建,您可以使用单例模式。
+ (YourClass *)sharedInstance {
static dispatch_once_t once;
static YourClass *sharedMyClass;
dispatch_once(&once, ^ {
sharedMyClass = [[self alloc] init];
});
return sharedMyClass;
}
+ (void)showMessage:(NSString *)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: JMESSAGETITLE message:message delegate:[YourClass sharedInstance] cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
答案 1 :(得分:0)
试试这个:
确认NSObject类中的UIAlertViewDelegate
协议如下:
@interface yourClass : NSObject <UIAlertViewDelegate>
将UIAlertView *alert
对象作为类的实例变量或
属性。像这样:
@interface yourClass : NSObject <UIAlertViewDelegate>
@property (nonatomic, strong) UIAlertView *alert;
@end
在使用alert
对象的方法中,设置委托self.alert.delegate = self