-1

I'm trying to make app that takes info from the two text fields and randomly selects one of the sentences and places it in a label on another view controller. I'm a student in the Mobile Apps 1 class so I'm new to this. If you could explain it as much as possible it will be greatly appreciated. Happy new year!

My code:

class twoIdeasViewController: UIViewController {

    @IBOutlet weak var twoIdeaContinueButton: UIButton!
    @IBOutlet weak var twoIdea2TextField: UITextField!
    @IBOutlet weak var twoIdea1TextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Enter Ideas"
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        let twoIdea1:String = twoIdea1TextField.text!
        let twoIdea2:String = twoIdea2TextField.text!
        return true
    }

    func prepareForSegue(segue: UIStoryboardSegue, Object: AnyObject?){
        let twoIdeaFinal = segue.destinationViewController as! twoFinalViewController
        twoIdeaFinal.twoIdea = //the variable that will contain the randomizer    
    }
}
Louis Tur
  • 1,303
  • 10
  • 16
D Doe
  • 33
  • 5
  • Explain what you have tried and what is not working. Also, check the "Related" section to the right, for questions that are similar to yours. – fishinear Jan 08 '16 at 15:55

2 Answers2

1

Make use of arc4random_uniform() to generate a random number that controls which of the two text fields you wish to extract and send text from. Also, you seem to need to fix up your prepateForSegue method: you need to match the segue identifier with the identifier of your 2nd view controller (set in attributes inspector while selecting this other view controller in your storyboard).

@IBOutlet weak var twoIdea2TextField: UITextField!
@IBOutlet weak var twoIdea1TextField: UITextField!

// ...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    /* Get the new view controller using segue.destinationViewController.
       Pass the randomly chosen text view text to the UILabel of the
       new view controller. */

    /* Here: you need to match with the identifier of your
       VC 'twoFinalViewController' (set in attributes inspector) */
    if segue.identifier == "twoFinalVC" {
        let viewController = segue.destinationViewController as! ViewController

        let random = arc4random_uniform(2)
        viewController.twoFinalLabel.text = (random == 0) ? (twoIdea1TextField.text ?? "") : (twoIdea2TextField.text ?? "")
    }
}

For a detailed description covering segue communication between two view controllers (UITableViewController and UIViewController), see the following thread

Community
  • 1
  • 1
dfrib
  • 70,367
  • 12
  • 127
  • 192
  • text doesn't pop up in my label when I run it. It's something with my second viewcontroller probably : class twoFinalViewController: UIViewController { var twoIdea = String() @IBOutlet weak var twoFinalLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() twoFinalLabel.text = "\(twoIdea)" } } – D Doe Jan 08 '16 at 16:13
  • @DDoe Please add this additional code into your question, not these comments. – dfrib Jan 08 '16 at 16:14
  • @DDoe see if the updated answer fixes it for you. Note that you need to match the `segue.identifier` with the corresponding identifier you've entered in the Attributes inspector for the target view controller. – dfrib Jan 08 '16 at 16:23
0

You can use something like that

func getRandomString() -> String
{
    let randomNumber = arc4random_uniform(2) + 1

    switch randomNumber
    {
        case 1:
            return twoIdea1TextField.text!
        case 2:
            return twoIdea2TextField.text!
        default:
            return ""
    }
}

I have no time, but I think that with an enum is simpler than what I did.

Luca D'Alberti
  • 4,749
  • 3
  • 25
  • 45
  • How would i send this through the segue? Do I send the function getRandomString through so it would be: twoIdeaFinal.twoIdea = getRandomString – D Doe Jan 08 '16 at 15:54
  • It should be 'twoIdeaFinal.twoIdea = self.getRandomString()' – Luca D'Alberti Jan 08 '16 at 15:55
  • The code works, but it the information doesn't appear in the label. The label is just blank, Is there something wrong with the second viewcontroller code? : class twoFinalViewController: UIViewController { @IBOutlet weak var twoFinalLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() twoFinalLabel.text = "\(twoRandom)" } var twoRandom = String() } – D Doe Jan 08 '16 at 16:00
  • What's wrong is the way you are setting the label value. If you debug your code, into viewDidLoad you haven't yet the randomString... – Luca D'Alberti Jan 08 '16 at 16:01
  • What do I have to do to the label i set. @LucaD – D Doe Jan 08 '16 at 16:05
  • @dfri the text doesn't show still. The label just disappears when I run the app. – D Doe Jan 08 '16 at 16:10