viewcontroller - Presenting a view controller programmatically in swift -
hi trying convert following objective c code swift navigate 1 view controller view controller when button clicked. appreciated
this taken apple's programming guide
- (void)add:(id)sender { // create root view controller navigation controller // new view controller configures cancel , done button // navigation bar. recipeaddviewcontroller *addcontroller = [[recipeaddviewcontroller alloc] init]; // configure recipeaddviewcontroller. in case, reports // changes custom delegate object. addcontroller.delegate = self; // create navigation controller , present it. uinavigationcontroller *navigationcontroller = [[uinavigationcontroller alloc] initwithrootviewcontroller:addcontroller]; [self presentviewcontroller:navigationcontroller animated:yes completion: nil]; }
my code indicated below, not sure how implement in navigation controller, in storyboard mainsectionviewcontroller embedded navigation controller
func sectionbtnclicked (sender: uibutton!) { let sectioncontroller = self.storyboard?.instantiateviewcontrollerwithidentifier("mainsectionsvc") mainsectionviewcontroller let navcontroller = uinavigationcontroler. ///not sure comes here, appreciated self.presentviewcontroller(navcontroller, animated: true, completion: nil) }
do want present navcontroller modally?
if yes, answer
self.presentviewcontroller(navcontroller, animated: true, completion: nil)
"self" current view controller present navcontroller
and put this,
class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() var thebutton = uibutton() // add event button thebutton.addtarget(self, action: "buttontouchinside:", forcontrolevents: .touchupinside) self.view.addsubview(thebutton) } func buttontouchinside(sender:uibutton!) { // when button touched, we're going present view controller // 1. wrap view controller within navigation controller let navcontroller = uinavigationcontroller(rootviewcontroller: yourviewcontroller) // 2. present navigation controller self.presentviewcontroller(navcontroller, animated: true, completion: nil) } }
but,
if want navigate between viewcontroller in navigationcontroller, can use
self.navigationcontroller.pushviewcontroller(viewcontrollertopush, animated: true)
Comments
Post a Comment