0

I want to return random text from one of two Labels. I can only find answers for returning random numbers. See image below for what I'm trying to do.enter image description here

Any ideas? Thanks!

How my text works:

// FIRST VIEW

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "segueTest") {                                      
            var svc = segue.destinationViewController as! ResultViewController;     

            svc.userEntered = askUser.text                                 
            svc.textOne = userTextOne.text                                          
            svc.textTwo = userTextTwo.text                                   

        }
    }

// SECOND VIEW

var userEntered:String!                                        
var textOne:String!                                        
var textTwo:String!



    var theResult: [String!] {

        return [textOne.text, textTwo.text]

    }

override func viewDidLoad() {
        super.viewDidLoad()

        myUser.text = userEntered                         
        elementOne.text = textOne                           
        elementTwo.text = textOne
    }
George
  • 322
  • 1
  • 6
  • 25
  • Still not really clear. Is there some reason you can't use the random number generator and then an `if` or `switch` to select the appropriate text based on the random number? – JenB Aug 27 '15 at 13:20
  • @zaph Random 1 and Random 2 is equal to whatever the user typed in on the first view controller so I want to return one randomly. – George Aug 27 '15 at 13:26

2 Answers2

1

Make array with label titles. Then make random number generator and return title that is positioned in array on random number spot.

Kristijan Delivuk
  • 1,253
  • 13
  • 24
  • I can make an array but not sure what to do next. I get an error using arc4random (Instance cannot be used on viewcontroller). `@IBOutlet weak var factorOneText: UILabel! @IBOutlet weak var factorTwoText: UILabel! @IBOutlet weak var resultText: UILabel! var randomText: [String!] { return [factorOneText.text, factorTwoText.text] } let index = arc4random_uniform(randomText.count)` – George Aug 27 '15 at 14:19
  • here u have great random article: http://stackoverflow.com/questions/24132399/how-does-one-make-random-number-between-range-for-arc4random-uniform (3rd post) when u extend int (or other struct) with random method use it like: var randomNumberWithRange = Int.random(0...array.count) and then u return random label like arrayOfLabelTitles[randomNumberWithRange] – Kristijan Delivuk Aug 27 '15 at 14:27
0

First you want to have a reference to everything the user typed on the first view controller. I'm assuming there will be numerous Strings.

So, lets say you have an Array of all the things the user typed in on VC 1.

var wordsOrSomethingArray = ["This","that","who","what","why??","ok"]

Now you'll use the handy extension below (or without it to generate a random number).

let randomOne = wordsOrSomethingArray[Int.randomNumber(0, wordsOrSomethingArray.count-1)]
let randomTwo = wordsOrSomethingArray[Int.randomNumber(0, wordsOrSomethingArray.count-1)]

Here is the handy extension I like to use for a random Int. called by Int.randomNumber(....) Make sure you put it outside your class.

extension Int {
    static func randomNumber(min:Int,_ max:Int)->Int {
        return min + Int(arc4random_uniform(UInt32(max - min + 1)))
    }
}
Bseaborn
  • 4,347
  • 2
  • 14
  • 9
  • The user can type anything they want and I already have it set up to present that on the SecondViewController (will add to OP), so I don't have a set list of words. With the code you posted I get error type Int has no member randomNumber and extension Int only valid in file scope. – George Aug 27 '15 at 15:56
  • @George Did you make sure you pasted the extension outside the class? – Bseaborn Aug 27 '15 at 16:11
  • Ok that Int code works now but I get an error on the randomOne/Two saying it can't be used on ViewController due to: `var theResult: [String!] { return [textOne.text, textTwo.text] }` – George Aug 27 '15 at 16:32
  • @George What does the error say? And I'm confused at what the ultimate goal is. Could you explain exactly what you are trying to accomplish? – Bseaborn Aug 27 '15 at 16:44
  • See the image above, Random 1/2 is whatever the user enters on the first view. Instance member theResult cannot be used on type ResultViewController. – George Aug 27 '15 at 17:18