Thursday, September 17, 2015

Pass parameters / strings between view controllers programmatically (without using storyboard segues)

1. Create a new file called MyClass.swift as:

class MyClass {
    var string1 : String = ""
    var string2 : String = ""
    static let myInstance = MyClass()

}

2. Modify ViewController.swift as:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.cyanColor()
        
        let myLabel = UILabel(frame: CGRectMake(50, 100, 300, 20))
        myLabel.text = "ViewController"
        view.addSubview(myLabel)
        
        let myButton = UIButton(frame: CGRectMake(50, 200, 100, 20))
        myButton.addTarget(self, action: "btnPressed:", forControlEvents: UIControlEvents.TouchUpInside)
        myButton.setTitle("Button", forState: UIControlState.Normal)
        myButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        view.addSubview(myButton)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        let myLabelFrom = UILabel(frame: CGRectMake(50, 150, 300, 20))
        myLabelFrom.text = "From 2nd VC:\(MyClass.myInstance.string2)"
        myLabelFrom.tag = 50
        view.addSubview(myLabelFrom)
    }
    
    func btnPressed(sender: UIButton){
        //Remove myLabelFrom label because it will be drawn again in viewWillAppear()
        view.viewWithTag(50)?.removeFromSuperview()
        
        //Set the string to be passed to the second view controller.
        MyClass.myInstance.string1 = "String from VC"
        
        let secondVC = SecondViewController()
        
        //Set view controller transition animation style
        secondVC.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
        self.presentViewController(secondVC, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

3. Create a new file called SecondViewController.swift as:

import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.yellowColor()
        
        let myLabelSecond = UILabel(frame: CGRectMake(70, 150, 300, 20))
        myLabelSecond.text = "SecondViewController:"
        view.addSubview(myLabelSecond)
        let myLabelSecondFrom = UILabel(frame: CGRectMake(70, 200, 300, 20))
        myLabelSecondFrom.text = "From VC:\(MyClass.myInstance.string1)"
        view.addSubview(myLabelSecondFrom)
        
        let myButtonSecond = UIButton(frame: CGRectMake(70, 250, 100, 20))
        myButtonSecond.addTarget(self, action: "btnPressedSecond:", forControlEvents: UIControlEvents.TouchUpInside)
        myButtonSecond.setTitle("Back", forState: UIControlState.Normal)
        myButtonSecond.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        view.addSubview(myButtonSecond)
    }

    func btnPressedSecond(sender: UIButton){
        //Set the string to be passed to the original view controller.
        MyClass.myInstance.string2 = "String from 2nd VC"
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Run the iOS simulator and the first view controller is shown as:


Press 'Button' and the second view controller with yellow background is shown as:


 After pressing the 'Back' button, the screen returns to the first view controller. The string from the second VC is now displayed.


No comments:

Post a Comment