How to Show LoginViewController over SplitViewController? - ios

I am working on application for iPad which have CustomTabBar (3 Tabs), Navigation Bar and SplitView Controller.
Follow of application screens:
1-Login Screen (Present over SplitView) >> Forgot password (Push on login navigation)
2- Forget password if pushed on login navigation then it will be poped and then login will be dismissed on successful login.
3- On Successful Login, SplitView is shown.
How I code in AppDelegate:
self.detailViewController = [[[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:self.detailViewController] autorelease];
customTabBarCont = [[CustomTabBarController alloc] init];
self.splitViewController = [[[UISplitViewController alloc] init] autorelease];
self.splitViewController.delegate = self.detailViewController;
NSArray *viewControllers = [[[NSArray alloc] initWithObjects:customTabBarCont,detailNavigationController, nil] autorelease];
self.splitViewController.viewControllers = viewControllers;
self.detailViewController.splitViewController = self.splitViewController;
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
loginVC = [[[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil] autorelease];
UINavigationController *loginNavigationController = [[[UINavigationController alloc] initWithRootViewController:loginVC] autorelease];
[self.splitViewController presentViewController:loginNavigationController animated:NO completion:nil];
I have SettingViewController in which i have given the functionality of Sign-out.
It create issues when user Signout from Portrait and Login in LandscapeView
How i code for SignOut
self.loginVC = [[[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil] autorelease];
if ([app_delegate.detailViewController.masterPopoverController isPopoverVisible]) {
[app_delegate.detailViewController.masterPopoverController dismissPopoverAnimated:NO];
}
UINavigationController *loginNavigationController = [[[UINavigationController alloc] initWithRootViewController:self.loginVC] autorelease];
When we rotate again then everything goes well. but how to solve this issue. or where i am wrong.

You just have to add one method in your DetailViewController.m
- (BOOL)splitViewController:(UISplitViewController*)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
return NO;
}
The run you project and see what happens. Surly this will solve your problem

Related

call method of different view controller in app delegate while a pushing new controller

I am working on a chat application and on successful login I want to push view a controller and show tab bar to user.
following is the code I have used for it
This is my TabBarViewCtr
appdel=(TabBarAppDelegate *)[[UIApplication sharedApplication]delegate];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
nextFirstViewCtr = [[FirstViewCtr alloc] initWithNibName:#"FirstViewCtr" bundle:nil];
nextSecondViewCtr=[[SecondViewCtr alloc] initWithNibName:#"SecondViewCtr" bundle:nil];
nextThirdViewCtr=[[ThirdViewCtr alloc] initWithNibName:#"ThirdViewCtr" bundle:nil];
nextForthViewCtr=[[ForthViewCtr alloc] initWithNibName:#"ForthViewCtr" bundle:nil];
nextsixviewctr=[[sixviewctr alloc] initWithNibName:#"sixviewctr" bundle:nil];
}
else
{
nextFirstViewCtr = [[FirstViewCtr alloc] initWithNibName:#"FirstViewCtr-iPad" bundle:nil];
nextSecondViewCtr=[[SecondViewCtr alloc] initWithNibName:#"SecondViewCtr-iPad" bundle:nil];
nextThirdViewCtr=[[ThirdViewCtr alloc] initWithNibName:#"ThirdViewCtr-iPad" bundle:nil];
nextForthViewCtr=[[ForthViewCtr alloc] initWithNibName:#"ForthViewCtr-iPad" bundle:nil];
nextsixviewctr=[[sixviewctr alloc] initWithNibName:#"sixviewctr-iPad" bundle:nil];
}
[nextFirstViewCtr setTitle:#"1st tab"];
[nextSecondViewCtr setTitle:#"2nd tab"];
[nextThirdViewCtr setTitle:#"3rd tab"];
[nextForthViewCtr setTitle:#"4th tab"];
[nextsixviewctr setTitle:#"5th tab"];
nav1 = [[UINavigationController alloc] initWithRootViewController:nextFirstViewCtr];
nav2 = [[UINavigationController alloc] initWithRootViewController:nextSecondViewCtr];
nav3 = [[UINavigationController alloc] initWithRootViewController:nextThirdViewCtr];
nav4 = [[UINavigationController alloc] initWithRootViewController:nextForthViewCtr];
nav5 = [[UINavigationController alloc] initWithRootViewController:nextsixviewctr];
nav1.navigationBarHidden=YES;
nav2.navigationBarHidden=YES;
nav3.navigationBarHidden=YES;
nav4.navigationBarHidden=YES;
nav5.navigationBarHidden=YES;
MainTabBar = [[UITabBarController alloc] init];
MainTabBar.delegate = self;
[MainTabBar setViewControllers:[NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nav5,nil]];
MainTabBar.view.frame=self.view.frame;
MainTabBar.selectedIndex=0;
[self.view addSubview:MainTabBar.view];
In the login view controller i have called connect method which is in app del and this works fine, on call of the connect method and for correct credential the method which is in app del for authentication get called.
The authentication method is as follows it is in TabBarAppDelegate.
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
DDLogVerbose(#"%#: %#", THIS_FILE, THIS_METHOD);
[self goOnline];
LoginPassViewController *obj_login = [[LoginPassViewController alloc] init];
[obj_login nextOne];
}
The nexone method looks like following which is in the loginpassviewcnt. It also get called but view is not pushed.
this method is in LoginPassViewController and get called from TabBarAppDelegate
-(void)nextOne{
TabBarViewCtr *MainTab;
if(MainTab==nil){
MainTab=[[TabBarViewCtr alloc] initWithNibName:#"TabBarViewCtr" bundle:nil];
}
[self.navigationController pushViewController:MainTab animated:YES];
}
When the nextOne method is called form LoginPassViewController it does works as required, but not when it called from TabBarAppDelegate .
I am not getting any error or even application is also not crashing can anyone please let me know how to do this.
Thanks in advance.

Put a navigation controller in a tabbar controller

I have tried to add a navigation controller to a tab bar controller but the tab appear black.
My code:
AppDelegate.m
#import "SettingsNavigationControllerViewController.h"
#import "SettingsViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2, *viewController3;
SettingsNavigationControllerViewController *viewController4;
UINavigationController *navigationController = [UINavigationController alloc];
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
viewController3 = [[ShareViewController alloc] initWithNibName:#"ShareViewController" bundle:nil];
SettingsViewController *settingViewController = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
viewController4 = [[SettingsNavigationControllerViewController alloc] initWithRootViewController:settingViewController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, navigationController];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
thank you in advance
This is because you added a navigationcontroller with no RootViewController and even doesnt initialized. Thats why the view appears as black. May be you mean to add viewController4 instead of navigationController . So change your code as below
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, viewController4];
You need to first create the navigation controllers with rootviewcontrollers
viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
Then instantiate the Tab Bar controller like this
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];
Create Navigation Controllers first.
UINavigationController *nav1 = [[UINavigationController alloc] init];
UIViewController *viewController1 = [[[FirstSteps alloc] initWithNibName:#"FirstView" bundle:nil] autorelease];
nav1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
UINavigationController *nav2 = [[UINavigationController alloc] init];
UIViewController *viewController2 = [[[Profiles alloc] initWithNibName:#"SecondView" bundle:nil] autorelease];
nav2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
Then initialize your tabbar controller like this.
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2 ,nil];
self.window.rootViewController=self.tabBarController;
Hope this helps you.
Hey mate please visit the below link for tab bar example.
I think this is good and nice example which would be helpful to you.
https://github.com/alikaragoz/AKTabBarController
You only have to set your controllers name in appDelegate.m file

How to deal with a presentModal inside a tabBar that needs to use navigationController

I'm trying to push a view controller inside a presentmodal but nothing happening. So, I have a loginController as rootViewController that push a tabBarControler then inside one of tabBar views I open a presentModal. In the presentModal I have an UITableView when clicked push another view but when I try do this nothing happen. To try be more clear here's the hierarchy:
LoginViewController(as root view controller) >>>push>>> UITabBarController(HomeViewController and two others) >>>open>>> PackViewController(presentModal) that needs to open another view by pushViewController:
The code:
AppDelegate.m
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
LoginViewController_iPhone *loginViewController = [[LoginViewController_iPhone alloc] init];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:loginViewController] autorelease];
self.window.rootViewController = self.navigationController;
LoginViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
HomeViewController_iPhone *referenciaHomeViewController = [[HomeViewController_iPhone alloc] init];
MapaViewController_iPhone *referenciaMapaViewController = [[MapaViewController_iPhone alloc] init];
GuiaViewController_iPhone *referenciaMidiaViewController = [[GuiaViewController_iPhone alloc] init];
UINavigationController *navHome = [[UINavigationController alloc] initWithRootViewController:referenciaHomeViewController];
UINavigationController *navMapa = [[UINavigationController alloc] initWithRootViewController:referenciaMapaViewController];
UINavigationController *navGuia = [[UINavigationController alloc] initWithRootViewController:referenciaMidiaViewController];
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:#"tabBarBackground"]];
[self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"tabBarSelected"]];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navHome, navMapa, navGuia, nil];
}
- (void)login {
[self.navigationController pushViewController:self.tabBarController animated:YES];
}
PackViewController.m (PRESENTMODAL):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
CompraPackViewController_iPhone *controller = [[CompraPackViewController_iPhone alloc] init];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
When you do your present modal, you need to present a navigation controller whose root view controller is the table view controller.

Change the selected TabBar index on button click

In my app I have 4 tabs, on the first Tab : I have 4 views. on second view,there is one button which will open the third tab view. But my selected tab Index is One. How can I chage it to Third.
My code for tab bar is in APPDelegate as follow:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *nav1 = [[UINavigationController alloc] init];
UIViewController *viewController1 = [[[CarAccidentAppViewController alloc]
initWithNibName:#"CarAccidentAppViewController_iPhone" bundle:nil]
autorelease];
nav1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
//for steps tab...
UINavigationController *nav2 = [[UINavigationController alloc] init];
UIViewController *viewController2 = [[[FirstSteps alloc] initWithNibName:#"FirstSteps" bundle:nil] autorelease];
nav2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
//for profiles tab...
UINavigationController *nav3 = [[UINavigationController alloc] init];
UIViewController *viewController3 = [[[Profiles alloc] initWithNibName:#"Profiles" bundle:nil] autorelease];
nav3.viewControllers = [NSArray arrayWithObjects:viewController3, nil];
//for contact us tab...
UINavigationController *nav4 = [[UINavigationController alloc] init];
UIViewController *viewController4 = [[[ContactUs alloc] initWithNibName:#"ContactUs" bundle:nil] autorelease];
nav4.viewControllers = [NSArray arrayWithObjects:viewController4, nil]; self.tabBarController = [[[UITabBarController alloc] init]
autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4 ,nil];
self.window.rootViewController=self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
In Swift 2, you can just call:
self.tabBarController?.selectedIndex = 0
The simplest way to programatically switch tabs within the app is by doing this ..
[self.tabBarController setSelectedIndex:3];
I use this in one of my apps and it works extremely well.
Plasma
On Button Click
[self.navigationController pushViewController:YourView animated:YES];
[self.tabBarController setSelectedIndex:index];
self.tabBarController.selectedViewController=[self.tabBarController.viewControllers objectAtIndex:3];
Try this. This will helpful for you.

iOS5 SplitViewController leaves BarButton visible while in landscape view on app startup

I have a UISplitViewController with a UIViewController as master and a UINavigationController as my details controller (which contains an actual DetailsController as it's rootController).
In iOS5, at app startup (holding the device in landscape view), I add the splitViewController's view to my window but then I present a loginController on top of the splitViewController like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
KRMasterViewController *masterViewController = [[[KRMasterViewController alloc] initWithNibName:#"KRMasterViewController" bundle:nil] autorelease];
UINavigationController *masterNavigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
KRDetailViewController *detailViewController = [[[KRDetailViewController alloc] initWithNibName:#"KRDetailViewController" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.splitViewController = [[[UISplitViewController alloc] init] autorelease];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil];
[self.window addSubview:self.splitViewController.view];
LoginController *controller=[[LoginController alloc]
initWithNibName:#"LoginController" bundle:nil];
[self.splitViewController presentModalViewController:controller animated:false];
[self.window makeKeyAndVisible];
return YES;
}
As you can see the detailsController is my splitViewController's delegate. The problem is in iOS4, before the loginController gets displayed, the delegate method:
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)popoverController
is called then when I dismiss the loginController the delegate method:
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
gets called. I guess iOS realizes really late that I'm in landscape but figures out before I got to the detailController so everything was cool. In iOS 5, the second method does not get called by the time I get to the splitViewController. This means I'm left with the barButtonItem visible in landscape view. Funny enough, if I rotate to portrait then back to landscape, the methods gets called properly from then on. Anyone ever experienced this before? Any solutions?
I've had similar problem. After app launch I present Login modalVC. But when I dismiss it, the BarButtonItem in detailViewController is still visible.
Just use
[self performSelector:#selector(presentLogin) withObject:nil afterDelay:0.1]
and it will magically start working.
I ended up switching the root controller from being a navigation controller (when logging in) to a splitview controller for the main menu:
-(void)goToLogin{
self.rootSplitController=nil;
UINavigationController* navController=[[UINavigationController alloc]init];
navController.navigationBarHidden = true;
self.rootNavController=navController;
[navController release];
LoginController *loginController=[[LoginController alloc]init];
[self.rootNavController pushViewController:loginController animated:false];
[loginController release];
[self.window addSubview:self.rootNavController.view];
}
-(void)goToMain{
self.rootNavController=nil;
MasterController *masterViewController = [[[MasterController alloc]
initWithNibName:#"MasterController" bundle:nil] autorelease];
UINavigationController *masterNavigationController = [[[UINavigationController alloc]
initWithRootViewController:masterViewController] autorelease];
masterNavigationController.navigationBarHidden=true;
DetailsController *detailViewController = [[[DetailsController alloc]
initWithNibName:#"DetailsController" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc]
initWithRootViewController:detailViewController] autorelease];
detailNavigationController.navigationBarHidden=true;
self.rootSplitController = [[[UISplitViewController alloc] init] autorelease];
self.rootSplitController.delegate = detailViewController;
self.rootSplitController.viewControllers = [NSArray arrayWithObjects:
masterNavigationController, detailNavigationController, nil];
[self.window addSubview:self.rootSplitController.view];
}

Resources