【问题标题】:performSegueWithIdentifier before viewDidLoad and viewDidAppear sometimes it works and sometimes not在 viewDidLoad 和 viewDidAppear 之前 performSegueWithIdentifier 有时有效,有时无效
【发布时间】:2015-11-24 09:56:54
【问题描述】:

当我在viewDidLoadviewDidAppear 之前调用performSegueWithIdentifier 时,有时会起作用,有时会不起作用。

AppDelegate

- (void)applicationDidBecomeActive:(UIApplication *)application {
   NSLog(@"applicationDidBecomeActive");
[self abcNotif]; // the method post the notification.
}

VC1:

-(void)awakeFromNib {

    NSLog(@"awakeFromNib");
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(theNotif:) name:@"abcNotif" object:nil];
}

-(void)theNotif:(UILocalNotification*)notif {
    if([[[NSUserDefaults standardUserDefaults]valueForKey:@"flag"]isEqualToString:@"YES"]) {
        [self performSegueWithIdentifier:@"seg1" sender:self];
         NSLog(@"theNotif = %@", [[notif userInfo]valueForKey:@"notif1Key"]);
    }
}

登录控制台

awakeFromNib
didFinishLaunchingWithOptions
applicationDidBecomeActive
theNotif = notif1Value
viewDidLoad

尽管在viewDidLoadviewDidAppear 之前调用了performSegueWithIdentifier,但一切正常。但在某些情况下,这不起作用。为什么会有这种行为。人们也问了这些问题Why doesn't performSegueWithIdentifier work inside viewDidLoad?

【问题讨论】:

    标签: ios objective-c uiviewcontroller uistoryboardsegue nsnotificationcenter


    【解决方案1】:

    要理解的关键是 UIViewController 子类会延迟加载(创建)它们的视图属性。所以 viewDidLoad 会在视图加载后被调用,但那是什么时候呢?当然,启动应用程序的正常周期会在某个时间点将其添加到窗口中,但您可以通过在视图上进行调用来使其更早发生。尝试插入[self view];在您调用 performSegue.. 之前,这将确保加载视图。即便如此,它也不能确保视图已添加到窗口中,如果还没有发生,那么我看不到如何推送模式。

    【讨论】:

    • 在调用 [self view] 之后唯一的变化是 viewDidLoad 在通知之前被调用。但是在 viewDidLoad 和 viewDidAppear 之前 performSegueWithIdentifier 是如何工作的?
    • 好吧,如果你的 segue 正在推送另一个 ViewController,那么一个 viewController 怎么能在它进入堆栈之前推送另一个呢?
    • 这就是我要问的:)
    猜你喜欢
    • 2015-02-11
    • 2012-08-02
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多