0

So I'm coding my first app, yeah, from scratch and I never done anything like it before so please bear with me.
I wanna take the randomised value of the first constant and use it to determine the content shown on screen through a label upon a view controller, this might be quite easy for some people but I am really struggling here. I commented out my code so you know what i intend it to do. Now, I know I could approach this many different ways such as not having a label at all and photoshop phrases on images but nah.... I wanna CODE!

Any ideas? Thank you all very much :3 <3

import UIKit

class ViewController: UIViewController {
    let random = Int(arc4random_uniform(11)) //Randomised int values from 0 to 11 (-1)
    @IBOutlet weak var text: UILabel!
    @IBOutlet weak var phrase: UIButton! //this button should reset the entire process over again
    @IBOutlet var imageauthor: UIImageView!
    override func viewDidLoad() {
            super.viewDidLoad()
        self .imageauthor.image = UIImage(named:"0\(random).jpg") //Viewcontroller displays a random image out of randomised value
        self .text.text = ("''") //this should somehow check what the randomised value is and call the Uilabel text bellow it
    }


    var string1 = ("My fist app has many holes1")
    ... string2 = ("My fist app has many holes2")
    ... string3.... 
Insane Skull
  • 9,220
  • 9
  • 44
  • 63
  • I'm not sure what you want. Do you want to append some text in your label according to the random value ? Ex: random value = 1, so label text = string1 ? – smdsgn Jan 31 '16 at 11:12
  • Yes! My first constant let random = Int(arc4random_uniform(11)) will randomise a number from 0-10(11-1=10) that will determine an image number from my image.assets and the next step is to display a given string in a given label that is related to the image. Got it? – Samuel Pantoja Jan 31 '16 at 13:06
  • Ok, so @appzYourLife's answer should do the job. – smdsgn Jan 31 '16 at 14:42
  • I'm trying to implement his method but still... You know... I don't really know what to do with it, I got clues but ya know... if this is what I need then imma keep trying and researching the syntax till I get it working, Thanks. – Samuel Pantoja Jan 31 '16 at 14:45
  • Have you managed to implement it ? – smdsgn Jan 31 '16 at 16:42
  • I didn't! Console keeps giving me error! I'll put more effort into it this week and return with the results ASAP. – Samuel Pantoja Feb 01 '16 at 04:39

2 Answers2

0
  1. You put your sentences into an array (let's call it sentences).
  2. You generate a random value between 0 and sentences.count (not included).
  3. You use the random integer to pick a value from the sentences array.

Like this:

let sentences = ["My fist app has many holes1", "My fist app has many holes2", "My fist app has many holes3"];
let random = Int(arc4random_uniform(UInt32(sentences.count)))
let randomSentence = sentences[random]
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
0

Concretely it should be something like this :

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var imageauthor: UIImageView!
    @IBOutlet weak var text: UILabel!
    // 1
    @IBAction func showRandom(sender: UIButton) {
        showRandom()
    }

    // 2
    let arrayOfStrings: [String] = ["String of image 0", "String of image 1", "String of image 2", "String of image 3", "String of image 4", "String of image 5", "String of image 6", "String of image 7", "String of image 8", "String of image 9"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //3
        showRandom()
    }

    //4
    func showRandom() {
        let random = Int(arc4random_uniform(UInt32(arrayOfStrings.count)))

        self.imageauthor.image = UIImage(named:"0\(random).jpg")
        self.text.text = (arrayOfStrings[random])
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


//1 Create a connection Action on your button : It's the same process as Outlet but select Connection : Action and Type : UIButton instead. And execute your random function on click. See documentation

//2 Declare a array with all your sentences inside. Respect the order.

//3 Execute your random function on viewDidLoad

//4 Create the function showRandom() who put the random image and the according text into your imageView and label.

smdsgn
  • 1,736
  • 1
  • 11
  • 11