Как работать с другим обработчиком UIAlertAction в одном и том же UIAlertController в iOS?

Я использую Objective-C для написания кода UIAlertController.

У меня больше кнопок, но кнопки будут отображать разные UIAlertController и работать с другим обработчиком UIAlertAction.

Итак, я хочу создать один UIAlertController и UIAlertAction.

Как показано ниже:

-(void) initAlert{
    alertController = [UIAlertController alertControllerWithTitle:@"hint" message:@"count down alert" preferredStyle:UIAlertControllerStyleAlert];

    doneAction = [UIAlertAction actionWithTitle:@"okey" style:UIAlertActionStyleDefault handler:
              ^(UIAlertAction *action) {
    NSLog(@"show log");
    }];
    [alertController addAction:doneAction];
}

-(void) showAlert{
    [self presentViewController:alertController animated:YES completion:nil];
}

Затем я хочу использовать другую кнопку IBAction для вызова метода showAlert и установить другой заголовок UIAlertController, заголовок UIAlertAction и использовать другой обработчик alertAction.

Но я сталкиваюсь с некоторыми проблемами.

Я вызываю метод в другой кнопке, как показано ниже:

- (IBAction)btn1Action:(UIButton *)sender {

    alertController.title = @"controller 1";
    alertController.message = @"message1";

    [self showAlert];
}

- (IBAction)btn2Action:(UIButton *)sender {

    alertController.title = @"controller 2";
    alertController.message = @"message2";

    [self showAlert];
}

Я не знаю, как изменить заголовок UIAlertAction с тем же самым действием doneAction, я ищу некоторые данные, показывающие, что свойство UIAlertAction is readyonly.

Итак, есть ли другие способы изменить заголовок UIAlertAction? или мы можем удалить метод UIAlertController addAction:, чтобы добавить другие UIAlertAction?

И как я могу передать другой обработчик UIAlertAction в AlertAction для использования одного и того же UIAlertController?

Спасибо большое.


person dickfala    schedule 05.01.2016    source источник
comment
Я не думаю, что UIAlertController предназначен для повторного использования. Вы должны создать разные UIAlertController для разных предупреждений.   -  person cekisakurek    schedule 05.01.2016


Ответы (1)


UIAlertController не следует использовать несколько раз. Просто используйте новый экземпляр UIAlertController каждый раз, когда вы хотите вывести предупреждение.

- (IBAction)btn1Action:(UIButton *)sender {

    [self showAlert:@"Controller 1" message:@"Message 1" handler:^(UIAlertAction *action) {
        NSLog(@"btn1Action");
    }];
}

- (IBAction)btn2Action:(UIButton *)sender {

    [self showAlert:@"Controller 2" message:@"Message 2" handler:^(UIAlertAction *action) {
        NSLog(@"btn2Action");
    }];
}

-(void)showAlert:(NSString*)alertTitle message:(NSString*)message handler:(void (^ __nullable)(UIAlertAction *action))handler {
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:alertTitle message:message preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction * doneAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:handler];
    [alertController addAction:doneAction];

    [self presentViewController:alertController animated:YES completion:nil];
}
person myuiviews    schedule 05.01.2016