ios - 如何根据我们单击的按钮设置标题。发送数据

标签 ios swift uibutton

我一直在寻找我的答案,但一切似乎都有些困惑,所以我决定尝试提出以下问题:

我在第一个 View 中有三个按钮。

红色、蓝色和白色。

在第一个 View 中还有一个隐藏的标签,它会根据单击的按钮而变化(当单击带有文本“红色/蓝色/白色按钮已选择”的按钮时它会出现)。

此外,当触摸按钮时,会出现一个带有文本“下一个屏幕”的新按钮,以将转场推送到标题中带有标签的另一个 View 。

问题是:如何根据我们在第一个 VC 中点击的按钮来设置新 View 中的标题?

我的意思是,如果我们点击了红色按钮,标题将是:“我们点击了红色按钮”。等等

Screen mainVC

代码:

@IBOutlet weak var labelTipo: UILabel!
@IBOutlet weak var siguiente: UIButton!

var redSel = String()
var BlueSel = String()
var whiteSel = String()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    siguiente.hidden = true

    redSel = labelTipo.text!
    BlueSel = labelTipo.text!
    whiteSel = labelTipo.text!
}

@IBAction func redButton(sender: AnyObject) {
    siguiente.hidden = false

    labelTipo.text = "Has elegido un regalo de cumpleaños"
}

@IBAction func blueButton(sender: AnyObject) {
     siguiente.hidden = false

    labelTipo.text = "Has elegido un regalo de aniversario"
}

@IBAction func whiteButton(sender: AnyObject) {
     siguiente.hidden = false

    labelTipo.text = "Has elegido un regalo casual"
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "detailViewController") {
        print("Colour is")

        let vc = segue.destinationViewController as! detailViewController

        vc.blueSel = BlueSel
        vc.redSel = redSel
        vc.whiteSel = whiteSel
    }
}
}

class ViewController: UIViewController {

var redSel = String()
var blueSel = String()
var whiteSel = String()

@IBOutlet weak var titleLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    titleLabel.text = ""
}

最佳答案

很简单,请创建一个局部变量来分配单击按钮时的字符串。并将此变量传递给另一个 viewController,您将其推送到的位置。在您的情况下,将“labelTipo.text”的值分配给一个新变量并将其传递给另一个 viewController。

// Your SourceViewController

@IBOutlet weak var labelTipo: UILabel!
@IBOutlet weak var siguiente: UIButton!

    var passThisValue = String()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    siguiente.hidden = true

}

@IBAction func redButton(sender: AnyObject) {
    siguiente.hidden = false

    labelTipo.text = "Has elegido un regalo de cumpleaños"
    self.passThisValue = labelTipo.text 

}

@IBAction func blueButton(sender: AnyObject) {
     siguiente.hidden = false

    labelTipo.text = "Has elegido un regalo de aniversario"
    self.passThisValue = labelTipo.text 

}

@IBAction func whiteButton(sender: AnyObject) {
     siguiente.hidden = false

    labelTipo.text = "Has elegido un regalo casual"

    self.passThisValue = labelTipo.text 
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "AnotherViewController") {

        let vc = segue.destinationViewController as! AnotherViewController

        vc.valueFromSourceViewController = self.passThisValue
    }
}
}

// Your AnotherViewController 

class AnotherViewController: UIViewController {

var valueFromSourceViewController = String()

@IBOutlet weak var titleLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    titleLabel.text = self.valueFromSourceViewController
}

关于ios - 如何根据我们单击的按钮设置标题。发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32935054/

相关文章:

ios - UICollectionViewController 与流布局崩溃

iphone - Xcode iPhone - 要求多个文本输入然后返回它们的类

swift - Tableview 附件无法正确加载

ios - UIButton 数组 - 每个按钮都指向 CollectionView swift 中的标签 0

ios - 在 keyboardWillShow keyboardWillHide 中同步动画——同时使用硬件键盘和虚拟键盘

iphone - iOS - 动画效果 - 图片弹出

ios - 如何使用 swift 水平添加 tableview 将 UICollectionView 实现到 UITableviewCell 中?

ios - 在 Tableview Swift 中删除 Header View Section 的白色边框底部

ios - 在 UIViewController 中调用方法以定位错误

ios - 向 UIButton 添加其他 View