Cameron
Cameron

Reputation: 185

Capture a var from a function in a different ViewController | Swift

I am making a horoscope app and in the main view controller you can click a series of buttons to pull up a pop up screen which would display their horoscope.

I have a major function buttonLayout() that makes all the buttons for my ViewController and sets their images.

After you click on a button I want it to bring up a popUp WITH THAT SAME BUTTON THAT THEY CLICKED ON IN THE MAIN VIEWCONTROLLER

The part I capitalized is what I cannot figure out how to do.

main ViewController

clicking a button

the popUp view controller where I want the corresponding button to be displayed

This is my code for the ViewController: // ViewController.swift // Zodiacv2

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var headerLabel: UILabel! 
@IBOutlet weak var subHeaderLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    headerLayout()
    buttonLayout()
}

func headerLayout() {
    headerLabel.text = header
    subHeaderLabel.text = subHeader
}

func buttonLayout() {
    for index in 0..<9 {
        var yPos = 195
        var xPos = (105 * index) + 50
        if (xPos >= 300) {
            yPos += 135
            xPos -= 315
        }
        if (xPos >= 300) {
            yPos += 135
            xPos -= 315
        }
        let button = UIButton()
        let buttonImage = UIImage(named: sign[index])
        button.setImage(buttonImage, for: .normal)
        button.frame = CGRect(x: xPos, y: yPos, width: 105, height: 130)
        button.tag = 5
        button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
        view.addSubview(button)
    }
}

func buttonClicked() {
    let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopUp") as! PopUpViewController
    self.addChildViewController(popOverVC)
    popOverVC.view.frame = self.view.frame
    self.view.addSubview(popOverVC.view)
    popOverVC.didMove(toParentViewController: self)
}

}

This is my code for my popUp View Controller:

//  PopUpViewController.swift
//  Zodiacv2
import UIKit

class PopUpViewController: UIViewController {

@IBOutlet weak var headerlabelPop: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    self.showAnimate()
    self.headerlabelPop.text = subHeader
}

func showButton() {
    let vc = UIStoryboard(name: "PopUp", bundle: nil).instantiateViewController(withIdentifier: "Main") as! ViewController

}

func showAnimate() {
    self.view.transform = CGAffineTransform(scaleX: 1.3,y: 1.3)
    self.view.alpha = 0.0
    UIView.animate(withDuration: 0.25, animations: {
        self.view.alpha = 1.0
        self.view.transform = CGAffineTransform(scaleX: 1.0,y: 1.0)
    });
}

@IBAction func closePopUp(_ sender: UIButton) {
    removeAnimate()
}

func removeAnimate() {
    UIView.animate(withDuration: 0.25, animations: {
        self.view.transform = CGAffineTransform(scaleX: 1.3,y: 1.3)
        self.view.alpha = 0.0
    }, completion: {(finished : Bool) in
        if (finished) {
            self.view.removeFromSuperview()
        }
    });
}

}

In the showButton function I want to access either the index so I can say sign[index] or the UIButton image from the buttonLayout() function.

Upvotes: 3

Views: 98

Answers (1)

Gabriel Goncalves
Gabriel Goncalves

Reputation: 5160

You can just inject the value of the button to the new view controller. (This is code from the code you shared)

func buttonClicked(_ sender: UIButton) {
    let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopUp") as! PopUpViewController
    popOverVC.characteristic = sign[sender.tag]
    self.addChildViewController(popOverVC)
    popOverVC.view.frame = self.view.frame
    self.view.addSubview(popOverVC.view)
    popOverVC.didMove(toParentViewController: self)
}

But I would change the way you are presenting the buttons. It would be better to use just a UICollectionView and then handle the tap of each Cell (using UICollectionViewDelegate).

Upvotes: 1

Related Questions