簡體   English   中英

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

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

所以我有一個我的CustomViewController的nib文件,每次我想呈現這個控制器我都這樣做

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

我想要的是讓CustomViewController自己從nib加載自己。 因此,父VC中的調用將是這樣的

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

在CustomViewController中添加此init方法。 它將根據您的需要工作。

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