5
votes

I have a general question concerning the navigation between views.

I have a view controller 1 embeded in a navigation controller. In the nav bar, I have a button to do add data, when pushed, it goes to view controller 2 through segue Show (in Storyboard). In view controller 2, I return to view controller 1 after collecting data when I click the button save in nav bar on the right. I collect data with:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {...}

I would prefer instead of a back button to have a cancel button in the view controller 2 to return to view controller 1.

I feel something is wrong in my design but I dont know what.

In fact I am looking for something very similar to app clock.

Do you have any idea?

Edit: Here is the story board. you will notice the loop beween the two controller. how to do when I click save to not have a back button automatically on the first controller?

Storyboard

2

2 Answers

9
votes

You just have to select your UIBarButtonItem, click at show Attributes Inspector, click at Identifier and select "Cancel" from the drop-down menu.

enter image description here

enter image description here

@IBAction func cancelDownload(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil)

}
3
votes

You can customize the text of the back button by adding this code to the view controller that contains it (in viewDidLoad):

let backItem = UIBarButtonItem(title: "Cancel", style: .Bordered, target: nil, action: nil)
navigationItem.backBarButtonItem = backItem