-1

I have two classes:

class ExplorerViewController: UIViewController {

    lazy var studyButton: ExploreButton = {
        let button = ExploreButton()
        button.setTitle("Study", forState: .Normal)
        return button
    }()

}

and

class ViewController: UIViewController, UISearchBarDelegate, LocateOnTheMap, GMSMapViewDelegate {

}

I'm trying to make it so that when I click the studyButton, it sends the button title to ViewController and goes to that view.

I'm not using storyboards and am having trouble with segues since every tutorial seems to give different examples that are specific to the things they've been working with and 95% of them seem to be operating with storyboard. Can someone give me a general way of how to do this?

How do I give the starting view controller an identifier because it isn't instantiated like the other controllers that I 'move' to after. How can I move from ViewController to ExplorerViewController and then move back to that same ViewController (with all changes intact).

tryingtolearn
  • 2,528
  • 7
  • 26
  • 45
  • 1
    http://stackoverflow.com/questions/32051629/passing-variables-between-storyboards-without-segues-swift – Shades Aug 21 '16 at 13:51

4 Answers4

0

Create an initializer for your ViewController that receives the "title" variable:

class ViewController: UIViewController, UISearchBarDelegate, LocateOnTheMap, GMSMapViewDelegate {
    var btnTitle: String?

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?, btnTitle:String?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.btnTitle = btnTitle
    }
}

When creating the ViewController object use this initializer.

var viewController = ViewController(nibName: "ViewController", bundle: nil, btnTitle: title
Max Pevsner
  • 4,098
  • 2
  • 18
  • 32
0

You can initialize UIViewController that you want navigate to, assign data to properties in that controller and call this method:

presentViewController(viewController, animated: true, completion: nil)

For example:

let destinationViewController = ViewController()
destinationViewController.frame = self.view.frame
destinationViewController.buttonTitle = "title"
self.presentViewController(destinationViewController, animated: true, completion: nil)

Although I would suggest you to get familiar with Storyboards and perform navigation with Segues.

  • Thanks for this answer. This answer lets me move from explorerViewController to viewController. Is there a way to move back to that instance of explorerViewController from viewController? – tryingtolearn Aug 22 '16 at 16:15
  • Yes, you can use method called ```dismissViewControllerAnimated``` for example: ```self.dismissViewControllerAnimated(true, completion:nil)``` – Paweł Kuźniak Aug 23 '16 at 07:45
0

Make sure of two things:-

1.) You have given your viewController an StoryBoard ID lets say "viewControllerVC_ID" in it's Identity inspector

2.) You have NavigationController Embed in to your Initial entry point View Controller

In ViewController declare a variable

var btnLabelTxt : String!

Create an @IBAction of that button in ExplorerViewController :-

@IBAction func exploreBtnAction(sender : UIButton!){

        let vcScene = self.navigationController?.storyboard?.instantiateViewControllerWithIdentifier("viewControllerVC_ID") as! ViewController

        vcScene.btnLabelTxt = "Study"

        //or you can just access the button itself in the viewController and set the title
        //By vcScene.yourBtn.setTitle("Study", forState: .Normal)   

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


}
Dravidian
  • 9,945
  • 3
  • 34
  • 74
-3

please see this question How to push viewcontroller ( view controller )? for how to switch between views.

to pass data once you have reference to the new view, you can assign the data to a property of that view.

Community
  • 1
  • 1
Sagdeshp
  • 1
  • 2