Best approach to switch between TabBarController and ViewController - ios

I have been trying an app with initial login screen which then takes to a TabBarController.
I want to know whats the best approach to do this any example code would be appreciated. I have tried it but I am unable to switch from ViewController to TabController.

I'm not sure that this is the best way to do it, but it's quick and dirty and works. Present a modal view controller inside your applicationDidFinishLaunchineWithOptions: method. You should replace the #selector with something more appropriate to what you want to do. Background color is for effect only.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
// ***** The relevant code *****
UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[[viewController view] setBackgroundColor:[UIColor redColor]];
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dismissButton setFrame:CGRectMake(10, 10, 300, 44)];
[dismissButton setTitle:#"Dismiss" forState:UIControlStateNormal];
[dismissButton addTarget:[self tabBarController] action:#selector(dismissModalViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
[[viewController view] addSubview:dismissButton];
[[self tabBarController] presentViewController:navigationController animated:NO completion:nil];
return YES;
}
I normally wouldn't like to put this sort of code in the app delegate, but if it's a one-time thing like login details, maybe it's ok.

Assume your root view controller is also your login view.
Now from your root view controller, you can present the tab bar controller a number of ways. One way is to just call the presentViewController method from the root view controller.
Setup
From within the root view controller, sometime before presenting the tab bar, set it up:
myTabBarViewController = [[MyTabBarViewController alloc] init];
[myTabBarViewController setModalPresentationStyle:UIModalPresentationFullScreen];
[myTabBarViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[myTabBarViewController setRootTabBarDelegate:self];
Presentation
When you are ready to present, just call this:
[self presentViewController:myTabBarViewController animated:YES completion:nil];
Notes
The view controller hierarchy looks like this:
AppDelegate
L RootViewController
L MyTabBarController

Related

Can't pop iOS viewController. Not sure, but I think it's something with the Navigation Controller

I'm having trouble trying to pop a view
App Delegate
#implementation MAAppDelegate
#synthesize navController;
#synthesize detailViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Init the navController for the Master Detail View of the grade cells
UINavigationController *navController = [[UINavigationController alloc] init];
detailViewController = [[UIViewController alloc] init]; //step6
navController = [[UINavigationController alloc] initWithRootViewController:[[MAController alloc] init]]; //step7
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = navController; //step8
[self.window makeKeyAndVisible];
// Set MAController as rootViewController
//self.window.rootViewController = [[MAController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// Use the insanely cool TSMessages to show network alerts
[TSMessage setDefaultViewController: self.window.rootViewController];
return YES;
}
First part of viewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController setNavigationBarHidden:YES];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStyleBordered target:self action:#selector(home:)];
self.navigationItem.leftBarButtonItem=newBackButton;
Later, when I change the viewController
NSLog(#"Opened progress report");
UIViewController *detailViewControl = [[UIViewController alloc] init];
// Set progress report as the view controller
[self.navigationController pushViewController:detailViewControl animated:YES];
UIImage *background = [UIImage imageNamed:#"bg"];
// Add static image bg
self.backgroundImageView = [[UIImageView alloc] initWithImage:background];
self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:self.backgroundImageView];
// Add blurred layer to image when tableView goes in front of it
self.blurredImageView = [[UIImageView alloc] init];
self.blurredImageView.contentMode = UIViewContentModeScaleAspectFill;
self.blurredImageView.alpha = 0;
[self.blurredImageView setImageToBlur:background blurRadius:10 completionBlock:nil];
[self.view addSubview:self.blurredImageView];
[self.navigationController setNavigationBarHidden:NO];
So I don't understand why that when I do this, a selector from the button (that I know fires, because I get Righthtere in my log):
-(void)home:(UIBarButtonItem *)sender {
NSLog(#"Righthtere");
// Set progress report as the view controller
[self.navigationController popToViewController:self animated:YES];
}
It doesn't go back to the initial view controller.
You seem to be confusing popToViewController and popViewControllerAnimated. popViewControllerAnimated removes the current view from the stack and brings the new stack top the active view controller. popToViewController pops the stack until the listed view controller is on top of the stack.
Since you are calling popToViewController with self, it will look and see that the requested view controller is already on top of the stack and do nothing. If you wish to go back one view controller then your call should be.
[self.navigationController popViewControllerAnimated:YES];
I use the below code to pop the previous viewcontroller in iOS 8.
[self presentModalViewController:viewcontroller animated:YES];

TabBarController and NavigationController

I am making an application but I'm still a beginner and I'm trying to get used to the RootViewController and how it should be set.
At the beginning my application launches, I want there to be a View which is not in my tabBarController (which is set to be my rootViewController).
What I am trying to ask is, Can I have another view which is outside my UITabBarController launch first without it being in the tabBarController's items list?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
FacebookFeedViewController *facebookClass = [[FacebookFeedViewController alloc] initWithNibName:#"FacebookFeedViewController" bundle:nil];
TwitterFeedViewController *twitterClass = [[TwitterFeedViewController alloc] initWithNibName:#"TwitterFeedViewController" bundle:nil];
LinkedInFeedViewController *linkClass = [[LinkedInFeedViewController alloc] initWithNibName:#"LinkedInFeedViewController" bundle:nil];
FTLFullFeedViewController *masterClass = [[FTLFullFeedViewController alloc] initWithNibName:#"FTLFullFeedViewController" bundle:nil];
/// tab button title
facebookClass.title = #"Facebook";
twitterClass.title = #"Twitter";
linkClass.title=#"LinkedIn";
masterClass.title=#"FTL";
// tab button Images
facebookClass.tabBarItem.image = [UIImage imageNamed:#"facebook_32"];
twitterClass.tabBarItem.image = [UIImage imageNamed:#"twitter_32"];
WelcomeViewController *welcomeClass= [[WelcomeViewController alloc] initWithNibName:#"WelcomeViewController" bundle:nil];
navController = [[ UINavigationController alloc] initWithRootViewController:welcomeClass];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:facebookClass];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:twitterClass];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:linkClass];
UINavigationController *navController5 = [[UINavigationController alloc] initWithRootViewController:masterClass];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController,navController5,navController2,navController3,navController4,nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
I know you already selected an answer but all that's doing is pushing a UITabBar view on top of an existing view, not creating a new UITabBarController view. Based on our brief conversation (latest XCode, no StoryBoards, using XIBs) you're going to want to create a xib as a UITabBarController then push it into view...
View *view = [[View alloc] initWithNibName:#"myUITabBarXIB" bundle:nil];
view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: view animated:YES];
This will present your XIB file but not on top of the existing view controller when the desired action takes place.
yes! ofcourse you do.
[self.view addsubview:yourTabbar.view];
Hope this will help you.

xcode tabbed application with loginViewController

I am fairly new to xcode so apologies if I am asking bad questions. My issue is that I have created a tabbed application but would like a login screen to show before the tabs display. There a lots of posts about this and the consensus is that you need to get your tabBarController to present a view controller. This makes sense but for some reason my app is not displaying the login screen. I'm going to paste my appDelegate.m code below. Any help would be much appreciated.
Tks
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(3);
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
LoginViewController *loginViewController = [[[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil] autorelease];
UIViewController *viewController1 = [[[SecondViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[loginViewController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[_tabBarController presentViewController:loginViewController animated:YES completion:nil];
[self.window makeKeyAndVisible];
return YES;
}
You should do the presentation from the controller in the first tab (assuming that's the controller you want to show after the login screen is dismissed). Do the presentation from the viewDidAppear method with the animated argument set to NO.

Presenting Modal View Controller From Xib

I would like to show a modal dialog from a xib. The code that shows my window is:
self.vcSettings = [[ViewControllerSettings alloc] initWithNibName:#"ViewControllerSettings" bundle:[NSBundle mainBundle]];
[self presentModalViewController:self.vcSettings animated:YES];
When this runs though, I get a blank screen, and not what was inside of my ViewControllerSettings.xib. I imagine I'm showing the view incorrectly somehow. Any advice is appreciated.
EDIT:
I think this should be
[self.navigationController presentModalViewController:self.vcSettings animated:YES];
but for some reason self.navigationController is nil.
EDIT:
Self is a UIViewController instantiated in my AppDelegate like so:
UIViewController* viewMain = [[ViewController_iPhone alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = viewMain;
[self.window makeKeyAndVisible];
You need to create a UINavigationController, set it's root view to the XIB controller that you want to present (in your case vcSettings), then present the UINavigationController
self.vcSettings = [[ViewControllerSettings alloc] initWithNibName:#"ViewControllerSettings" bundle:nil];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:vcSettings];
[self.navigationController presentModalViewController:controller animated:YES];

Issue Presenting View modally after a UISplitView loads

I'm new to UISplitView development, so I'm sure there is something obvious I'm doing wrong. I have a basic UISplitView iPad app that loads up with two UITableView controllers when the app launches. This works just fine.
What I am trying to do is immediately upon launch, presenting an "authentication" view modally so that a user will need to login before continuing. Here is the code I have so far which compiles and works without breaking, but the view is not showing.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil];
masterViewController.detailViewController = detailViewController;
masterViewController.managedObjectContext = self.managedObjectContext;
self.window.rootViewController = self.splitViewController;
[self presentAuthenticate];
[self.window makeKeyAndVisible];
applicationDidLaunch = YES;
return applicationDidLaunch;
}
- (void) presentAuthenticate {
AuthenticateViewController *loginController = [[AuthenticateViewController alloc] initWithNibName:#"AuthenticateViewController" bundle:nil];
[loginController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[loginController setModalPresentationStyle:UIModalPresentationFormSheet];
if ([self.splitViewController respondsToSelector:#selector(presentViewController:animated:completion:)]) {
[self.splitViewController presentViewController:loginController animated:NO completion:nil];
} else {
[self.splitViewController presentModalViewController:loginController animated:NO]; //iOS 4 works fine with or without animation
}
}
I defined the AuthenticateViewController as a View with a few textfields in it and have it wired to the File's Owners view.
Thanks ahead of time!
A viewcontroller will not allow to push/present on anotherview unless and until the view is complete loading.
Simple saying we are not allow to call presentModalViewController/pushViewController in a viewcontroller viewDidLoad/viewWillAppear. we need to call this in viewDidAppear.
I had the same issue you said.
Some Solution I can say are,
Do the loading of AuthenticateViewController after [self.window makeKeyAndVisible]; and in a performSelctor (may be with a delay).
Move the code to display AuthenticateViewController in SplitView's DetailView controller viewDidAppear.
thanks,
Naveen Shan

Resources