skip to Main Content

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.... 

2

Answers


    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]
    
    Login or Signup to reply.
  1. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search