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 :)
Related
I'm trying to send some values from one viewcontroller to another. (all embedded in NavigationController). I could make segue with normals viewcontrollers so in general I understand a segue idea. I've to do that in two different ways and got two different problems.
First way, segue does right but "two times". I mean after segue (which I want) there is another segue to same controller but without navigation controller and without data I would send. The "back" button on last viewcontroller is returning to AlmostViewController.
Here's code:
Second way, Nothing happened,
ErorCould not cast value of type 'RevisionApp.AlmostViewController'
(0x1055d58c0) to 'UINavigationController' (0x107669f18).
Here's code:
For the first Problem, When using embedded in navigation controller you have don't have to create action function when the button is tapped. Navigation controller does it for you. So things you need to do :
Disconnect the function btnTapped from the button using storyBoard.
Delete or comment the function btnTapped(You don't need it).
Problem is line number 18. You are trying to cast AlmostViewController to a UINavigationController. Directly access like this,
let detailController = segue.destination as! AlmostViewController
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewController2") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
I have implemented SWRevealViewController into my project successfully. The side menu is a UITableView with different cells which navigate to differing view controllers. When I tap on the 'profile' cell it defaults to the LoginViewController, there the user will login in and, once verified, will go to the ProfileViewController. This leads to my problem, when I navigate to the ProfileViewController the Navigation bar button (on the left) does not work properly. Likewise, when I press the logout button (the right navigation bar button) it does navigate back to the LoginViewController, however, the menu button does not work.
My navigation functions are as follows:
func switchVC() { //Navigates to the profileViewController
let storyboard = UIStoryboard.init(name: "Login", bundle: nil)
let nav = storyboard.instantiateViewControllerWithIdentifier("Profile")
UIApplication.sharedApplication().keyWindow?.rootViewController = nav
}
func switchBack() { //Navigates back to the LoginViewController
let storyboard = UIStoryboard.init(name: "Login", bundle: nil)
let nav = storyboard.instantiateViewControllerWithIdentifier("Login")
UIApplication.sharedApplication().keyWindow?.rootViewController = nav
}
My storyboard is as follows:
Any help that you may be able to provide would be greatly appreciated. Thank you everyone!
Cheers
Your question is somewhat difficult to answer, but maybe this will help. In order for SWReveal View Controller to work, you always have to route to the main SWReveal Controller Scene for the menu button to work. For example, if my storyboard looked like
SWRevealViewController Scene --> Navigation Controller Scene --> Main View
and I routed back to the Navigation Controller Scene, the button would not work. You have to route back to the first Instance of the SWRevealController Scene that comes before the navigation / any other scenes you are trying to reach.
If you need to add another instance of SWRevealViewController Scene to make that work, just segue through the sw_front identifier to the navigation controller/ view controller you need the button to work in, and then segue it through the sw_rear identifier back to the same one instance of the menu.
Sorry if this answer is confusing, please let me know if I can clarify anything.
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 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.
What is the best practice to switch between multiple views; change the rootViewController or use modal views?
Setting the rootviewController:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var vc : UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
var window :UIWindow = UIApplication.sharedApplication().keyWindow!
window.rootViewController = vc;
window.makeKeyAndVisible()
Changing the modal view:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
self.presentViewController(initViewController,animated: false, nil)
I'm confused as to which to use when I need to present the user some other view.
p.s. In my case, I've an app starting with the login form as the rootViewController. After login, I think it's best to change the rootViewController but am I right?
My suggestion is instead of bothering lot about it you just got to override the rootviewController rest of the thing is taken care by your app.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
self.window?.rootViewController? = initViewController
In this code we are just overriding rootview controller from AppDelegate.
The point of the "rootViewController" is to a a central pivot for all of your views.
See it as a the top-most link in series of chains. e.g. views on the left, can talk to views on the right can talk to views in the middle thanks to the "rootViewController". the "common link".. On top of that, it controls (or has the capability of controlling) how the hypothetical views on the left, right and middle are configured etc...
Fine, I worded it a bit trivially, but the (UINavigationController) is specifically for hierarchal content i.e. a UITabViewController is a great example..
I've just noticed a great post on here (Changing root view controller of a iOS Window) whilst finding a decent image to explain it decently.
Kudos to Matt on that post :) Hope this helps.
If not feel free to ask away :)
edit and for reference for others, here's something cheap and quick I slapped together that might get your head wrapped around storyboards :)
https://dl.dropboxusercontent.com/u/61211034/viewFun.zip
Cheers,
A
The application window's root view controller is the first view loaded into the window after the splash screen. Although it is possible to switch the window's view controller to change the view, consider using a UINavigationController and pushing additional view controller onto the view stack. This gives the user more flexibility, allowing the user to go back and forth between views.