Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenting a view controller programmatically in swift

Hi I am trying to convert the following objective C code into swift to navigate from one view controller to another view controller when a button is clicked. any help would be much appreciated

This is taken from apple's programming guide

  - (void)add:(id)sender {
 // Create the root view controller for the navigation controller
 // The new view controller configures a Cancel and Done button for the
 // navigation bar.
   RecipeAddViewController *addController = [[RecipeAddViewController alloc]
                   init];

 // Configure the RecipeAddViewController. In this case, it reports any
 // changes to a custom delegate object.
   addController.delegate = self;

 // Create the navigation controller and present it.
  UINavigationController *navigationController = [[UINavigationController alloc]
                         initWithRootViewController:addController];
  [self presentViewController:navigationController animated:YES completion: nil];
  }

my code is indicated below, but not sure how to implement in the navigation controller, in the storyboard my mainSectionViewController is embedded with a Navigation Controller

    func sectionBtnClicked (sender: UIButton!) {


        let sectionController = self.storyboard?.instantiateViewControllerWithIdentifier("mainSectionsVC") as mainSectionViewController


        let navController = UINavigationcontroler. ///not sure what actually comes here, any help would be appreciated


        self.presentViewController(navController, animated: true, completion: nil)


}
like image 685
Magesh Avatar asked Dec 05 '14 23:12

Magesh


People also ask

How do I embed a view controller in a navigation controller programmatically Swift?

Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.

How do I create a view programmatically in Swift?

import UIKit class ViewController: UIViewController { override func viewDidLoad() { super. viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let myNewView=UIView(frame: CGRect(x: 10, y: 100, width: 300, height: 200)) // Change UIView background colour myNewView.


2 Answers

Do you want to present navController modally?

if yes, this is the answer

self.presentViewController(navController, animated: true, completion: nil)

"self" is the current view controller that will present the navController

And put it like this,

class ViewController: UIViewController {       

    override func viewDidLoad() {
        super.viewDidLoad()

        var theButton = UIButton()

        // Add the event to button
        theButton.addTarget(self, action: "buttonTouchInside:", forControlEvents: .TouchUpInside)

        self.view.addSubview(theButton)
    }

    func buttonTouchInside(sender:UIButton!)
    {
        // When the button is touched, we're going to present the view controller

        // 1. Wrap your view controller within the navigation controller

        let navController = UINavigationController(rootViewController: yourViewController)

        // 2. Present the navigation controller

        self.presentViewController(navController, animated: true, completion: nil)
    }

}

But,

If you want to navigate between viewController in the navigationController, you can use

self.navigationController.pushViewController(viewControllerToPush, animated: true)
like image 51
Edward Anthony Avatar answered Sep 21 '22 11:09

Edward Anthony


I made a simple solution. Here it is..

func actioncall () {
    let loginPageView =  self.storyboard?.instantiateViewControllerWithIdentifier("LoginPageID") as! ViewController
    self.presentViewController(loginPageView, animated: true, completion: nil)
}
like image 26
Alvin George Avatar answered Sep 21 '22 11:09

Alvin George