WangYudong
WangYudong

Reputation: 4423

View is onscreen but self.view.window is still nil

I have a ViewController, let's say FooViewController and it has a property:

@property (nonatomic, strong) NSAttributedString *foo;

I want to update the UI when the view appears, so the viewDidLoad and foo's setter is like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.foo = [[NSAttributedString alloc] initWithString:@"test" attributes:@{ NSForegroundColorAttributeName : [UIColor greenColor]} ];
}

- (void)setFoo:(NSAttributedString *)foo
{
    _foo = foo;
    if (self.view.window) [self updateUI];
}

I expected updateUI method would be called when the view is onscreen, but it didn't. Only when I remove if (self.view.window), the method is called. What's the issue here?

Thanks

Upvotes: 4

Views: 2552

Answers (1)

trojanfoe
trojanfoe

Reputation: 122391

Move the code to viewDidAppear (or just the bit that calls [self updateUI]).

Loading merely means it's been deserialized from the NIB, while appearing means it's part of the view hierarchy.

Upvotes: 5

Related Questions