How to passing data when using presentViewController with UITabbarController - ios

I am a newbie in ios. I tried working with UITabbarController and I had some problem with it when I tried passing data from a ViewController (this ViewController is not an item in Tabbar) to an other ViewController that is item in Tabbar. In this case i wanted when I click button Login,"Welcome to shop" will appear and the username in Login form will pass into label in "Welcome to shop".
I use code below to make "welcom to shop" controller appearing:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabbarController= [storyboard instantiateViewControllerWithIdentifier: #"tabbarID"];
[self presentViewController: tabbarController animated: YES completion: nil];
So any idea for me to passing username (in LoginController) into label (WelcomeToShopController) ?

My problem resolved after i researched :). Because i used tabbar and navigationController so i should instance tabbar -> NavigationController -> ViewController. In my case, this code below will work perfectly.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabbarController= [storyboard instantiateViewControllerWithIdentifier: #"tabbarID"];
UINavigationController *nav = (UINavigationController *)[tabbarController.viewControllers firstObject];
HistoryOrderingController *historyOderingVC = (HistoryOrderingController *)[[nav viewControllers] firstObject];
historyOderingVC.user = self.user;
[self presentViewController: tabbarController animated: YES completion: nil];

Related

How to switch from one storyboard to another storyboard

Team,
I have two storyboard.
One For authentication
And another for My application Dashboard.
For Authentication Storyboard The initialisation screen is loginScreen.
After login successfully i am loading Dashboard Storyboard.
For dashboard storyboard the initial screen is MainViewController.
Here I am implemented logout from in DashboardStoryboard. So now i want to switch back to my Authentication Storyboard.
Here its going back to loginScreen.
But I thing its not proper way for implementing.
It will be more helpful is there a way I can do better?
-(void)logout{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Authentication" bundle: nil];
LoginViewScreenController *loginViewScreenController = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewScreenController"];
[self.navigationController pushViewController: loginViewScreenController animated:NO];
}
Your feedback is highly appreciated.
It's Very easy using segue and Storyboard Reference. Please follwo steps and screenshots.
Step-1)
Drag and drop Storyboard Reference from Object Library in First(Main) Story board.
Step-2)
Add segue from your source ViewController to Storyboard reference.
Step-3)
Select another(second) storyboard.
Reference ID: StoryboardID of your destinationViewControler(second View Controller) which is available in Second.Storyboard
-(void)logout
{
UIViewController *aVCObj = [[UIApplication sharedApplication]delegate].window.rootViewController;
if ([aVCObj isKindOfClass:[UINavigationController class]]) {
UINavigationController *aNavController = (UINavigationController *)aVCObj;
[aNavController popToRootViewControllerAnimated:YES];
}
}
Here is a trick to do this by setting up a key true using NsuserDefaults when user logged in otherwise false and navigate your application when starts accordingly using presentViewController method without animation so the user will not get any option to go back previous vc.
Have a look below code illustrating above sentence:
if ([[[NSUserDefaults standardUserDefaults] valueForKey:#"isloggedIn"] isEqualToString:#"true"]) {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"loginView"];
[self presentViewController:vc animated:NO completion:nil];
}else{ // when logout
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"logoutView"];
[self presentViewController:vc animated:NO completion:nil];
}
If you need to apply some effects when vc appear just ad these two lines before presentViewController method see:
[vc setModalPresentationStyle:UIModalPresentationCustom];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
Note: set the key false when user logout.
let sb = UIStoryboard(name: "Main", bundle: nil)
let homeVC = sb.instantiateViewController(withIdentifier: "TabBarController")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = homeVC

after call presentViewController tab bar is missing

test1 is the first view in the tarbarcontorller..
after I call presentViewController tab bar is missing
How can i solve it??
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
TestTableViewController *TestTableViewController = [storyboard instantiateViewControllerWithIdentifier:#"test1"];
TestTableViewController.memberid = memberid;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:TestTableViewController];
[self.navigationController presentViewController:navigationController animated:YES completion:nil];
You should present your TabBarController, and not the first view of your tab bar controller. It will display the first view in your tab bar controller properly.

self.navigationController is 'null' even after embed in a navigationcontroller

I want to add a navigationcontroller to an existing viewcontroller which is created using storyboard, i have embed it in a navigation controller, but the code for navigating (shown below) is not working even after embed in the navigation controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self.navigationController pushViewController:nextViewController animated:YES];
When i have tried to log self.navigationController, it shows null.
Update: It is fine when i am trying with presentViewcontroller , but i
want to push the viewcontroller with navigationController.
I am struggling with this for two days,Please help.
If self is a subclass of UINavigationController, you do not need to refer to the navigationController property. (Note that this won't work if EditProfileViewController is also a subclass of UINavigationController, as you can't push a UINavigationController inside a UINavigationController).
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self pushViewController:nextViewController animated:YES];
Otherwise, if you don't have a pre-existing navigation controller
EditProfileViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:#"EPVController"];
[self presentViewController:nextViewController animated:YES completion:nil];

Cant find navigation controller to navigate another view controller

I have a custom UIPageViewController which contain 5 UIViewControllers, now I would like to navigate from one of those 5 UIViewController, But I can't because in those UIViewControllers have no UINavigationController. Can anyone suggest me , how I can navigate from one those 5 UIViewControllers to other UIViewController?
The code in your - [AppDelegate application:didFinishLaunchingWithOptions:]
might look like this (after checking your skipping condition of course):
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:#"MyAuth"]; //if you assigned this ID is storyboard
UIViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:#"Login"]; //if you assigned this ID is storyboard
UIViewController *vc3 = [storyboard instantiateViewControllerWithIdentifier:#"Details"];
NSArray *controllers = #[vc1, vc2, vc3];
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
[navController setViewControllers:controllers];
If you paste just this to - [AppDelegate application:didFinishLaunchingWithOptions:], you'll see that it works immediately.

pushviewcontroller is not work in xcode6

The below code is work in ipad,but not in iphone.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *controller = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
UINavigationController *navgiate = [[UINavigationController alloc]initWithRootViewController:controller];
[self.navigationController pushViewController:navgiate animated:YES];
i cannot able to load MoreApsviewController by instatinating. View Controller is not pushing Please help me how to do it. Thanks in Advance
If your container is a navigation controller, you can simply intilize a VC and push it like this;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *controller = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
//No need to create a nav controller here
[self.navigationController pushViewController:controller animated:YES];
If you want to add another navigation controller you can present it or add as child VC, but you can't push another navigationVC.
These 2 steps should be checked.
If the View Controller you are trying to access is embedded in a Navigation controller you are correct in referencing the correct view, otherwise, change UINavigationController *navgiate to UIViewController * navigate (OR UITableViewController * navigate if it is a Table View).
Check your referencing of View Controllers. If you have the name (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"]; I will assume that you have named your View Controller MoreAppsViewController. You need to name and reference the UINavigationController as it is the point of reference.
This is the result for View Controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *controller = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
[self.navigationController pushViewController:controller animated:YES];
This is the result for Navigation Controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
//You need to name your Navigation Controller "MoreAppsViewController" for this to work
UINavigationController *controller = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"MoreAppsViewController"];
[self.navigationController pushViewController:controller animated:YES];

Resources