简体   繁体   English

如何使ViewController在viewDidLoad()上正确加载自己的nib?

[英]How to make ViewController load itself from nib on viewDidLoad() properly?

So I have a nib file for my CustomViewController and every time I want to present this Controller I do 所以我有一个我的CustomViewController的nib文件,每次我想呈现这个控制器我都这样做

    let vc = CustomViewController(nibName: "CustomViewController", bundle: nil)
    self.present(vc, animated: true, completion: nil)

What I want is to make CustomViewController to load itself from nib by itself. 我想要的是让CustomViewController自己从nib加载自己。 So that the call in the parent VC would be like this 因此,父VC中的调用将是这样的

    let vc = CustomViewController()
    self.present(vc, animated: true, completion: nil)

In CustomViewController add this init method. 在CustomViewController中添加此init方法。 it will works as per your need. 它将根据您的需要工作。

convenience init() {
     self.init(nibName: "CustomViewController", bundle: nil)
}

试试这种方式....

MyViewClass *myViewObject = [[[NSBundle mainBundle] loadNibNamed:@"MyViewClassNib" owner:self options:nil] objectAtIndex:0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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