简体   繁体   中英

Equivalent of `loadViewIfNeeded` for cocoa?

I'm looking for equivalent function on AppKit for UIKit's loadViewIfNeeded . (this is in order to populate outlets programmatically).

Thank you

Simply query the view property on NSViewController .

In other words, the equivalent implementation is just:

// Swift
func loadViewIfNeeded() {
    _ = self.view
}
// Obj-C
- (void)loadViewIfNeeded {
   (void)self.view;
}

I don't recommend making this added abstraction, as this is a Cocoa convention that is understood. In my mind, if you're trying to force load the view, it usually is a code smell. Your view should be able to render itself based on the model representing it.

You just need to reference the view on the NSViewController . Since you didn't specify the language, I'll give the answer in both Objective-C:

(void)viewController.view;

and Swift:

_ = viewController.view

NSViewController has private method named -[NSViewController _loadViewIfRequired] , maybe equivalent of loadViewIfNeeded .

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