简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM