[英]Using UIAlertController in Xcode 8 with Objective-C
每次用户启动应用程序时,我都试图显示一个UIAlertController
。
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My alert" message:@"This should be come when the app start" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *yesButton = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:yesButton];
[self presentViewController:alert animated:YES completion:nil];
}
但这是行不通的。 我在 Xcode 模拟器中运行该应用程序。 该应用程序运行但没有显示警报视图。 我究竟做错了什么?
尝试在viewDidAppear:
委托中打开UIAlertController
。 或者,您可以将其显示为AppDelegate
方法didFinishLaunchingWithOptions:
一部分,以使其更加独立于视图控制器。
由于您是 iOS 新手,因此您必须花一点时间来了解appdelegate
类的所有功能,然后了解viewController
生命周期方法,以便您了解应用程序的启动行为,这将解决您上面分享的问题:)
应用委托方法:
application:didFinishLaunchingWithOptions: —此方法允许您在您的应用程序显示给用户之前执行任何最终初始化。
application:willFinishLaunchingWithOptions: — 此方法是您的应用程序在启动时执行代码的第一次机会。
applicationDidBecomeActive: ——让你的应用知道它即将成为前台应用。 使用此方法进行最后一分钟的准备。
applicationWillResignActive: ——让你知道你的应用程序正在从前台应用程序转变。 使用此方法将您的应用置于静止状态。
applicationDidEnterBackground: —让您知道您的应用程序现在正在后台运行并且可能随时暂停。
applicationWillEnterForeground: —让您知道您的应用程序正在移出后台并返回到前台,但它尚未激活。
applicationWillTerminate: ——让你知道你的应用程序正在被终止。 如果您的应用程序被暂停,则不会调用此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.