Cancel button instead of a back button in Navigation Controller - ios

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?

You just have to select your UIBarButtonItem, click at show Attributes Inspector, click at Identifier and select "Cancel" from the drop-down menu.
#IBAction func cancelDownload(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}

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

Related

How to show the tab bar when using Segue back in swift

I have a Tab bar in the first view and a total of two views.
However, when I move from the first view to the second view and then back to the first view using the Segue, the Tab bar of the first view disappears.
When return to the first view from the second view, what is the way the Tab bar does not disappear?(without using unwind)
Don't use unwind segue here. When you need to get back to previous ViewController, just dismiss your current ViewController
dismiss(animated: true, completion: nil) /* call this in second VC */
I suggest you read the documentation for understanding how combined View Controller Interfaces. Anyway, if you need to pop to previous view controller on the flow, you need to use
navController.popViewController(animated: true)
But, if the need to pop up on the specific ViewController on the queue of View Controllers in the NavigationViewController, you need to use
navController.popToViewController(ViewController, animated: true)
From the moment you are using a NavigationController, a return button will automatically appear on the UINavigationBar, so you do not have to worry about that. Unless you want to customize the back buttons in the viewcontrollers queue, in this case use the above methods.
It's for page 1. Display tabBarController once page is loaded.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = true
}
If click event is fired, hide tabBarController.
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "twoSegue") {
self.tabBarController?.tabBar.hidden = false
}
}

Back Button with Multiple Sources

Within my app, I want to create a user profile controller, which can be navigated to from several different controllers. I would like it to have a back button that will take the user back to whichever controller they came from.
How do I do this?
Dismiss
Add UIButton for back navigation somewhere to your UserProfileController. Then in UserProfileController create action and set it as action of your button. This action dismisses your UserProfileController (so you get back to previous UIViewController)
#IBAction func backButtonPressed(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
UINavigationController
Alternatively you can have previous ViewControllers embed in UINavigationController. Then you will be able to tap to back button (which is set by default) in UINavigationBar which gets you to previous UIViewController

Back button in Navigation bar not displayed

I have a view controller connected to a popover controller, which directs to a new view controller. I want to add a back button on the navigation item to the last view controller so that when I click back it will return to the first view controller
I tried creating segue and click action in the popover controller and add back button in the segue function/click action function
neither of them works
This is what I do in the Popover. I already present the popover with UIPopoverPresentationControllerDelegate and this is what PopoverVC looks like. In this way, I add IBAction to enable the click action of the label in popover. I write the backbutton in the IBAction function and create the navigationcontroller with rootview, and the back button doesn't appear.
class PopoverViewController: UIViewController {
//TODO: add BACK button
#IBAction func createNewChat(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle:nil)
let contactVC = storyboard.instantiateViewController(withIdentifier: "contacts") as? NewContactsViewController
let nc2 = UINavigationController()
let back = UIBarButtonItem()
back.title = "Back"
nc2.navigationItem.backBarButtonItem = back
nc2.pushViewController(contactVC!, animated: true)
self.present(nc2, animated: true, completion: nil)
}
}
I also tried another way, I create a segue in storyboard and connect the popover to the navigation controller, the backbutton still doesn't appear.The segue is hooked up to the navigation controller in the storyboard.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let seg = segue.destination as! ContactsTableViewController
let back = UIBarButtonItem()
back.title = "Back"
navigationItem.backBarButtonItem = back
}
My question is: how to make the back button appear? After it appears, how to make it work? I don't want the back button points to the popover, I want the back button will make users return to the VC that creates the popoverVC.
The back button in a navigation bar doesn't belong to THIS view controller; it is the back button item of the previous view controller already on the navigation stack. But you have no previous view controller. So just use, like, a left bar button item that says Back, or something.
Thus, give the bar button item (back) an action and target and change
nc2.navigationItem.backBarButtonItem = back
to
nc2.navigationItem.leftBarButtonItem = back

Add another back button to a Navigation Controller

I have a navigation controller so when I click on a button on my main view, it opens another view with the "Back" navigation item.
What I'm trying to do is to add another item on my second view on the right, so this view should have a "Back" item, already working, and a new "Edit" item which does the same thing as Back button but I can get the prepareForSegue function to run some code in complement.
How can I achieve to do that ?
Here is what I tried :
I'm using two navigation controllers because I couldn't add an "Edit" item if my second view wasn't embed in a navigation controller.
So this is working, I have my two "Back" and "Edit" items, but I don't know how to manage to get the same behavior as the "Back" item on my "Edit" item, I tried segues but it doesn't work as expected.
Note that the button I talked about on my first view isn't shown in the editor nor its segue, I did it programmatically.
You can simple call popViewController(animated:) when the edit button is tapped.
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
let editItem = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(editButtonTapped(_:)))
self.navigationItem.rightBarButtonItem = editItem
}
#objc func editButtonTapped(_ sender: UIBarButtonItem)
{
self.navigationController?.popViewController(animated: true)
}
}
Storyboard:

Change back button title when pushing a UIViewController on UITableViewController

View hierarchy:
UINavigationController
->UITableViewController(1)
->UITableViewController(2)
->UIViewController(3)
In 1 and 2 I have this code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
self.navigationItem.backBarButtonItem?.title = ""
}
It should override the back button title of the next view controller pushed on the current view controller. It works from 1 -> 2. But it does not work for 2 -> 3. In 3 the back button title has a title, the name of the previous UITableViewController.
Any ideas whats wrong? I am using swift, xcode6.1 and iOS8.1
You could init a new back button with no title. Just put this in the viewDidLoad() of each view controller.
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
--
I am going to extend this answer with my experience. So I managed to remove the title of the back button to display just the back arrow. In storyboard you have to select the navigation item that displays the title inside the navigation bar of the previous view controller. There is a property called Back Button. Just enter a space and save. It will remove the back button title.
Update
UIBarButtonItemStyle.Bordered was deprecated in iOS 8.0. Use UIBarButtonItemStyle.Plain instead.
thanks,
if you want using customer back button image, you can use this
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: “backImage”)
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: “ backImage”)
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: “”, style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

Resources