New to swift - slowly but surely starting to fully understand this.
How would I go about calling a structured array of URLs weeks.urls[0]
in an @IBAction? I have attached a screenshot of the error, and my code for WeekDetalViewController.
class WeekDetailViewController: UIViewController {
var week: WeekInfo!
@IBOutlet weak var lessonCompleteBtn: UIButton!
@IBOutlet weak var Button1: UIButton!
@IBOutlet weak var Button2: UIButton!
@IBOutlet weak var Button3: UIButton!
@IBOutlet weak var Button4: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
lessonCompleteBtn.backgroundColor = UIColor.gray
lessonCompleteBtn.layer.cornerRadius = 20
}
@IBAction func lessonBtnTapped(_ sender: AnyObject) {
sender.setTitle("PSET Complete!", for: [])
if lessonCompleteBtn.backgroundColor == UIColor.gray{
lessonCompleteBtn.backgroundColor = UIColor(red: 17/255, green: 107/255, blue: 0/255, alpha: 1.0)
}
else if lessonCompleteBtn.backgroundColor == UIColor(red: 17/255, green: 107/255, blue: 0/255, alpha: 1.0){
sender.setTitle("Complete PSET!", for: [])
lessonCompleteBtn.backgroundColor = UIColor.gray
}
}
@IBAction func Button1(_ sender: Any) {
UIApplication.shared.open(URL(week.urls[0])!)
}
@IBAction func Button2(_ sender: Any) {
UIApplication.shared.open(URL(week.urls[1])!)
}
@IBAction func Button3(_ sender: Any) {
UIApplication.shared.open(URL(week.urls[2])!)
}
@IBAction func Button4(_ sender: Any) {
UIApplication.shared.open(URL(week.urls[3])!)
}
}```
Here you go ... you already have URL in your Array ... so no need to create them again ... And also they are not optionals ... so you don't need to use force unwrapping
@IBAction func Button1(_ sender: Any) {
UIApplication.shared.open(week.urls[0])
}
@IBAction func Button2(_ sender: Any) {
UIApplication.shared.open((week.urls[1])
}
@IBAction func Button3(_ sender: Any) {
UIApplication.shared.open(week.urls[2])
}
@IBAction func Button4(_ sender: Any) {
UIApplication.shared.open(week.urls[3])
}
Better to use one function Instead of 4 ... just set buttons tag
@IBAction func ButtonTapped(_ sender: UIButton) {
UIApplication.shared.open(week.urls[sender.tag])
}