Parion
Parion

Reputation: 438

How do i load a viewcontroller from a xib file?

I tried to load a viewcontroller from a custom cell using delegates.But i get nil from the set delegate! Here is a sample if anyone can help! 1. In 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. In Main View

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)  
}

Upvotes: 2

Views: 5205

Answers (2)

Anil Kukadeja
Anil Kukadeja

Reputation: 1358

To load view controller from XIB do the following steps.

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

later on you can push the same view controller object like

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

Upvotes: 4

Jay Bhadja
Jay Bhadja

Reputation: 30

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

Upvotes: 0

Related Questions