简体   繁体   中英

How Do I Make A Button Get An Alert View To Pop Up When Pressed And Go Back To The Main View Controller?

NOTE: Apologies in advance for my lack of being able to explain easily, as well as being only half a semester of coding knowledge, high school

So, recently I have had a project I have to do for a class, and it's one of those basic "Choose-Your-Own-Adventure" (CYOA) type of stories. However, as of now, I only have the navigation controller. One of the things I am having trouble with is getting the button at the end to pop up an alert view when pressed, so to say something like "Story finished! Go back to start!" and then of course the "Okay" button part on the alert view.

I know how to make an alert view, but only popping up because of a condition. We had a project called Tic-Tac-Toe, and when one wins, an alert view pops up. What I want to do with my CYOA app is make it so when you press the "Go Back To Start" button an alert view will pop up, as well as take you back to the initial view controller. The navigation controller will also be available in the app, so I do not want to take that out, however, having a "Go Back To Start" button at the end of each story line or path, will allow the user to not have to go back through the previous story line that they just came from, and rather save them a bit of taps.

Code for my project

There (pictured above) is the code for one of the ends. However, I know I have messed up in making this code work.

The goBackOnTapped is referring to the "Go Back To Start" button.

I am also willing to put up a copy of the file on an external site, of what I have so far if you would like to see otherwise into the code.

You can find the code here: Link to file via Canvas

Don't have enough rep to put a comment. I think what you need is this:

self.navigationController?.popToRootViewControllerAnimated(true);

Saves you the unwanted taps.

There's a method you can use that will instantiate the initial view controller.

let initialView = storyboard.instantiateInitialViewController() 
self.presentViewController(initialView, animated: true, completion: nil) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM