【问题标题】:Present "Controller" from another Class in Objective-C在 Objective-C 中从另一个类中呈现“控制器”
【发布时间】:2016-12-10 19:33:35
【问题描述】:

如何呈现另一个类的 UIAlertController?

我想知道如何在 UIAlertController 中捕获“确定”按钮的操作,该控制器是在 B 类中创建但在 A 类中呈现的。

这就是我如何从 ClassA 调用在类“ErrorHandler”上创建警报的方法:

ErrorHandler *handler = [[ErrorHandler alloc] init];
[self presentViewController:[handler alertWithInternetErrorCode] animated:YES completion:nil];

这是ErrorHandler.m中alertWithInternetErrorCode的实现:

- (UIAlertController *)alertWithInternetErrorCode{

    UIAlertController * alert = [UIAlertController
                                 alertControllerWithTitle:@"Error"
                                 message:@"No internet conneciton"
                                 preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancel = [UIAlertAction
                              actionWithTitle:@"Cancel"
                              style:UIAlertActionStyleCancel
                              handler:^(UIAlertAction * action) {
                                  NSLog(@"cancelled");
                              }];

    [alert addAction:cancel];
    return alert;
}

再次,我想知道如何能够在其他类中创建这类对象,并且仍然能够在您调用它们的类中呈现它们。这包括捕捉他们的行为。在这种情况下,它将是“取消按钮”内的 NSLog 操作。 是否可以调用方法而不是 NSLog?假设一个委托方法并导航回 A 类中的代码?

【问题讨论】:

  • 传递一个视图控制器,例如:- (void)presentAlertWithInternetErrorCodeOnController:(UIViewController *)controller

标签: ios objective-c methods delegates uialertcontroller


【解决方案1】:

2 个选择:

最佳选择:

将控制器传递给方法,如下所示:- (UIAlertController *)alertWithInternetErrorCodeInPresenter: (UIViewController *) presenter

致电[presenter presentViewController: alert animated:YES completion:nil];

如果这不可能:

UIViewController *rootVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[rootVC presentViewController:alert animated:YES completion:nil];

编辑——捕捉动作:

- (void) presentAlertWithInternetErrorCodeInPresenter:(UIViewController<CustomAlertViewProtocol> *) presenter{

    UIAlertController * alert = [UIAlertController
                             alertControllerWithTitle:@"Error"
                             message:@"No internet connection"
                             preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancel = [UIAlertAction
                          actionWithTitle:@"Cancel"
                          style:UIAlertActionStyleCancel
                          handler:^(UIAlertAction * action) {
                              [presenter cancelPressed];//Here's the key
                          }];

    [alert addAction:cancel];
    [presenter presentViewController: alert animated:YES completion:nil];
}

ErrorHandler.h 文件中,您必须声明此协议:

@protocol CustomAlertViewProtocol
- (void) cancelPressed;
@end

现在,在您想要使用此方法的任何视图控制器 .h 文件中,您必须告诉编译器您正在遵循 CustomAlertViewProtocol:

@interface MyViewController : UIViewController <CustomAlertViewProtocol>

并且在.m中你必须实现协议方法:

- (void) cancelPressed {
    //Do whatever you want
}

现在实际显示警报:

ErrorHandler *handler = [[ErrorHandler alloc] init];//Or whatever initializer you use
[handler presentAlertWithInternetErrorCodeInPresenter: self];

【讨论】:

  • 我已经从 A 类调用了 B 类中的 AlertView。并且我已经在 A 类中呈现了警报(B 类返回了 AlertViewController)。现在,是否可以从 A 类中捕获“ok AlertButton”操作?我应该在 B 中声明一个代表吗?还是A?这个我想弄清楚。
猜你喜欢
  • 2015-06-17
  • 1970-01-01
  • 2018-05-19
  • 2023-03-17
  • 1970-01-01
  • 2011-06-15
  • 2011-12-06
  • 1970-01-01
  • 2018-12-12
相关资源
最近更新 更多