OGeek|极客世界-中国程序员成长平台

标题: ios - 从 Objective-C 中的另一个类呈现 "Controller" [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:10
标题: ios - 从 Objective-C 中的另一个类呈现 "Controller"

如何呈现另一个类的 UIAlertController?

我想知道如何在 UIAlertController 中捕获“确定”按钮的操作,该 Controller 是在 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?假设一个委托(delegate)方法并导航回 A 类中的代码?



Best Answer-推荐答案


2 个选择:

最佳选择:

将 Controller 传递给方法,如下所示:- (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];

编辑——捕捉 Action :

- (void) presentAlertWithInternetErrorCodeInPresenterUIViewController<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):

@protocol CustomAlertViewProtocol
- (void) cancelPressed;
@end

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

@interface MyViewController : UIViewController <CustomAlertViewProtocol>

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

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

现在实际显示警报:

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

关于ios - 从 Objective-C 中的另一个类呈现 "Controller",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38771374/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4