Hi All,
Im new here ( and a newbie, no previous coding experience) and have just completed mod 3 Match App.
My app builds and runs fine.
I would like to add a reset function to the alert message to enable the user to run the program again. Ive modified the alert message and handler to run a function.
In this function I can reset the timer and get it to re run, but cannot re load the cards images ( set the alpha back to 1 and re-load a new random array).
Any help would be greatly appreciated.
Cheers,
Gav
Hi Gav,
Welcome to the community!
Are you calling all the following in your reset method:
model = CardModel()
cardAarry = [Card]()
cardAarry = model.getCards()
collectionView.reloadData()
Blessings,
—Mark
Hi Mark !
Thanks so much for the welcome and the code. I had it all other than the .reloadData, I was trying to work with the collection view but didn’t know about this . option ! It’s a great lesson for me to take away to try .notation and see what options are available.
Appreciate your time and solution.
Cheers
Gav
Hi
I am new to this course, and wanted to add the possibility to restart the game. I found this thread but it didn’t quite work as I expected, since I couldn’t pass a function to the handler in the UIAlertAction.
I tried google and combined what I found with what was written here, my “solution” was:
I added a second button in the alert message, one that simply makes it disappear as in this tutorial, and a second which would provide the option of restarting:
let newGameAction = UIAlertAction(title: “Try again?”, style: .default, handler: resetGame(alert:))
alert.addAction(newGameAction)
I also added this function, which I use in the handler:
func resetGame(alert: UIAlertAction!) {
milliseconds = 60*1000
model = CardModel()
cardsArray = [Card]()
cardsArray = model.getCards()
collectionView.reloadData()
timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(timerFired), userInfo: nil, repeats: true)
timerLabel.textColor = UIColor.black
RunLoop.main.add(timer!, forMode: .common)
}
The handler didn’t work without the UIAlertAction! argument/parameter, but in the doesn’t it appears, that it is only the name tag, that is passed in the handler, why is that?
I seems to work, but is this an acceptable way of doing it, or is there an easier way
Regards
Thomas
Hi Thomas!
Welcome to the community!
You can call the restart code in a trailing enclosure, something like this:
let myAlert:UIAlertController = UIAlertController(title: "Attention", message: "Would you like to play again?", preferredStyle: UIAlertController.Style.alert)
//create an action alert, this is the button the user will tap
let firstAction:UIAlertAction = UIAlertAction(title: "Restart Game", style: UIAlertAction.Style.destructive) { (myAlert) in
//Here you place all your code to restart. Don't forget to add self. since it is in an enclosure
}
//Add the alert action
myAlert.addAction(firstAction)
//Present the alert
present(myAlert, animated: true, completion: nil)
If you are also creating for the iPad, for need to add the following before presenting:
//new for iPad alerts
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
Blessings,
—Mark
Hi Thomas !
I got my reset button to work using the one button after Mark’s help as per his thread. I’m new to this I’ll try adding screen shots. if it doesn’t work let me know.
When you set up the alert as per Chris’s video when you get to the handler parameter as per this
If you select return, and type " action " in the UIAlert Action
Then you can put your reset function in the code tab
I hope those screen shots worked.
Cheers
Gav
Hi Gav
Screenshots appear fine thanks
/Thomas
Hi Mark
Thanks, it works.
I added an extra button to your code (the original solution), providing the user two options: restart or quit, which was what I wanted
/Thomas