如何从xib文件加载viewcontroller?

时间:2016-04-14 06:47:09

标签: swift delegates xib

我尝试使用delegates从自定义单元格加载viewcontroller。但是我从set委托中得到nil! 以下是有人可以提供帮助的示例! 1.在Cell中

protocol hotelFindDelegate{
func modalDidFinished(modalText: String)
}
class hotelFindCell: UITableViewCell {

var delegate:hotelFindDelegate?
@IBAction func findButton(sender: AnyObject) {       

    self.delegate!.modalDidFinished("HELLO")
    print("Damn nothing works")
}

2。在主视图中

class MainViewController:hotelFindDelegate {
let modalView = hotelFindCell()
override func viewDidLoad() {
    super.viewDidLoad()
    self.modalView?.delegate = self  
}

func modalDidFinished(modalText: String){
 let viewController:UIViewController = UIStoryboard(name: "Main", bundle:          nil).instantiateViewControllerWithIdentifier("HotelListVC") as UIViewController

 self.presentViewController(viewController, animated: false, completion: nil)
 self.modalView.delegate = self
 print(modalText)  
}

2 个答案:

答案 0 :(得分:1)

  

要从XIB加载视图控制器,请执行以下步骤。

let settingVC : SettingsViewController = SettingsViewController(nibName :"SettingsViewController",bundle : nil)
  

稍后您可以推送相同的视图控制器对象,如

self.navigationController?.pushViewController(settingsVC, animated: true)

答案 1 :(得分:0)

NSBundle.mainBundle()。loadNibNamed(" viewController",owner:self,options:nil)