Warning: Attempt to present BBBViewController on AAAViewController whose view is not in the window h...

原文地址:http://blog.changyy.org/2014/08/ios-warning-attempt-to-present.html

有點久沒有純手工寫 UI 互動類的 Orz 測試時不知為何在 viewDidLoad 彈跳新的 YourViewController 時,會出現這個 Warning 並且沒有任何結果。

查詢了一下,發現要在 viewDidAppear 呼叫就能避免現象,有人說是在 viewDidLoad 時沒有 Window Hierarchy 資訊。擺在 viewDidAppear 時,若彈跳的 YourViewController 關閉時,切入 ViewController 時,又進入 viewDidAppear 又會彈跳 YourViewController 出來。

總之,測試時應該可以先這樣用吧,若要正式使用大概要多加一些條件判斷:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self presentViewController:[[YourViewController alloc] init] animated:YES completion:NULL];
}
參考資料

@ Updated 2014-08-17: 另一招則是在 viewDidLoad 中,採用 dispatch_async -> dispatch_get_main_queue 使用也行。

- (void)viewDidLoad
{
    [super viewDidLoad];

    dispatch_async(dispatch_get_main_queue(), ^{
        YourViewController *v = [[YourViewController alloc] init];
        [self presentViewController:v animated:YES completion:^{}];
    });
}

转载于:https://www.cnblogs.com/vickjo/p/4835181.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值