-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2_iRecommend_sol.swift
82 lines (65 loc) · 3.27 KB
/
day2_iRecommend_sol.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import UIKit
// Solution 1: Dictionaries
class ViewController: UIViewController {
// outlets are references to UI components, treat these as you would any object
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var backgroundImage: UIImageView!
// actions are functions called after user interaction occurs
// button clicked
@IBAction func generateButtonClicked(_ sender: Any) {
setUpScreen()
}
func setUpScreen() {
// dictionaries have keys and values; "look up" a key to find its value
let imagelib: [String: String] = ["Yosemite": "yosemite.jpg", "Banff": "banff.jpg", "Arches": "arches.jpg"]
// calls generateRandomPark() function, which returns a string of the park name
let randomPark = generateRandomPark()
// changing the text of a label in your Storyboard
titleLabel.text = randomPark
// changing the image of the UIImage view - have to create a UIImage object with the file name passed in as a parameter
backgroundImage.image = UIImage(named: imagelib[randomPark]!)
}
// generates a random park; returns a String value
func generateRandomPark() -> String {
let ntlParks: [String] = ["Yosemite", "Banff", "Arches"]
// ! necessary to indicate that we know the array is not empty
return ntlParks.randomElement()!
}
// this function is called when the view is loading
// do all your initial screen set up here
override func viewDidLoad() {
super.viewDidLoad()
setUpScreen()
}
}
// Solution 2: Arrays
class ViewController2: UIViewController {
// outlets are references to UI components, treat these as you would any object
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var backgroundImage: UIImageView!
// actions are functions called after user interaction occurs
// button clicked
@IBAction func generateButtonClicked(_ sender: Any) {
setUpScreen()
}
func setUpScreen() {
// ensure that the items correspond so that you can match up the image with the correct item
// don't want the two to be separately random, e.g. Yosemite label and Arches image
let ntlParks: [String] = ["Yosemite", "Banff", "Arches"]
let imageArray: [String] = ["yosemite.jpg", "banff.jpg", "arches.jpg"]
// get a random index (this is where the randomness will come from)
// swift has this function:
let randomNumber = Int.random(in: 0 ..< ntlParks.count)
// because the following two use the same index (randomNumber doesn't change values in between this line of code and the next), the image and text will correspond so long as your arrays are written correctly
// changing the text of a label in your Storyboard
titleLabel.text = ntlParks[randomNumber]
// changing the image of the UIImage view - have to create a UIImage object with the file name passed in as a parameter
backgroundImage.image = UIImage(named: imageArray[randomNumber])
}
// this function is called when the view is loading
// do all your initial screen set up here
override func viewDidLoad() {
super.viewDidLoad()
setUpScreen()
}
}