I have 2 view controllers. View controller 1 has a textfield and 3 buttons that can be clicked. Filling out the textfield stores the user's name and depending on the button clicked stores the language you want to play the game in. There is a button at the bottom of the view controller 1 there is a button called start. The start takes you to view controller 2 for the home screen of the app. For the start button to be able to take you from view controller 1 to view controller 2 it requires that the language and the name variables are not nil.
In the startReady function, I have what I believe is how to make sure this happens but I am getting these errors. I realise the return statement should be returning something but I am unsure what needs to be returned
Non-void function should return a value
and
Missing return in instance method expected to return 'HomeScreen?'
Can someone please help me get this working? thanks
This is what the UI looks like for View Controller 1:
The following code is view controller 1:
import UIKit
class MainMenu: UIViewController {
var language = ""
var name = ""
var checkOne = false
var checkTwo = false
@IBOutlet weak var label: UILabel!
@IBOutlet weak var textfield: UITextField!
@IBOutlet weak var start: UIButton!
@IBAction func maoriButtion(_ sender: Any) {
language = "maori"
label.text = (language)
checkTwo = true
}
@IBAction func frenchButton(_ sender: Any) {
language = "french"
label.text = (language)
checkTwo = true
}
@IBAction func spanishButton(_ sender: Any) {
language = "spanish"
label.text = (language)
checkTwo = true
}
@IBAction func start(_ sender: Any) {
if let n = textfield.text{
name = n
}
}
@IBSegueAction func startReady(_ coder: NSCoder) -> HomeScreen? {
if !(language.isEmpty), !(name.isEmpty){
return
}else{
print("error")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
CodePudding user response:
As the return value implies you have to return a Homescreen
object for example
@IBSegueAction func startReady(_ coder: NSCoder) -> HomeScreen? {
if !language.isEmpty, !name.isEmpty {
return HomeScreen(coder: coder, language: language, name: name)
} else {
return nil
}
}
And basically you can reduce the IBActions of the 3 buttons to one
@IBAction func languageButton(_ sender: UIButton) {
language = sender.title(for: .normal).lowercased()
label.text = language
checkTwo = true
}