How to load an XIB?

前端 未结 3 1350
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 12:33

I have an app with 2 screens (MainViewController and AboutViewController). Upon the user clicking a button, I\'d like to load the AboutViewController screen, which is defin

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 13:00

    When you call [AboutViewController init], it's expected to call some form of [super init], which is a synonym for [UIViewController init]. When this happens, your view controller will automatically look for a nib file called (in your case) AboutViewController.xib. If it finds that file, it loads it's contents into your view controller for you.

    So basically, all you need to do is initialize your view controller, and make sure it has the same name as the associated nib file.

    If you wanted to load a nib file with a different name into your view controller, you could explicitly call initWithNibName:bundle: with the name of whichever nib file you like.

    If the standard init (with a same-name nib file) isn't working for you, there are a couple things you could check.

    • the spelling of the class name is the same as the spelling (and case) of the nib file
    • the nib file is included in the project, and not just sitting in the same directory
    • your UIViewController subclass's init method does also call [super init]
    • you are calling your UIViewController subclass's init method
    • you are indeed making your view controller's view visible

提交回复
热议问题