我一直在寻找我的答案,但一切似乎都有点令人困惑,所以我决定尝试提出以下问题:
我在第一个视图中有三个按钮。
红色,蓝色和白色。
在第一个视图中也有一个隐藏的标签,它会根据点击的按钮而变化(单击按钮时会显示“选择红/蓝/白色按钮”)。
此外,当触摸一个按钮时,会出现一个带有文本“下一个屏幕”的新按钮,将一个segue推送到标题中带有标签的另一个视图。
问题是:如何根据我们在第一个VC中点击的按钮在新视图中设置标题?
我的意思是,如果我们点击了红色,那就是:“我们点击了红色按钮”。等
代码:
@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 = ""
}
答案 0 :(得分:2)
这很简单,请在单击按钮时创建一个局部变量来分配字符串。并将此变量传递给另一个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
}
答案 1 :(得分:0)
尝试在prepareForSegue方法中添加一些内容,但将labelTipo变量作为类的属性:
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
//make some newLocalVariable property in vc!
vc.newLocalVariable = self.labelTipo.text
}
}
所以实际上你保存了你的“vc”新值的局部变量“newLocalVariable”,它等于labelTipo.text或者你想要的任何东西。
答案 2 :(得分:0)
尽管如此,你可以更简洁地做到这一点。这是一个完整的示例,您可以从https://bitbucket.org/abizern/so-32935054/get/1ec75832723d.zip
下载如果为选择类型设置枚举,则可以在设置值时配置UI。
这是完整的主视图控制器
import UIKit
class ViewController: UIViewController {
enum Selection: String {
case NoSelection = ""
case RedButton = "Has elegido un regalo de cumpleaños"
case BlueButton = "Has elegido un regalo de aniversario"
case WhiteButton = "Has elegido un regalo casual"
}
@IBOutlet var colorLabel: UILabel!
@IBOutlet var nextButton: UIButton!
var selection: Selection = .NoSelection {
didSet {
colorLabel.text = selection.rawValue
if selection == .NoSelection {
nextButton.hidden = true
} else {
nextButton.hidden = false
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
colorLabel.text = selection.rawValue
nextButton.hidden = true
}
@IBAction func onRed(sender: UIButton) {
selection = .RedButton
}
@IBAction func onBlue(sender: UIButton) {
selection = .BlueButton
}
@IBAction func onWhite(sender: UIButton) {
selection = .WhiteButton
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard
segue.identifier == "ShowDetail",
let destinationVC = segue.destinationViewController as? DetailViewController
else { return }
destinationVC.displayText = selection.rawValue
}
}
在segue中,您将字符串传递给目标视图控制器:
import UIKit
class DetailViewController: UIViewController {
@IBOutlet var label: UILabel!
var displayText: String = ""
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
label.text = displayText
}
}
这是更少的代码和更优雅的恕我直言。通过设置选择来配置UI,您不必在每个功能中手动设置按钮的hidden
变量。