IOS/Objective-C:从 Storyboard 应用中子类 Class 中的代码启动 UIAlertController

IOS/Objective-C: Launch UIAlertController from code in subclassed Class in Storyboard app

我的应用程序中的两个视图控制器子class 具有通用方法的相同 class。我想用这个常见的 sub-classed 控制器的方法从代码中启动一个 alertController(发出警报)。但是下面的代码没有启动任何东西。

任何人都可以建议指向 subclassed VC 以启动 alertController 的正确方法吗?

提前感谢您的任何建议。

查看连接到情节提要子class的控制器 1 是常见的 class,如下所示:

@interface IDManageItemsVC : IDCommonVC <UIAlertViewDelegate>

通用的VC subclasses CoreVC 整个应用程序有更通用的方法:

#import "IDCoreVC.h"
@interface IDCommonVC : IDCoreVC<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

我正在尝试从通用代码VC(连接到故事板的class 的超级class)触发警报,如下所示:

-(void)fireAlert {

 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Delete?" message:nil preferredStyle:UIAlertControllerStyleAlert];

 UIAlertAction* yesButton = [UIAlertAction
 actionWithTitle:@"OK"
 style:UIAlertActionStyleDefault
 handler:^(UIAlertAction * action)
 {
 //run code
 }];

 UIAlertAction* noButton = [UIAlertAction
 actionWithTitle:@"Not Now"
 style:UIAlertActionStyleDefault
 handler:^(UIAlertAction * action)
 {
 //if chosen run code
 }];

 [alert addAction:noButton];
 [alert addAction:yesButton];
 if ([alert respondsToSelector:@selector(setPreferredAction:)]) {
 [alert setPreferredAction:yesButton];
 }

/* following points to VC not in hierarchy so commented out
    id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
    if([rootViewController isKindOfClass:[UINavigationController class]])
    {
        rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
    }
    if([rootViewController isKindOfClass:[UITabBarController class]])
    {
        rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
    }
    [rootViewController presentViewController:alertInvite animated:YES completion:nil]; */
//Following does not do anything
[self presentViewController:alert animated:YES completion:nil];
}

编辑:

使用下面的方法,打个断点,我目视验证了topViewController是正确的,然后从中呈现了alertview,它仍然没有显示。我唯一注意到的是,当我目视检查 alertview 时,它看起来是空白的,左上角只有一条轻微的白色曲线,圆角可能与白色矩形相对。所以也许我创建警报视图的方式有问题。

UIViewController *currentTopVC = [self currentTopViewController];
currentTopVC.presentViewController......... 

- (UIViewController *)currentTopViewController {
    UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}

而不是 [self presentViewController...,以下内容:

UIViewController *currentTopVC = [self currentTopViewController];
    [currentTopVC presentViewController:alertInvite animated:YES completion:nil]; 

我复制并粘贴了你的代码并替换了

[self presentViewController:alertInvite animated:YES completion:nil];

[self presentViewController:alert animated:YES completion:nil];

成功了。

在我看来这不像是继承问题。您是否有机会从 viewDidLoad: 或在实际显示视图控制器之前调用的任何方法调用此方法?如果是这样,请尝试从 viewDidAppear:

调用它