在NSObject类中没有调用UIAlertView

Kis*_*yas 0 objective-c ios

我创建了一个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");
}
Run Code Online (Sandbox Code Playgroud)

我不知道问题是什么.

Bha*_*ivi 9

这一行中的自我

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:JMESSAGETITLE message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
Run Code Online (Sandbox Code Playgroud)

表示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];
}
Run Code Online (Sandbox Code Playgroud)