Abhishek Biswas
Abhishek Biswas

Reputation: 1263

How to change global variable from inside the class and access in different view controller?

class A : UIViewController {

var b = B()
...

 override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = false

         print("inside view will appear")
         print(b.transferText) // Here showing Goku only
    }


}

And Second class

class B : UIViewController{
var transferText = "Goku"
 ...

 override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.transferText = "vegeta"
    }

}

So I have to basically transfer the transferText from class B to A, when i click the UINavigationBar Back button. Any help would be appreciated .

Upvotes: 0

Views: 894

Answers (3)

Naresh Reddy M
Naresh Reddy M

Reputation: 1096

Using delegation pattern would be a nice solution for passing data from child to parent (In your case - Class B to Class A) as you're saying Class A Has Class B instance and need to post data back to the parent when an event occurs.

1) Declare a protocol in Class B.

import UIKit
protocol ClassBDelegate
{
    func didUpdate(strTransferText : String)
}    
class B: UIViewController
{
    // Should have valid connection from your Interface builder to this function.
    @IBAction func navBarBtnUpdateClickked(_ sender: AnyObject)
    {
        // on Navigation bar click,Update transferText and fire the delegate again.
        transferText = "Updated Goku by navigation bar button tap"
        didTapNavigationbarButton();
    }
    var transferText = "Goku" // Default.
    var delegate : ClassBDelegate? // Declare a public variable to store delegate.
    override func viewDidLoad()
    {
        super.viewDidLoad()
    }
    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
        // Update transferText and fire delegate.
        transferText = "Updated Goku by navigation bar button tap"
        didTapNavigationbarButton(); // fire delegate from ViewWill appear.
    }
    private func didTapNavigationbarButton() -> Void
    {
        // Need to notify Instance of A class from here.
        // Using Delegation pattern!
        if(delegate != nil)
        {
            delegate!.didUpdate(strTransferText: transferText);
        }
     }

}

2) Confirm to ClassBDelegate in Class A and implement the delegate.

// Confirming to ClassB Delegate
class A: UIViewController,ClassBDelegate 
{
    var b : B?
    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Setup a segue in storyboard to move from class A to Class B to get class B Instance.
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        b = segue.destination as? B
        b?.delegate = self
        print("inside Prepare for segue function of Class A")
        print(b?.transferText) // Here showing Goku only(Default value).
    }
    internal func didUpdate(strTransferText: String)
    {
        print("Updated transferText in Class B " + strTransferText);
    }
}

3) There you go,You'll get call backs for Class A instance whenever button pressed in Class B.

  • you can achieve it by other ways like Observer pattern(NSNotificationCenter) or KVO pattern and even with Singleton pattern(Not applicable always).

Hope this helps to explore the best fit for your requirement.

Happy to help :)

Upvotes: 1

Mohammad Arifuzzaman
Mohammad Arifuzzaman

Reputation: 878

Use static variable to do that

class B : UIViewController{
static var transferText = "Goku"
 override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        B.transferText = "vegeta"
    }

}

in A class Use

print(B.transferText) 

or for use after navigation back you can use completion handler , when navigation back just call that completion block with text.

Upvotes: 3

nynohu
nynohu

Reputation: 1668

In class B, you declare property for A class

var a = A()

And in the B's viewWillDisappear, put

a.transferText = "vegeta" 

(you declared property transferText in A class).

Upvotes: 0

Related Questions