Kutay Demireren
Kutay Demireren

Reputation: 650

How to Give Button Exit Functionality in iOS

I am designing two pages, one followed by the other, in Xcode 7. Let's call them the first and second view controllers. From a button in the first view controller, with a modal segue, the second view controller shows up. I want to add a button to second view controller so that when I hit that button, I can simply exit from the second and back to the first view controller.

I know this feature can be done by navigation controller with putting cancel button to the navigation bar on second view controller which I don't want to. I want specifically a button to have that functionality.

I thought of calling the first view controller with code when the button is tapped, but it sounds me like a bad coding. So, I would like to learn what is a good way of achieving this.

Thanks,

Upvotes: 0

Views: 583

Answers (2)

Greg
Greg

Reputation: 25459

Add method to the second view controller:

@IBAction func exitButtonPressed(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil);
}

Next add button to the second view controller in interface builder and connect button's action to this method.

Upvotes: 3

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

use Unwind Segues

Unwind Segues give you a way to “unwind” the navigation stack and specify a destination to go back to.

for sample tutorial1, tutorial2

Upvotes: 2

Related Questions