Objective c viewdidload问题代码4.2

Objective c viewdidload问题代码4.2,objective-c,xcode4,viewcontroller,viewdidload,viewwillappear,Objective C,Xcode4,Viewcontroller,Viewdidload,Viewwillappear,我只是学习了如何使用push和pop在视图之间切换。 现在,在我的第二个视图中,我添加了一个标签,每当我的第二个视图被推送时,我都会改变她的值。 我添加标签,将她连接到我的文件所有者,并使用viewdidload更改她的值。 当我进入第二视野时,什么都没有发生。但是当我使用viewdiapper时,所有的工作都很完美(但是需要一秒钟才能更新标签值) 我的代码是: mysecondviewcontroller.h: @interface SecondViewController : UIViewC

我只是学习了如何使用push和pop在视图之间切换。 现在,在我的第二个视图中,我添加了一个标签,每当我的第二个视图被推送时,我都会改变她的值。 我添加标签,将她连接到我的文件所有者,并使用viewdidload更改她的值。 当我进入第二视野时,什么都没有发生。但是当我使用viewdiapper时,所有的工作都很完美(但是需要一秒钟才能更新标签值)

我的代码是: mysecondviewcontroller.h:

@interface SecondViewController : UIViewController
{
     IBOutlet UILabel *textLabel;
     NSString *label;
}

@property (copy) NSString *label;

@end
mysecondviewcontroller.m(当然是合成标签):

my firstviewcontroller.m(iAction):

我的viewdidload中有什么问题


谢谢

如果使用的是
viewDidLoad
,则需要在执行任何其他操作之前调用super函数

- (void)viewDidLoad
{
   [super viewDidLoad];
   textLabel.text = label;
    NSLog(@"viewdidload2");

// Do any additional setup after loading the view from its nib.
}
我认为还有另一个问题,您在按下视图控制器后设置了
secondVieController.label
,但这意味着在
viewdiload
运行时,
secondVieController.label
仍然为空。这应该可以解决它

- (IBAction)pushViewController:(id)sender
{
    static int count = 1;

    SecondViewController *secondVieController = [[SecondViewController alloc] init];
    secondVieController.title = @"second";
    secondVieController.label = [NSString stringWithFormat:@"number: %d", count];     
    [self.navigationController pushViewController:secondVieController animated:YES];   

    count++;

}

如果您想在每次加载视图时更新标签,则必须将代码写入ViewWillExample方法。

事实上,这几乎适用于您要重写的任何函数。不过也有例外,特别是当你不使用ARC时,dealloc函数是你最后调用的超函数。我喜欢这样想——在“in”的时候先调用super——init,viewwillbeen,等等,然后在退出的时候最后调用它——dealloc,viewwilldefine。
- (void)viewDidLoad
{
   [super viewDidLoad];
   textLabel.text = label;
    NSLog(@"viewdidload2");

// Do any additional setup after loading the view from its nib.
}
- (IBAction)pushViewController:(id)sender
{
    static int count = 1;

    SecondViewController *secondVieController = [[SecondViewController alloc] init];
    secondVieController.title = @"second";
    secondVieController.label = [NSString stringWithFormat:@"number: %d", count];     
    [self.navigationController pushViewController:secondVieController animated:YES];   

    count++;

}