简体   繁体   English

在 Xcode 8 中使用 UIAlertController 和 Objective-C

[英]Using UIAlertController in Xcode 8 with Objective-C

I am trying to show a UIAlertController every time the user start the app.每次用户启动应用程序时,我都试图显示一个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];
}

But this is not working.但这是行不通的。 I run the app in Xcode simulator.我在 Xcode 模拟器中运行该应用程序。 The app runs but no alertview is showing.该应用程序运行但没有显示警报视图。 What am I doing wrong?我究竟做错了什么?

Try opening the UIAlertController in the viewDidAppear: delegate instead.尝试在viewDidAppear:委托中打开UIAlertController Alternatively, you could show it as part of your AppDelegate method didFinishLaunchingWithOptions: to make it more view-controller independent.或者,您可以将其显示为AppDelegate方法didFinishLaunchingWithOptions:一部分,以使其更加独立于视图控制器。

As you are newbie in iOS so you must spend your little time to understand all the functions of appdelegate class and after that viewController life cycle method so you will know about the startup behavior of application this will solve your problem like you shared above :)由于您是 iOS 新手,因此您必须花一点时间来了解appdelegate类的所有功能,然后了解viewController生命周期方法,以便您了解应用程序的启动行为,这将解决您上面分享的问题:)

App delegate methods:应用委托方法:

  • application:didFinishLaunchingWithOptions: —This method allows you to perform any final initialization before your app is displayed to the user. application:didFinishLaunchingWithOptions: —此方法允许您在您的应用程序显示给用户之前执行任何最终初始化。

  • application:willFinishLaunchingWithOptions: —This method is your app's first chance to execute code at launch time. application:willFinishLaunchingWithOptions: — 此方法是您的应用程序在启动时执行代码的第一次机会。

  • applicationDidBecomeActive: —Lets your app know that it is about to become the foreground app. applicationDidBecomeActive: ——让你的应用知道它即将成为前台应用。 Use this method for any last minute preparation.使用此方法进行最后一分钟的准备。

  • applicationWillResignActive: —Lets you know that your app is transitioning away from being the foreground app. applicationWillResignActive: ——让你知道你的应用程序正在从前台应用程序转变。 Use this method to put your app into a quiescent state.使用此方法将您的应用置于静止状态。

  • applicationDidEnterBackground: —Lets you know that your app is now running in the background and may be suspended at any time. applicationDidEnterBackground: —让您知道您的应用程序现在正在后台运行并且可能随时暂停。

  • applicationWillEnterForeground: —Lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active. applicationWillEnterForeground: —让您知道您的应用程序正在移出后台并返回到前台,但它尚未激活。

  • applicationWillTerminate: —Lets you know that your app is being terminated. applicationWillTerminate: ——让你知道你的应用程序正在被终止。 This method is not called if your app is suspended.如果您的应用程序被暂停,则不会调用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM