Fail to assign value from A to B in Xcode

2019-06-10 11:45发布

问题:

I encounter a strange thing while coding in Xcode 4.3. The nature of problem is described in the title. I mentioned this together with a storyboard connection problem in another post. The connection problem seems to go away at least for now. But the value assign problem persists. So I put it up as a new post.

-(void) setQuestion:(NSString *)question
    {
        _question = question;
        self.questionLabel.text = question;
        NSLog(@"The quesion is %@",question);
        NSLog(@"The quesion label text is %@",self.questionLabel.text);
    }

The result of the NSLog:

2012-07-29 04:03:53.817 Kitchen Sink[18628:f803] The quesion is What do you want your label to say? 
2012-07-29 04:03:53.820 Kitchen Sink[18628:f803] The quesion label text is (null)

Any thought?

回答1:

Thanks for Ludvig and Mills. I add a line in viewDidLoad and problem solved. But the NSLog result is interesting.

I add this in code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.questionLabel.text = self.question;
    ...
}

And I put up NSLog(@"The label is %@",self.questionLabel); It shows the following:

2012-07-30 06:08:59.066 Kitchen Sink[21285:f803] The quesion is What do you want your label to say?
2012-07-30 06:08:59.068 Kitchen Sink[21285:f803] The quesion label text is (null)
2012-07-30 06:08:59.069 Kitchen Sink[21285:f803] The label is (null)

Why the label is still null? But self.questionLabel.text do change this time.