Reputation: 491
I have been looking for my answer but everything seems a little bit confusing, so I decided to try to ask the following question:
I have three buttons in a first view.
Red , blue, and white.
And a hidden Label in this first one view too, which changes depending of the button clicked (and it appears when a button is clicked with text "red/blue/white button selected").
Also, when a button is touched, a new button does appear with a text "next screen" to push a segue to another view with a label in the title.
Question is: how to set the title in the new view depending of which button we tapped in the first VC?
I mean, if we have clicked red the tittle would be: "We have clicked red button" . Etc
Code :
@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 = ""
}
Upvotes: 0
Views: 207
Reputation: 150635
You can do this a lot more succinctly with a little though. Here is a complete example which you can download from https://bitbucket.org/abizern/so-32935054/get/1ec75832723d.zip
If you set an enum for the types of selection you can configure the UI when setting the values.
This is the complete main view controller
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
}
}
In the segue you pass the string to the destination view controller:
import UIKit
class DetailViewController: UIViewController {
@IBOutlet var label: UILabel!
var displayText: String = ""
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
label.text = displayText
}
}
This is less code and a bit more elegant IMHO. The UI is configured by setting the selection, you don't have to manually set the button's hidden
variable in each function.
Upvotes: 0
Reputation: 822
It is very simple, Please create a local variable to assign the string when you click in the button. And pass this variable to another viewController, where you are pushing it to. In your case assign the value of "labelTipo.text" to a new variable and pass it to another 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
}
Upvotes: 2
Reputation: 2069
Try to make some adds in your prepareForSegue method but make labelTipo variable as property of your class:
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
}
}
So actually you saves in local variable "newLocalVariable" of your "vc" new value which is equals to labelTipo.text for example or whatever you want.
Upvotes: 0