ios – Passing knowledge between view controllers

[ad_1]

Swift 5

Effectively Matt Value’s reply is completely wonderful for passing knowledge, however I
am going to rewrite it, within the newest Swift model as a result of I imagine new
programmers discover it give up difficult as a result of new syntax and
strategies/frameworks, as unique publish is in Goal-C.

There are a number of choices for passing knowledge between view controllers.

  1. Utilizing Navigation Controller Push
  2. Utilizing Segue
  3. Utilizing Delegate
  4. Utilizing Notification Observer
  5. Utilizing Block

I’m going to rewrite his logic in Swift with the newest iOS framework


Passing Knowledge by Navigation Controller Push: From ViewControllerA to ViewControllerB

Step 1. Declare variable in ViewControllerB

var isSomethingEnabled = false

Step 2. Print Variable in ViewControllerB’ ViewDidLoad technique

override func viewDidLoad() {
    tremendous.viewDidLoad()
    // Print worth obtained by segue, navigation push
    print("Worth of 'isSomethingEnabled' from ViewControllerA: ", isSomethingEnabled)
}

Step 3. In ViewControllerA Move Knowledge whereas pushing by Navigation Controller

if let viewControllerB = UIStoryboard(title: "Major", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
    viewControllerB.isSomethingEnabled = true
    if let navigator = navigationController {
        navigator.pushViewController(viewControllerB, animated: true)
    }
}

So right here is the whole code for:

ViewControllerA

import UIKit

class ViewControllerA: UIViewController  {

    override func viewDidLoad() {
        tremendous.viewDidLoad()
    }

    // MARK: Passing knowledge by navigation PushViewController
    @IBAction func goToViewControllerB(_ sender: Any) {

        if let viewControllerB = UIStoryboard(title: "Major", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
            viewControllerB.isSomethingEnabled = true
            if let navigator = navigationController {
                navigator.pushViewController(viewControllerB, animated: true)
            }
        }
    }
}

ViewControllerB

import UIKit

class ViewControllerB: UIViewController {

    // MARK:  - Variable for Passing Knowledge by Navigation push
    var isSomethingEnabled = false

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        // Print worth obtained by navigation push
        print("Worth of 'isSomethingEnabled' from ViewControllerA: ", isSomethingEnabled)
    }
}

Passing Knowledge by Segue: From ViewControllerA to ViewControllerB

Step 1. Create Segue from ViewControllerA to ViewControllerB and provides Identifier = showDetailSegue in Storyboard as proven under

enter image description here

Step 2. In ViewControllerB Declare a viable named isSomethingEnabled and print its worth.

Step 3. In ViewControllerA cross isSomethingEnabled’s worth whereas passing Segue

So right here is the whole code for:

ViewControllerA

import UIKit

class ViewControllerA: UIViewController  {

    override func viewDidLoad() {
        tremendous.viewDidLoad()
    }

    // MARK:  - - Passing Knowledge by Segue  - -
    @IBAction func goToViewControllerBUsingSegue(_ sender: Any) {
        performSegue(withIdentifier: "showDetailSegue", sender: nil)
    }

    // Segue Delegate Methodology
    override func put together(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "showDetailSegue") {
            let controller = segue.vacation spot as? ViewControllerB
            controller?.isSomethingEnabled = true//passing knowledge
        }
    }
}

ViewControllerB

import UIKit

class ViewControllerB: UIViewController {
    var isSomethingEnabled = false

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        // Print worth obtained by segue
        print("Worth of 'isSomethingEnabled' from ViewControllerA: ", isSomethingEnabled)
    }
}

Passing Knowledge by Delegate: From ViewControllerB to ViewControllerA

Step 1. Declare Protocol ViewControllerBDelegate within the ViewControllerB file, however exterior the category

protocol ViewControllerBDelegate: NSObjectProtocol {

    // Lessons that undertake this protocol MUST outline
    // this technique -- and hopefully do one thing in
    // that definition.
    func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem merchandise: String?)
}

Step 2. Declare Delegate variable occasion in ViewControllerB

var delegate: ViewControllerBDelegate?

Step 3. Ship knowledge for delegate inside viewDidLoad technique of ViewControllerB

delegate?.addItemViewController(self, didFinishEnteringItem: "Knowledge for ViewControllerA")

Step 4. Affirm ViewControllerBDelegate in ViewControllerA

class ViewControllerA: UIViewController, ViewControllerBDelegate  {
// to do
}

Step 5. Affirm that you’ll implement a delegate in ViewControllerA

if let viewControllerB = UIStoryboard(title: "Major", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
    viewControllerB.delegate = self//confirming delegate
    if let navigator = navigationController {
        navigator.pushViewController(viewControllerB, animated: true)
    }
}

Step 6. Implement delegate technique for receiving knowledge in ViewControllerA

func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem merchandise: String?) {
    print("Worth from ViewControllerB's Delegate", merchandise!)
}

So right here is the whole code for:

ViewControllerA

import UIKit

class ViewControllerA: UIViewController, ViewControllerBDelegate  {

    override func viewDidLoad() {
        tremendous.viewDidLoad()
    }

    // Delegate technique
    func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem merchandise: String?) {
        print("Worth from ViewControllerB's Delegate", merchandise!)
    }

    @IBAction func goToViewControllerForDelegate(_ sender: Any) {

        if let viewControllerB = UIStoryboard(title: "Major", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
            viewControllerB.delegate = self
            if let navigator = navigationController {
                navigator.pushViewController(viewControllerB, animated: true)
            }
        }
    }
}

ViewControllerB

import UIKit

//Protocol decleare
protocol ViewControllerBDelegate: NSObjectProtocol {
    // Lessons that undertake this protocol MUST outline
    // this technique -- and hopefully do one thing in
    // that definition.
    func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem merchandise: String?)
}

class ViewControllerB: UIViewController {
    var delegate: ViewControllerBDelegate?

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        // MARK:  - - - -  Set Knowledge for Passing Knowledge by Delegate  - - - - - -
        delegate?.addItemViewController(self, didFinishEnteringItem: "Knowledge for ViewControllerA")
    }
}

Passing Knowledge by Notification Observer: From ViewControllerB to ViewControllerA

Step 1. Set and publish knowledge within the notification observer in ViewControllerB

let objToBeSent = "Take a look at Message from Notification"
        NotificationCenter.default.publish(title: Notification.Title("NotificationIdentifier"), object: objToBeSent)

Step 2. Add Notification Observer in ViewControllerA

NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), title: Notification.Title("NotificationIdentifier"), object: nil)

Step 3. Obtain Notification knowledge worth in ViewControllerA

@objc func methodOfReceivedNotification(notification: Notification) {
    print("Worth of notification: ", notification.object ?? "")
}

So right here is the whole code for:

ViewControllerA

import UIKit

class ViewControllerA: UIViewController{

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        // Add observer in controller(s) the place you need to obtain knowledge
        NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), title: Notification.Title("NotificationIdentifier"), object: nil)
    }

    // MARK: Methodology for receiving Knowledge by Submit Notification
    @objc func methodOfReceivedNotification(notification: Notification) {
        print("Worth of notification: ", notification.object ?? "")
    }
}

ViewControllerB

import UIKit

class ViewControllerB: UIViewController {

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        // MARK:Set knowledge for Passing Knowledge by Submit Notification
        let objToBeSent = "Take a look at Message from Notification"
        NotificationCenter.default.publish(title: Notification.Title("NotificationIdentifier"), object: objToBeSent)
    }
}

Passing Knowledge by Block: From ViewControllerB to ViewControllerA

Step 1. Declare block in ViewControllerB

var authorizationCompletionBlock:((Bool)->())? = {_ in}

Step 2. Set knowledge in block in ViewControllerB

if authorizationCompletionBlock != nil
{
    authorizationCompletionBlock!(true)
}

Step 3. Obtain block knowledge in ViewControllerA

// Receiver Block
controller!.authorizationCompletionBlock = { isGranted in
    print("Knowledge obtained from Block is: ", isGranted)
}

So right here is the whole code for:

ViewControllerA

import UIKit

class ViewControllerA: UIViewController  {

    override func viewDidLoad() {
        tremendous.viewDidLoad()
    }

    // MARK:Methodology for receiving Knowledge by Block
    override func put together(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "showDetailSegue") {
            let controller = segue.vacation spot as? ViewControllerB
            controller?.isSomethingEnabled = true

            // Receiver Block
            controller!.authorizationCompletionBlock = { isGranted in
                print("Knowledge obtained from Block is: ", isGranted)
            }
        }
    }
}

ViewControllerB

import UIKit

class ViewControllerB: UIViewController {

    // MARK: Variable for Passing Knowledge by Block
    var authorizationCompletionBlock:((Bool)->())? = {_ in}

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        // MARK: Set knowledge for Passing Knowledge by Block
        if authorizationCompletionBlock != nil
        {
            authorizationCompletionBlock!(true)
        }
    }
}

You will discover full pattern Software at my GitHub Please let me know when you’ve got any query(s) on this.

[ad_2]

Leave a Reply