As a part of an iOS course I'm taking through Udacity I'm creating an app to record and play audio. This article is part of an assignment for that course.
This guide explains how to create a modal that confirms a specific user action within an app using Swift 2.0 and Xcode 7.
For the app I'm building, the modal will request that the user confirm the start of audio recording.
- Add a new Cocoa Touch Class file of type "UIViewController."
- Drag a new View Controller onto the storyboard, set its class to match file you just created.
- Choose a dark background color for the initial View within this new View Controller and lower its opacity.
- Drag a new View onto your new View Controller in the storyboard, this is the sub View that will hold the content of the modal that appears.
- Provide constraints to center align the sub View vertically and horizontally.
- Add two buttons, name them "Start" and "Cancel" and give them constraints.
- Run code in the View Controller after clicking a button in the modal using delegation
Add a new Cocoa Touch Class file of type "UIViewController."
Drag a new View Controller onto the storyboard and set its class to match the file you just created.
Choose a dark background color and lower its opacity.
Drag a new View onto your new View Controller in the storyboard to add a sub View.
Provide constraints to center align the sub View vertically and horizontally.
Note: If you've added constraints but they're red, make sure you've added both inner constraints for width and height and outer constraints for positioning vertically and horizontally. The constraints will turn blue once you've satisfied the minimum number of requirements for them to act.
Add two buttons, name them "Start" and "Cancel" and give them constraints. Also add a label if you would like the modal to display a message.
When you're done your modal should look something like this.
Create a segue from the original View Controller to the new modal View Controller you've created
At this point you can build your app and the modal will appear when the button you just connected to it with a segue is pressed.
So now lets have the modal buttons dismiss the modal and then run code on the original view that called it. I accomplished this using delegation
First let's write the code we want to execute. Since my app is for recording audio and the modal is a confirmation with two options, I placed my code in two functions called "startRecording" and "cancelRecording." I placed this code on the original View Controller.
Write the code that will execute when a button on the modal is pressed.
Add a prepareForSegue override
(I recommend imagining GOB Bluth saying 'prepare for segue')
Override the built in prepareForSegue function and pass the current View Controller object that contains the functions you just wrote, to the destination View Controller which is your modal's view controller. Note: I'm describing how I currently understand this to work, but I'm new to Swift, if you know better please correct me.
Create the delegate
First Green Arrow: In the modal's View Controller set up the delegation using the "protocol" keyword outside of the class declaration. In my case the methods it will run are the "cancelRecording" and "startRecording" methods, so those need to be included in the protocol for the delegate.
Second Green Arrow: Within the class define a variable with the same type as the protocol. In my case I called it "delegate."
Third and Fourth Green Arrow: Use Button actions to fire the built in "dismissViewControllerAnimated" and in the body of that run the code you want to execute on the View Controller that called the modal.
For example, in my case the "Start" button action runs the dismissViewController and which then runs the "self.delegate?.startRecording()" to run the "startRecording."
Add the delegate type to the modal calling View Controller
Finally, back on the View Controller that calls the modal's View Controller, add the delegate type to its class. In my case it's called "modalConfirmRecordDelegate"
And that should do it. You now have a modal and its buttons perform actions on code from a different view controller. I hope this was helpful!
The resources I found most helpful while writing this tutorial
If you need more details on a specific item, especially the delegate aspects of this article, these resources are a great place to start.
DJ Spiess / Deejeu video about transferring data between views in iOS 9