1

I wanted to make a converter, This is how it works: On the initial screen, it asks from which unit you wanna convert, then when the user clicks on the button it sends the title of the button as string to the next view controller, then using the if statement and checking the the title string to the all units like inch, meter, yard, etc it identifies from which unit we are converting from, then on 'Editing did change' function in the text field it converts and gives the answer I have a problem when it recognises and sends the button title to the next view, what is happening is it is not carrying out the touch up inside function and directly sending the from string which doesn't carry the button title,

Here is the code

class lengthconversions: UIViewController {

var from = "From"

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "answer" {
        if let destinationVC = segue.destinationViewController as? lengthconversionsanswer{
            destinationVC.from = from

        }
    }
}

@IBAction func fromunit(sender: AnyObject) {
    let buttonTitle = (sender as? UIButton)?.titleLabel?.text
    from = buttonTitle!
    print(from)
}}

How can I do that the touch up inside function happens and then the data transfer through the segue.

p.s - The button to which the segue is hooked is the same to which the Touch up inside function is used

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dreamjar
  • 197
  • 2
  • 9
  • One option is to remove the segue from the button, but recreate it between the view controllers (see http://stackoverflow.com/questions/27650140/switching-views-programmatically-in-swift/27650207#27650207) and then have your `IBAction` do what it needs and only programmatically `performSegueWithIdentifier` when it wants to. – Rob May 31 '15 at 14:50
  • Thanks! I'll try this one! – Dreamjar May 31 '15 at 15:15

1 Answers1

0

The prepareForSegue function has an argument of sender. If you are performing the segue because of a button click, then the sender will be the button.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if segue.identifier == "answer" {
    if let destinationVC = segue.destinationViewController as? lengthconversionsanswer,
       let button = sender as? UIButton {
      destinationVC.from = button.titleLabel?.text!
    }
  }
}
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • It should solve it. Use of the `sender` appears to be generally unappreciated. For example, in a `UITableView`, a cell-initiated segue will have the `sender` be a `UITableViewCell` which can be used to populate the state of the `destinationViewController`. Please up-vote and mark as 'answered'! – GoZoner May 31 '15 at 15:35
  • Sorry but I can't vote up. I font have 15+ reputation. I really wanted to vote up. Sorry again. – Dreamjar May 31 '15 at 16:16
  • You can mark as 'answered' - click the 'check mark'! – GoZoner May 31 '15 at 16:17
  • ?? On the left, just below the down arrow, about near the words 'let button' in my answer. ?? It is an outline, click it and it fills in. ?? – GoZoner Jun 01 '15 at 02:41