-1

This question might be asked already about hiding buttons, but I was wondering if I could just click a button which would affect the variables in another view controller. For example, I have firstViewController and endViewController. There's a button in endViewController that the user presses which should change a variable in the firstViewController. Is there a way to access the endViewController button from the firstViewController?

Edit

I haven't tried much so far except control clicking the endViewController button into the firstViewController (which didn't work).

class firstViewController: UIViewController {
    @IBAction func nextButton(_ sender: Any) { //button that sits in endViewController
    
    }
}

3 Answers3

1

You can use the DELEGATE PATTERN to pass data back:

Here's a little help on delegates between two view controllers:

Step 1: Make a protocol in the UIViewController that you will be removing/will be sending the data.

protocol FooTwoViewControllerDelegate:class {
    func myVCDidFinish(_ controller: FooTwoViewController, text: String)
}

Step2: Declare the delegate in the sending class (i.e. UIViewcontroller)

class FooTwoViewController: UIViewController {
    weak var delegate: FooTwoViewControllerDelegate?
    [snip...]
}

Step3: Use the delegate in a class method to send the data to the receiving method, which is any method that adopts the protocol.

@IBAction func saveColor(_ sender: UIBarButtonItem) {
        delegate?.myVCDidFinish(self, text: colorLabel.text) //assuming the delegate is assigned otherwise error
}

Step 4: Adopt the protocol in the receiving class

class ViewController: UIViewController, FooTwoViewControllerDelegate {

Step 5: Implement the delegate method

func myVCDidFinish(_ controller: FooTwoViewController, text: String) {
    colorLabel.text = "The Color is " +  text
    controller.navigationController.popViewController(animated: true)
}

Step 6: Set the delegate in the prepareForSegue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mySegue" {
        let vc = segue.destination as! FooTwoViewController
        vc.colorString = colorLabel.text
        vc.delegate = self
    }
}
    

And that should work. This is of course just code fragments, but should give you the idea. For a long explanation of this code you can go over to my blog entry here:

segues and delegates

If you are interested in what's going on under the hood with a delegate I did write on that here:

under the hood with delegates

original answer

SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96
1

First View Controller

The code for the First View Controller is

import UIKit

class FirstViewController: UIViewController, DataEnteredDelegate {

@IBOutlet weak var label: UILabel!

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showSecondViewController" {
        let secondViewController = segue.destinationViewController as! SecondViewController
        secondViewController.delegate = self
    }
}

func userDidEnterInformation(info: String) {
    label.text = info
}
}

Note the use of our custom DataEnteredDelegate protocol.

Second View Controller and Protocol

The code for the second view controller is

import UIKit

// protocol used for sending data back
protocol DataEnteredDelegate: class {
func userDidEnterInformation(info: String)
}

class SecondViewController: UIViewController {

// making this a weak variable so that it won't create a strong reference cycle
weak var delegate: DataEnteredDelegate? = nil

@IBOutlet weak var textField: UITextField!

@IBAction func sendTextBackButton(sender: UIButton) {
    
    // call this method on whichever class implements our delegate protocol
    delegate?.userDidEnterInformation(textField.text!)
    
    // go back to the previous view controller
    self.navigationController?.popViewControllerAnimated(true)
}
}

Note that the protocol is outside of the View Controller class.

That's it. Running the app now you should be able to send data back from the second view controller to the first.

Original post: https://stackoverflow.com/a/33229483/13783496

0

There are 2 methods:

  1. You can create a segue from button in endviewcontroller to firstviewcontroller in storyboard. You can configure the func prepare(for segue: UIStoryboardSegue, sender: Any?) for it. let endVC = endViewcontroller() endVC.color = "blue"

  2. You can keep the variable whose value needs to be changed as static datatype. On the click action of button, you can access variable as, EndViewController.color = "Red". Kindly use static variables only if you want other Viewcontrollers to access it directly.

AlBlue
  • 23,254
  • 14
  • 71
  • 91