Navigation controller not displaying back button - ios

I have a profile that is embedded in a navigation controller. When I create a segue that goes to another view controller (also embedded in a navigation controller), I get the screen below. I used to seeing the back button that, in this case, would say "< Profile" but all I see is a blank nav bar with no back button, SOS!
Can anybody help me fix this issue? I can't seem to find any help!
This is the code I have written for the segue to go from the profile view controller to the followers view controller, which is the one with the blank nav bar.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toFollowers" {
var profileVC: FollowersViewController = segue.destinationViewController as! FollowersViewController
profileVC.followers = true
}
}

Are you setting hidesBackButton = YES or backBarButtonItem = nil in
profileVC, or does it have a different leftBarButtonItem
defined?
Have you remove back via programmatically please remove those code.
EDITED
Following code returns how many controllers into UINavigationController please check it out it's greater then 0 or not.
var stack = self.navigationController!.viewControllers as Array
May this helps lot.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toFollowers" {
//Try getting the navigation controller first as
var navVc = segue.destination as UINavigationViewContrller
//than
var profileVc = navVc.viewControllers.first as! FollowersViewController
// var profileVC: FollowersViewController = //segue.destinationViewController as! FollowersViewController
// profileVC.followers = true
}
}

Related

Unable to pass data to View Controller embedded in nav bar

I am trying to do something this:
I want to pass some data to ApplicationsViewController which is embedded in nav bar, therefore I get the error: Could not cast value of type 'UINavigationController'
I am not able to understand how to implement the same. Help is much appreciated. Thank you.
The reason is you can't pass data directly to ApplicationViewController as InboxViewController segue's destination is UINavigationController.
So, first you need to access UINavigationController and after that ApplicationsViewController from the stack.
Try the code below:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToApplications" {
if let navigation = segue.destination as? UINavigationController, let applicationVC = navigation.topViewController as? ApplicationsViewController {
applicationVC.id = "RGB"
}
}
}
when pass data application view controller which is rootviewController of UINavigationController. you should give segue name of navigation controler and type cast as UINavigationController as destination viewcontroller.
Fetch rootviewController which is application View controller and assign value of id
Here is code how to assign value to Application ViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "navigationSegue" { // segue of navigationVC
let navVc = segue.destination as! UINavigationController
let appVc = navVc.viewControllers.first as! ApplicationsViewController
appvc.id = "RGB"
}
}

iOS - Navigation bar has no back button

I am currently using a walkthrough view controller to show a "getting started" carousel when you first open my app. Once you select get started, there are two buttons at the bottom that direct you to login as one of two types of users:
However, As you can see here there is no back button to take you back to the page where you decide what type of user you are. Here is the code I am using to segue:
override open func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let NavViewController = segue.destination as! UINavigationController
let NextViewController = NavViewController.topViewController as! LoginViewController
if(segue.identifier == "client") {
NextViewController.request = 0;
} else {
NextViewController.request = 1
}
}
In addition, I have added titles to all of the other view controllers in their viewDidLoad() methods. If anyone has an idea of why the back button isn't showing, I would greatly appreciate your help!
Question has been answered by Grundewald. Solution: The navbar needs to begin before the initial segue. Thank you for your help!

Swift - How do I segue with a button from ViewController to a specific TabBarController

From a ViewController before a TabBarController in Swift, I would like to send to a specific Tab after the TabBarController that may be a NavigationController or a simple ViewController.
For the moment I make like this:
#IBAction func settingsButtonPressed(sender: AnyObject) {
tabBarController?.selectedIndex = 3 // 4d tab
performSegueWithIdentifier("tabBarShow", sender: self)
}
With this action, it performs the segue but it always show the first tab.
Thanks for the help!
You can use:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "tabBarShow"){
if let tabVC = segue.destinationViewController as? UITabBarController{
tabVC.selectedIndex = 3
}
}
}
You can try set the selectedViewController property instead of the selectedIndex:
tabBarController.selectedViewController = tabBarController.viewControllers![2]
This person had the same problem: https://stackoverflow.com/a/17919837/3933375
EDIT
This is on the assumption that the tabBarController has been initialised, if not you'll need to initialise it first.
You are setting the selected index before the TabbarController is loaded , so store your selected index somewhere , and set this in TabBarViewController's ViewDidLoad.
self.selectedIndex = valueOFSelectedIndexStored;

How to seque to ViewController inside Navigation Controller inside TabBarController

This is a totally dumb question I am sure. I have a viewcontroller that is inside a navigation controller that is inside a TabBarController. Apple says this is the right way to implement that setup. However, how can I prepareForSeque to that complex and send data to the first ViewController.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "presentActionItems" {
println("preparing")
let tabBarController:actionItemsTabController = segue.destinationViewController as! actionItemsTabController
let navigationController = ???
let viewController = ???
viewController.givenURL = actionItemsURL
}
}
I am sure this is an easy question, can I have some help.
Try this in your TabController's viewDidLoad after passing it your data
let firstViewController : UIViewController = self.viewControllers.objectAtIndex(0).topViewController;
firstViewController.this = self.this;
firstViewController.that = self.that;

Can't embed in navigation controller if I am passing data with prepareForSegue

I am working on a custom camera app where I present 3 viewControllers modally. At each segue, I pass data with prepareForSegue function. My problem is after the work with camera is finished, I need to show 2 more viewControllers which need to be inside a navigationController.
I have realized that If I don't pass any data, the navigation controller works fine. However, when I pass data, the app crashes on runtime. What is the right way of doing this?
Here is my prepare for segue function;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "camera2Crop"{
let controller: CropViewController = segue.destinationViewController as CropViewController
controller.photoTaken = self.photoTaken
}
}
where photoTakenis an UIImage object. Moreover, here is the screenshot from my storyboard where I put the navigationController. I call the prepareForSeguefunction in CustomCameraViewController to segue to CropViewController.
EDIT: I have changed my prepareForSegue to the following code;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "camera2Crop" {
let controller: CustomNavigationController = segue.destinationViewController as CustomNavigationController
controller.photoTaken = self.photoTaken
}
}
Now the app does not crash but I don't know how to send an object through Navigation Controller
let controller: CropViewController = segue.destinationViewController as CropViewController
Double check if segue.destinationViewController is actually the navigation view controller.
If it's the navigation controller, get CropViewController from it:
if segue.identifier == "camera2Crop" {
let navController = segue.destinationViewController as UINavigationController
let controller = navController.viewControllers[0] as CropViewController
controller.photoTaken = self.photoTaken
}
Note you don't have to subclass UINavigationController.

Resources