簡體   English   中英

在 Xcode 8 中使用 UIAlertController 和 Objective-C

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM