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.
Related
In my app I have two View Controllers embedded in Navigation Controller. And I want to have an option to get data from the SecondVC while only the FirstVC is opened. So it's like I don't want a user to see (or at least interact) with the SecondVC.
The point is
So user just taps on the button on the FirstVC (without opening SecondVC before) and I can get access to the data on the SecondVC.
How can I get data from the next view controller (SecondVC) in a navigation controller without opening it? Or at least not letting user to interact with it.
For now my only ideas are:
To push segue when user taps a button, get it into memory, load data and then dismiss it. But that doesn't look good when view controllers change so fast.
To present next view controller modally offscreen, get data and dismiss it. So the user won't even see that another view controller opened.
Edit:
I need to get access not just to method or smth. I need to get to the #IBOutlet. To the subclass of UIView (working with CorePlot) to be exactly
You should never call loadView() directly.
Instantiate your UIViewController and then call loadViewIfNeeded().
Calling this method loads the view controller’s view from its storyboard file, or creates the view as needed based on the established rules.
EDIT:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// make sure that the value is an optional so that you can unset it later. This one is optional because we use optional casting (with as?)
var vc = storyboard.instantiateViewController(withIdentifier: "secondVC") as? SecondViewController
vc?.loadViewIfNeeded()
// use the vc
// later unset vc variable
vc = nil
for this, you must call your 2ed View controller with 'modalPresent' and background of 2edVC must clear. Then get your data from 2ed VC. with no change. for more information about modalPresent there is the link.
Solution:
You can create a function for it or just insert in viewDidLoad() etc.
Don't forget to change StoryboardID for the chosen view controller (it can be done in your storyboard file in Identity inspector).
// You need to get to your Storyboard and instantiate the needed View Controller
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var graphVC = storyboard.instantiateViewController(withIdentifier: "graphVCID") as? graphVC
// Then call loadViewIfNeeded (don't call loadView()) to load #IBOutlet's etc
graphVC?.loadViewIfNeeded()
// My outlets are loaded with data when these functions are called
graphVC?.viewDidLoad()
graphVC?.viewWillAppear(false)
// Unset the reference to VC
graphVC = nil
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)
Ok, i know that when applicationWillEnterForeground fires from my AppDelegate that I can trigger events. What I would like to do is force the app to show a certain view when It re-appears from the background. The view is a UIViewController called loginViewController and it has a storyboard Id of "initViewController"
My question is, what do I use in this function (applicationWillEnterForground) to make this view load when the app comes into focus again? Thanks.
I've never done this, but you can probably accomplish it by adding the UIViewController to the UIViewController stack, if you just want to add a view controller and show its view (see Case 1) or by replacing the root view controller, if you want to discard the existing view controller stack and use a new one (see Case 2)
Case 1) In the first case you need a reference to the UIViewController that you want to make its parent view controller. You can save this in a static variable somewhere or if you are just planning on showing a temporary view when the app restarts then you can get a reference to the root view controller use it as the parent:
// get a reference to the main storyboard
let mainSB = UIStoryboard(name: "Main", bundle: nil)
// get a reference to the root view controller
if let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController,
// get a reference to the view controller using identifier
initVC = mainSB.instantiateViewControllerWithIdentifier("initViewController") as? UIViewController {
// present the view controller
rootVC.presentViewController(initVC, animated: false, completion: nil)
}
When you are finished use logic from within the view controller to dismiss it, and your user should be back where they started:
self.dismissViewControllerAnimated(true, completion: nil)
Case 2) You can discard the existing view controller stack and start a new one by replacing the root view controller and building up the stack manually. Present the view controllers in order with animated parameter set to false.
// get a reference to the main storyboard
let mainSB = UIStoryboard(name: "Main", bundle: nil)
// get references to view controllers
if let vc1 = mainSB.instantiateViewControllerWithIdentifier("vc1") as? UIViewController,
vc2 = mainSB.instantiateViewControllerWithIdentifier("vc2") as? UIViewController {
// set root view controller
UIApplication.sharedApplication().keyWindow?.rootViewController = vc1
// build up the view controller stack by adding next vc
vc1.presentViewController(vc2, animated: false, completion: nil)
}
Navigation Controllers
If one of your view controllers is a navigation controller you will need to cast it as such, and then push any view controllers onto your navigation controller. Navigation controllers have their own stack.
if let myNavCon = mainSB.instantiateViewControllerWithIdentifier("nav") as? UINavigationController {
// push view controller onto navigation controller stack
myNavCon.pushViewController(someViewController, animated: false )
}
WARNING
This does not deal with the model of your app at all (only the UI). You will also need to set any data that you would have set in prepareForSegue, etc. An easy system to use when you have VCs you present both programmatically and via storyboard segues is to take the code that would have been in prepare for segue and move it to your own instance method that takes a reference to the child view controller as its parameter. Then you can call it from prepare to segue with the destination view controller or from code before you present the view controller.
None of this code has been tested. It was written directly via the website. It likely contains typos. Please let me know so I can fix any.
I have a side menu with different navigation points. The first point is the Home nav. This should send the user back to the main view.
If I start the app, the main view will be generated by the storyboard. Now if a user taps on "Home", I would like to reuse this instance. Is this possible? (With Swift)
Thanks
You can instantiate a view from a storyboard like so:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeViewController = storyboard.instantiateViewControllerWithIdentifier("Home")
Then do as you will :)
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.