ios - 从不同 ViewController 中的函数捕获 var | swift

标签 ios swift xcode uibutton

我正在制作一个占星应用程序,在主视图 Controller 中,您可以单击一系列按钮来拉出一个显示其占星的弹出屏幕。

我有一个主要功能 buttonLayout() 为我的 ViewController 制作所有按钮并设置它们的图像。

单击按钮后,我希望它弹出一个弹出窗口,其中包含他们在主视图 Controller 中单击的相同按钮

我大写的部分是我不知道该怎么做。

main ViewController

clicking a button

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

这是我的 ViewController 代码: // View Controller .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)
}

这是我的弹出 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()
        }
    });
}

在 showButton 函数中,我想访问索引以便我可以说 sign[index] 或来自 buttonLayout() 函数的 UIButton 图像。

最佳答案

您只需将按钮的值注入(inject)新的 View Controller 即可。 (这是您分享的代码中的代码)

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)
}

但我会改变您呈现按钮的方式。 最好只使用 UICollectionView,然后处理每个 Cell 的点击(使用 UICollectionViewDelegate)。

关于ios - 从不同 ViewController 中的函数捕获 var | swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46077976/

相关文章:

objective-c - 将文本字段的值放入另一个字符串

ios - 为 iOS/Xcode 的 UITextView 手动格式化字符串中以 http 或 https 开头的 url

ios - 如何在 Swift 中只显示 UITextField 的底部边框

iPhone 配置实用程序 - 显示配置文件中的所有设备

objective-c - 无法使用 NSURLConnection 使 iOS http post 正常工作

ios - 新闻详情 View Controller 表单

c# - 跨平台 Xamarin 表单 : How to create PDF file in local storage

arrays - 声明一个字典数组,其中每个键的值是另一个数组 (Swift)

iphone - 将NSData转换为字节数组

xcode - SpriteKit 可以使用什么类型的游戏 map ?