gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:51:26 24 4
gpt4 key购买 nike

我的应用程序中的两个 View Controller 子类化具有通用方法的同一类。我想从这个公共(public)子类 Controller 的方法中的代码启动一个alertController(触发警报)。但下面的代码没有启动任何东西。

任何人都可以建议指向子类 VC 来启动 AlertController 的正确方法吗?

提前感谢您的任何建议。

View Controller 1连接到 Storyboard子类公共(public)类,如下所示:

@interface IDManageItemsVC : IDCommonVC <UIAlertViewDelegate>

常见的 VC 子类 CoreVC 为整个应用程序提供了更多通用方法:

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

我正在尝试从 commonVC(连接到 Storyboard的类的父类(super 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,但它仍然没有显示。我唯一注意到的是,当我目视检查警报 View 时,它显示为空白,左上角只有一条轻微的白色曲线,其中圆角可能与白色矩形相对。所以也许我创建警报 View 的方式有问题。

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

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

而不是[selfpresentViewController...,如下:

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: 或在实际显示 View Controller 之前调用的任何方法调用此方法?如果是这样,请尝试从 viewDidAppear 调用它:

关于IOS/objective-C : Launch UIAlertController from code in subclassed Class in Storyboard app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51974678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com