swift - iOS Storyboard: Not able to instantiate view controller properly -


i'm having trouble instantiating custom view controller.

this storyboard set up. third view controller 1 i'm trying present.

i've tried both of these methods.

1: results in black screen.

var searchcontroller: searchcontroller = searchcontroller() self.presentviewcontroller(searchcontroller, animated: true, completion: nil) 

2: results in white, empty view controller popping up.

let mainstoryboard = uistoryboard(name: "main", bundle: nsbundle.mainbundle()) let searchcontroller : uiviewcontroller = mainstoryboard.instantiateviewcontrollerwithidentifier("searchcontroller") as! uiviewcontroller self.presentviewcontroller(searchcontroller, animated: true, completion: nil) 

here code actual view controller:

class searchcontroller: uiviewcontroller {      lazy var searchbar: uisearchbar = uisearchbar(frame: cgrectmake(0, 0, 200, 20))      override func viewdidload() {         super.viewdidload()          var leftitem = (uibarbuttonitem)(customview: searchbar)         self.title = "search"         self.navigationitem.leftbarbuttonitem = leftitem     }      override func didreceivememorywarning() {         super.didreceivememorywarning()     }     } 

it's confusing because custom view controller presented while using method #1 above. why wouldn't work controller?

thanks lot everyone.

when using storyboards don't need use presentviewcontroller or instantiate view controllers manually. instead it's best use segue move 1 uiviewcontroller next.

1. in case, want show segue, looks you've done judging storyboard.

enter image description here

2. need give segue identifier can selecting segue , going attributes editor.

3. perform segue call following first view controller (instead of presentviewcontroller).

self.performseguewithidentifier("youridhere", sender: self) 

this cause storyboard instantiate searchviewcontroller , present in way you've defined segue.

4. if want pass values searchviewcontroller, can override prepareforsegue in uiviewcontroller. in first view controller:

override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     if let searchviewcontroller = segue.destinationviewcontroller segue.identifier == "youridhere") {         // can set variables have access in `searchviewcontroller`.     } } 

and should it!


Popular posts from this blog