I need to update a variable in a View Controller and I need to do that from a Tab Bar Controller.
See image below:
I have tried so many different code but, since i implemented a Navigation Controller in between, none of them is working.
Code1
let myVC:MyViewController = self.viewControllers?[0] as MyViewController
myVC.x = "marco"
Code2
var storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var myVC:MyViewController = storyboard.instantiateViewControllerWithIdentifier("view2") as MyViewController
myVC.x = "marco"
Code3
let myInstance:MyViewController = MyViewController()
myInstance.x = "marco"
But none of them is working.
Help please
Thank you very much
You need to follow the view controller hierarchy from your tab bar controller down to your custom view controller, like this:
Tab bar controller (self) -> navigation controller -> custom view controller
We want to get the tab bar controller's first view controller (a navigation controller), and then get that navigation controller's first view controller. In code, you can do it this way:
let myVC = (self.viewControllers?[0] as UINavigationController).viewControllers[0] as MyViewController
myVC.x = "marco"
Note that this is brittle, and will fail if you change the hierarchy at all.
Related
I have a view controller as my initial view controller. there's a button in it(GO button) which when the user taps, it should go to another view controller(let's call it Destination view controller with label 'This is where i wanna go'). Meanwhile i want to pass it through a Tabbar controller. The reason is i want to have tabbar in my navigation stack.
I wish to go directly to the Destination view controller while pressing go button but it should show the tab bar items at the bottom.
So for achieving this in FirstViewController didLoadMethod I checked a bool value and pushed the view controller to the Destination view controller. I achieved the result I.e when pressing the Go button it goes to the Destination view controller and has tab bar items at it's bottom.
But the problem since it passes through the Tabbarcontroller the FirstViewController is shown for some seconds and then it pushes to the Destination view controller. I wish to hide FirstViewController while this transition takes place.
How to achieve this?
Picture shows what i want. what can I do to hide firstviewcontroller while having it in navigation stack?
I think this can be done in a simple way -
In the first viewController of the tab bar has a viewDidLoad() function or you can use loadView() which is called before the viewDidLoad() function. Push to the next viewController in the function.
Put your push navigation code in one of those functions
*You cant see the current view coltroller, it will push the screen to your required viewcontroller before loading the tab bar initial view contoller.
Hope it will work for you.
or >>>> you can check it out
let storyboard = UIStoryboard(name: "your_storyBoard_name", bundle: nil)
let viewController1 = storyboard.instantiateViewController(withIdentifier: "firstViewController")
let viewController2 = storyboard.instantiateViewController(withIdentifier: "secondViewcontroller")
let controllers = [viewController1, viewController2]
self.navigationController!.setViewControllers(self.navigationController!.viewControllers + controllers, animated: true)
The effect you're trying to produce is hard to do in a storyboard. Programmatically you would simply create the Tabbar Controller (with its children) and the "This is where I want to go" Controller, and then ask the navigation controller to show both at the same time.
For example, after "Go" is tapped, this is the code I would run inside your first view controller:
let tabBarController = UITabBarController()
let finalDestination = UIViewController()
var viewControllers = self.navigationController?.viewControllers ?? []
viewControllers.append(tabBarController)
viewControllers.append(finalDestination)
self.navigationController?.setViewControllers(viewControllers, animated: true)
Given the structure you shown, where the view controller A is the root view controller of the TabBar, you should push the second view controller B on the navigation stack inside either willAppear or didLoad of view controller A, according to your personal business logic (flag, conditions, etc.).
The trick here is to use either pushViewController or setViewControllers with animated: false so that the navigation stack will be set immediately during willAppear/didLoad and it won't show the push animation of B over A. This way, at onDidAppear the layout will be already fully rendered in it's final state: with B at the top of the navigation stack and no animations in progress.
I am new in swift.I want to move one screen to another but problem is when I go to another screen,old screen overrides in new screen.
here is my code
dispatch_async(dispatch_get_main_queue()) {
let appsDelegate = UIApplication.sharedApplication().delegate
appsDelegate?.window!!.rootViewController = nil
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("launcherEntry") as! UINavigationController
appsDelegate?.window!!.rootViewController = nextViewController
}
If you use UINavigationController, you should push the second view(screen) onto the first one. Check out the offical guide.
According to your code old screen overrides because you have not a push a UIViewController into navigation controller.
Here you destroyee the exiting controller then set a new controller. So that previos controller is move from the memory.
Please show Apple docs for the UINavigationController.
Update:
Please use following code for set a root view controller then you can push view controller easily.
let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as! UINavigationController
let rootViewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("ID_LoginVC") as! LoginVC
navigationController.viewControllers = [rootViewController]
self.window?.rootViewController = navigationController
As you are new , i will explain what will happen when your code will be executed.
App delegate will hold the current instance of our application. Setting root view controller is setting Main view controller of your application.
SO basically your code will replace the previous view controller with next one.
Now what you have to do,
We have Navigation controller to manage our navigation stack, that allow us to go back from where have initiated.
If you are Using storyboard , add your view controller into NavigationController, if you already know then ignore or follow this
SELECT your first view controller GOTO Editor in Xcode menu > Select EMBED IN option > NAVIGATION CONTROLLER. this will add your first view into navigation controller.
Now when you want to display next view controller, There are two ways to do so,
1)Using segue from Storyboard
Right click from view/button from where you want to show next view.
drag it to the next view controller and release the right click.
Select push
2)Programatically
you don't have to set root view controller, just push the next view controller in navigation controller you have
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("launcherEntry") as! UINavigationController
Above line will intimate the textview controller, now we have to push this view controller, we can achieve this with
self.navigationController?.pushViewController(nextViewController, animated: true)
I have current UISplitViewController setup:
UISplitViewController with master UINavigationController that contains UITableViewController and detail controller that contains UITabBarController.
Code:
// Create split view controller
let splitViewController = UISplitViewController()
let masterViewController = UINavigationController(rootViewController: UITableViewController())
masterViewController.topViewController?.title = "Master"
// Create tab bar controller
let tabBarController = UITabBarController()
// Setup view controllers
var viewControllers = [UIViewController]()
for i in Range(start: 0, end: 4) {
let vc = UIViewController()
vc.view.backgroundColor = UIColor.whiteColor()
vc.title = "View Controller \(i+1)"
let navigationController = UINavigationController(rootViewController: vc)
viewControllers.append(navigationController)
}
tabBarController.viewControllers = viewControllers
splitViewController.viewControllers = [masterViewController, tabBarController]
That yields following on iPhone 6S Plus in landscape:
Issue:
After rotation to the portrait mode, UINavigationController from detail view controller is replaced with master UINavigationController, instead of using navigation controller from the detail view controller.
It's obviously the expected behavior, but I would like to use UINavigationController from the detail view controller and still have a back button for the master view controller. You can look at the Facebook Messenger app to see what am I talking about.
The issue has to do with the fact that you're trying to embed a tab bar controller in a navigation controller, and make that the detail view controller for a split view controller.
The tab bar controller would expect to change navigation items for the selected tab, but your hierarchy is opposite of what it expects.
While the tab bar controller is able to display a selected tab's navigation items on a detail navigation controller which isn't collapsed, things break down once collapsed as the tab bar controller doesn't realize that it's been pushed onto a view controller stack of the master navigation controller. It's updating the wrong navigation bar at that point.
The SDK doesn't natively support that particular Adaptive UI hierarchy. You could file a feature request, or see if another developer has code to work around how the split view controller delegate would need to collapse an embedded tab bar controller.
I have a view which contain text views and a bar controller with two bars I want to update bar with data entered by user in text fields ... How can I access that tab bar after view has been loaded
You can access your UITabbarController instance like following if you have set the storyboardID for that
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mytabbar = storyboard.instantiateViewControllerWithIdentifier("myTabbar");
In similar manner you can access the instance of your UIViewController that are part of `UITabbarController and pass data on their instance. Or you can use notification for that purpose.
I'm having trouble with a simple Xamarin Studio Storyboards concept. See screenshots below for visuals and see the downloadable source code here.
Let's say I have a NavigationController with MainViewController on it. This is visible in my storyboard. I want a button which, when pressed, brings up a new NavigationController with RedViewController. I also want RedViewController on the same storyboard as the MainViewController. In this project, I tried to do that but for some reason when I do a:
var myStoryboard = AppDelegate.Storyboard;
// Instatiating View Controller with Storyboard ID 'StuffViewController'
RedViewController = myStoryboard.InstantiateViewController ("RedViewController") as RedViewController;
RedViewController.ModalTransitionStyle = UIModalTransitionStyle.CoverVertical;
this.PresentViewController(RedViewController, true, null);
the RedViewController doesn't have it's Navigation controller with it. When presented RedViewController's Navigation Controller is null! What am I doing wrong there?
Now when I created a NavigationController & BlueViewController in a totally seperate storyboard it works fine. When I press the Blue Button it goes to the BlueViewController and correctly shows it's NavigationController. Why is this one working but the other one not? The only difference that I can see is that they are on separate Storyboards.
UIStoryboard storyBoard = UIStoryboard.FromName ("BlueStoryboard", null);
UIViewController controller = storyBoard.InstantiateInitialViewController () as UIViewController;
controller.ModalTransitionStyle = UIModalTransitionStyle.CoverVertical;
this.PresentViewController (controller, true, null);
ViewController that can present a new NavigationController & ViewController ViewController called "Red" with a navigation bar
When you instantiate your new view controller you need to instantiate the UINavigationController, not the RedViewController.
In the case of your 'blue' code you instantiate the initialViewController - which is the navigation controller that contains the Blue controller.
You want
RedViewNavigationController = myStoryboard.InstantiateViewController ("RedViewNavigationController") as UINavigationController;
where 'RedViewNavigationController' is the identifier for the navigation controller that the Red View Controller is embedded in.
If you want to present the red controller with its navigation controller, you should instantiate the navigation controller (which, in turn, will instantiate the red controller), and present it.