1

i just learn how to change switch between views with push and pop. now, to my second view i add a label witch i wand to change her value every time my second view is push. i add the label, connect her to my file owner's and i use viewdidload to change her value. when i entered to my second view nothing is happed. but when i use viewdidapper all work perfect(but it take a second until the label value is update).

my code is: mysecondviewcontroller.h:

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

@property (copy) NSString *label;

@end

mysecondviewcontroller.m(ofcourse i synthesize label):

-(void)viewDidAppear:(BOOL)animated
 {
   textLabel.text = label;
   NSLog(@"viewdidapper2");
 }

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

// Do any additional setup after loading the view from its nib.
}

my firstviewcontroller.m(IBAction):

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

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

   count++;

}

what is the problem in my viewdidload?

thanks!

4

2 に答える 2

2

を使用している場合はviewDidLoad、他の処理を行う前にスーパー関数を呼び出す必要があります。

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

// Do any additional setup after loading the view from its nib.
}

別の問題があると思いますsecondVieController.label.View Controllerを押した後に設定していますが、これはviewDidLoad実行時に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++;

}
于 2012-05-26T16:25:18.940 に答える
0

ビューが読み込まれるたびにラベルを更新する場合は、Viewwillappear メソッドにコードを記述する必要があります。

于 2012-05-27T05:12:39.000 に答える