zoukankan      html  css  js  c++  java
  • pushViewController:animated:的问题

    1.在AppDelegate.m中:

    2.在SecondViewController.h中:

    3.在FirstViewController.m中:

    4.在SecondViewController.m中:

    5.最后,运行程序,发现问题:SecondViewController对象被FirstViewController对象的navigationController push后,                                         

                                            SecondViewController对象先按顺序依次调用执行其-test方法、-setWords:方法、test2方法、test3方法,

                                            最后,SecondViewController对象的-viewDidLoad方法才被调用执行,

                                            这致使-setWords:方法中的_label.text = _words;成为[nil对象 setText:_words],这就相当于没有执行,自然不能达到效果,   

                                            这是因为在SecondViewController对象中的-viewDidLoad方法中才开始创建该UILabel对象,所以,在此之前,_label始终为空对象(即nil对象)

    6.问题总结:

    B *b = [[B alloc] init];

    [a.navigationController pushViewController:b animated:YES]; 

    [b methodInB];

    iOS7之前,这样写是没问题的, 调用流程:

                        依次是B中的-loadView -> -viewDidLoad -> -viewwillAppear -> -viewDidAppear -> 最后是-methodInB

    从iOS7之后,调用流程变了:

                         [a.navigationController pushViewController:b animated:YES];之后,

                         B中最先被调用执行的是-methodInB -> -loadView -> -viewDidLoad -> -viewwillAppear -> -viewDidAppear

    7.问题解决:

       既然push之后的栈顶子控制器的-viewDidLoad方法被最后调用执行,那就去掉-setWords:方法中的_label.text = _words;

       转而在-viewDidLoad方法中写上_label.text = _words;就可以成功赋值了!

  • 相关阅读:
    find the most comfortable road
    Rank of Tetris
    Segment set
    Codeforces Round #380 (Div. 2)D. Sea Battle
    A Bug's Life
    Is It A Tree?
    N皇后问题
    符号三角形
    2016 ICPC总结
    Sudoku Killer
  • 原文地址:https://www.cnblogs.com/hello-Huashan/p/4769374.html
Copyright © 2011-2022 走看看