Implementing SWRevealViewConroller Library (slide out menu) in multiple storyboards - ios

I am trying to implement SWRevealViewController Library as given in VideoTutorial, I was successfully able to do that but I don't want everything on 1 storyboard, I want to break it down in 2 storyboards
AppDelegate Code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
storyboard = UIStoryboard(name: "MenuDrawer", bundle: nil)
initialViewController = storyboard.instantiateViewControllerWithIdentifier("SWRevealViewController") as! UIViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
Rightnow MenuDrawer storyboard has everything
SWRevealViewCOntroller
TableViewController
NavigationController
RootViewController
and below segues which are defined in Library:
segue1 (sw_rear) : between SWRevealViewController --> TableViewController
segue2 (sw_front) : between SWRevealViewController
--> NavigationController
now I want 3 and 4 in different storyboard. but when I move 3 and 4 to different storyboard how do I create segue 2 across storyboards

I am not really sure if I understand your problem but you can try this to retrive your second storyboard and load your navigationViewController from there.
let storyBoard : UIStoryboard = UIStoryboard(name: "SecondStoryBoard", bundle:nil)
let deleteViewController = storyBoard.instantiateViewControllerWithIdentifier("SecondNavigationController") as UINavigationController

as far as iknow, you don't need to push again the segue 2 on 3 and 4. what you need is refer the new view (3 and 4) on the segue 2. in Obj-c will be like that:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SWRevealViewController *view = [mainStoryboard instantiateViewControllerWithIdentifier:identifier];
[view setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.navigationController pushViewController:view animated:YES];
where "self" is the ViewController where segue 2 has been created.
i hope have answered your question.

Try This First Of All Make the other storyboard and add what you need then call the other storyboard in
AppDelegate Code:
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var storyboard = UIStoryboard(name: "MenuDrawer", bundle: nil)
var storyboard1 = UIStoryboard(name: "NewStoryBoardName", bundle: nil)
var initialViewController = storyboard.instantiateViewControllerWithIdentifier("SWRevealViewController") as! UIViewController
var initialViewController1 = storyboard1.instantiateViewControllerWithIdentifier("SWRevealViewController") as! UIViewController
self.window?.rootViewController = [initialViewController,initialViewController1]
self.window?.makeKeyAndVisible()
return true
}

You can not create segues between storyboards using Interface Builder.
It is only possible programmatically, example:
let storyBoardTwo = UIStoryboard(name: "storyBoardTwo", bundle: nil)
// Pick a view controller using it's identifier string set in Interface Builder.
let myVC = storyBoardTwo.instantiateViewControllerWithIdentifier("aViewController") as! UIViewController // Or use more specific (custom) type
// Push it to the navigation controller
self.navigationController.pushViewController(myVC, animated: true)
pushViewControllerwill simply put the myVC on top. This code is what a segue will end up doing. You can put this code anywhere in an existing ViewController, on a button tap IBAction or where ever needed.
If the pushed ViewController have any segues (from Storyboard or code) they will still work, as expected.
This of course works if you would want to programmatically load ViewController from one and the same Storyboard as well.

Related

Show viewcontroller by storyboard id inside UITabBarController

I am trying to show a specific viewcontroller, inside my UITabBarController by using the viewcontrollers storyboard id.
I can only get the viewcontroller to be shown without the UITabBarController.
How do I also show the UITabBarController?
I have tried this:
let identifier = "stats_view"
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: identifier)
window?.rootViewController = vc
Which gave me the problem listed above.
try the following code.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let myTabBar = self.window.rootViewController as? UITabBarController { // Getting Tab Bar
myTabBar.selectedIndex = 2 //Selecting tab here
return true
}
}
You should set selectedIndex of tabBarController to view specific viewController which are embedded in tabBarController. Use following code.
self.tabBarController?.selectedIndex = 1

How to load all view controllers storyboard, but skip the natural sequence and go direct to a specific view?

Suppose I have three view controllers inside a storyboard, I want to load all of them into view controllers stack, but I want to choose which one will appear first to the user. As shown below I would like to show the third view on load, instead to show the first, any glue?
Option 1. Using storyboards, you see the arrow pointing to your ViewController 1. Drag that to View Controller 2 or 3.
Option 2. On load of your first view controller, you can instantiate whichever view you'd like in your viewDidLoad(), provided you have given each storyboard view an ID.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "YourVCIdentifier")
self.present(controller, animated: true, completion: nil)
Option 3. In your AppDelegate file, you can do this.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "YourVCIdentifier")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}

Tabbar won't hide when pushed into a ViewController inside a UITabBarController

For the purpose of this question, I'm showing a stripped down version of my view hierarchy. My app contains a UITabBarController as the base. Each tab's top most view controller is a navigation controller and it has view controllers embedded in each of them.
Let's take the first tab.
UITabBarController -> UINavigationController -> UITableViewController -> UIViewController
Let's say the UITableViewController instance is some sort of a list and the UIViewController is the detail view. When the user taps on an item from the list, it takes you to the detail view. And when that happens I have set the UIViewController's hidesBottomBarWhenPushed property to true so that the tabbar at the bottom would hide when the user is in the detail view.
My app receives push notifications. When tapped on them, it should open directly into the detail view. I can get it to navigate there. But the issue is the tabbar at the bottom is still visible!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController
if openingFromPush {
let firstNavigationController = storyboard.instantiateViewController(withIdentifier: "FirstNavigationController") as! UINavigationController
let tableViewController = storyboard.instantiateViewController(withIdentifier: "TableViewController") as! TableViewController
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
viewController.hidesBottomBarWhenPushed = true
firstNavigationController.viewControllers = [tableViewController, viewController]
tabBarController.viewControllers?[0] = firstNavigationController
// tabBarController.tabBar.isHidden = true
window?.rootViewController = tabBarController
} else {
window?.rootViewController = tabBarController
}
window?.makeKeyAndVisible()
return true
}
I set that same hidesBottomBarWhenPushed property to true in the when I instantiate the view controller but that doesn't seem to have any effect. I even tried straight up hiding the tabbar like this tabBarController.tabBar.isHidden = true but that doesn't do anything at all either.
I can't figure how how to resolve this. Any help would be appreciated.
I attached a sample Xcode project here as well if that helps.
You can use this code for pushing detail view controller:
if openingFromPush {
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
viewController.hidesBottomBarWhenPushed = true
if let nvc = tabBarController.viewControllers?[0] as? UINavigationController {
nvc.pushViewController(viewController, animated: false)
}
window?.rootViewController = tabBarController
}
You don't need to init navigation view controller and table view controller again its already inside tab bar controller

How to instantiate a viewcontroller with an embedded navigation controller programmatically swift?

I'm trying to instantiate a viewcontroller that has a navigation controller embedded in it from the AppDelegate. Here's what my code looks like:
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let dm = DefaultsManager.sharedManager
if dm.rememberMe == true {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("VolunteerVC")
self.window!.rootViewController = vc
self.window?.makeKeyAndVisible()
}
the right viewcontroller is instantiated but the navigation controller that was embedded in it is now missing. Any idea how to instantiate it with the nav controller still connected?
Figured it out, give the nav controller a name under storyboard ID in the storyboard and for the line:
let vc = storyboard.instantiateViewControllerWithIdentifier("VolunteerVC")
replace the name of the viewcontroller with the name you gave the navigation controller.

Going directly to Main View controller for already logged in user

I have a loginViewController as my rootviewcontroller followed by the main screen and then other screens. My views follow push and pop approach. What I want is, if a user is already logged in my view should start from main screen else start from login screen, and if I logout from main screen it should go back to login screen and the push and pop structure should be maintained. I can achieve this using the modal transition, but I need to use push and pop approach, is this possible? Currently I have checked a already logged in condition in my appdelegate to set the rootview controller but it fails if I attempt to log out as its not present in my navigation controller stack.
if !alreadyLoggedin
{
let mainListVC = storyBoard.instantiateViewControllerWithIdentifier(“MainListViewController”)
self.window!.rootViewController = mainListVC
}
else
{
let loginVC = storyBoard.instantiateViewControllerWithIdentifier("ViewController")
self.window!.rootViewController = loginVC
}
It's basic use case, Please follow my steps.
In your story create navigation view controller with view controller.
Select navigation view controller as your initial view controller in storyboard.
Create three views controllers- InitialViewController, LoginViewController and MainViewController and set storyboard id for all view controllers.
Created sample project for you. Download link (https://www.dropbox.com/s/zk8x7ptg5mzmotk/test.zip?dl=0)
You need to use. AppDelegate
Objective C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"LoginStatus"]) {
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"loginController"];
}else{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"RootView"];
}
}
SWIFT - i hope its correct
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
//Using UserDefaults check already loggedin user or not
if (!someStatus) {
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}else{
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
}

Resources