Set SplitView Controller as root controller after Login IOS - ios7

Hi I am new to IOS development and developing IOS application in which I am using split view controller. My split view controller is not at root position.I am doing login first and then open split view controller. So what I did after user login successful I am changing root view controller. Here is what I did till now:
On sign in button click I am changing root view controller as split view controller like this
- (IBAction)signinAction:(id)sender
{
NSLog(#"inside sign in .... ");
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
delegate.window.rootViewController = [[UserProfileController alloc] init];
[self dismissViewControllerAnimated:YES completion:nil];
}
User profile is my detail view controller for my split view.
#interface UserProfileController : UIViewController<UISplitViewControllerDelegate>
#end
but after click sign in button it opens black window. Don't know whats going wrong. Am I doing some thing wrong? Need Help thank you.

first thing you should change id of your UISplitViewController to Split
after just don't make any connection between your first controller and the split view controller. Then in code switch to it with this code:
- (IBAction)signinAction:(id)sender
{
UISplitViewController *split = [self.storyboard instantiateViewControllerWithIdentifier:#"Split"];
self.view.window.rootViewController = split;
}

Related

Switch from view controller to custom tab bar controller

I have an application that has an initial view controller that allows the user to log in. After the users logs in I'm trying to change the view to a custom tab bar controller that is of class type TabViewController. The problem is that when I switch to the tab bar controller, the screen is black and the bottom tab bar is gray and empty.
Here is some relevant code:
in ViewController.m (initial log in view)
- (IBAction)logInButtonClicked:(UIButton *)sender
{
TabViewController *tabView = [[TabViewController alloc] initWithSession:session];
[self presentViewController:tabView animated:YES completion:nil];
}
in TabViewController.m (class assigned to the tab bar controller)
-(id) initWithSession: (Session*) s
{
self = [super init];
if (self)
{
session = s;
}
return self;
}
Note that when I do the default initialization like so:
TabViewController *tabView = [[TabViewController alloc] init];
I get the same result.
How can I make my tab view controller look like it does in my storyboard on initialization?
Storyboard:
What the tab view controller looks like in the simulator:
I'm not sure this is the best way but it's exact what I did in my last app and it works fine.
Try making the tab bar view controller the root/initial view controller of your app.
According to Apple's developer class reference:
When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
After doing this, set up a modal segue in the storyboard from the tab bar view controller to the login view controller, name it "segueLogin" and call it manually in viewDidAppear method of your tab bar view controller class.
if(!userHasLogin){
[self performSegueWithIdentifier:#"segueLogin" sender:self];
}
its really easy,
i will try to solve your problem in two step.
step 1-- select your TabViewController in storyboard and give it a identifier(below the custome class of TabViewController)
step 2--
- (IBAction)logInButtonClicked:(UIButton *)sender
{
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"Your_Story_Board_Name" bundle:nil];
TabViewController *tabView = [storyBoard instantiateViewControllerWithIdentifier:#"TabViewController_Identifier_From_Storyboard"];
[self presentViewController:tabView animated:YES completion:nil];
}
You should create your's TabViewController with UIStoryboard's - (id)instantiateViewControllerWithIdentifier:(NSString *)identifier
In yours case creating with [[TabViewController alloc] init] is wrong, you doesn't create all tabs programmatically.

How to access Tab View Controller from another View Controller

I'm having trouble accessing my view controllers under the tab bar controller. Here is what my storyboard looks like:
View Controller A (-> Page View Controller -> View Controller C
View Controller A -> Tab Bar Controller (MyTabBarController.h/.m) -> Navigation Controller (MyNavigationController.h/.m)-> View Controller B (TabViewController.h/.m)
Tab Bar Controller (MyTabBarController.h/.m) -> View Controller D
Tab Bar Controller (MyTabBarController.h/.m) -> View Controller E
From View Controller A I have an IBAction called loginButton that is connected to the Tab Bar Controller, and currently it looks like this:
- (IBAction)loginButton:(id)sender {
MyNavigationController *localNavigationController;
UIStoryboard * storyboard = self.storyboard;
MyTabBarController *tbc = [[MyTabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:1];
TabViewController *login = [storyboard instantiateViewControllerWithIdentifier: # "TabViewController"];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:login];
localNavigationController.delegate = self;
[localControllersArray addObject:localNavigationController];
tbc.viewControllers = localControllersArray;
tbc.delegate = self;
tbc.moreNavigationController.delegate = self;
tbc.selectedIndex = 0;
[self presentViewController:tbc animated:YES completion:^{
}];
}
I'm not able to get this displayed correctly. I am getting a bunch of warnings in this piece of code. and it is also not showing the different tab items in the bottom of the Tab Bar, even though I have put images/text on each tab.
So how do I display/access the view controllers inside the Tab Bar Controller correctly? (ie View Controllers C/D/E)?
The storyboard that you show in your question already contains the tab bar controller, navigation controller, and login controller properly hooked up to each other. Because of that, you shouldn't be instantiating a new tab bar controller or navigation controller in code -- they will be instantiated by the storyboard when you instantiate the tab bar controller. So, the only thing you need to do, is to give the tab bar controller in the storyboard an identifier, and do this (assume the identifier is called MyTabBarController):
- (IBAction)loginButton:(id)sender {
UITabBarController *tbc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyTabBarController"];
[self presentViewController:tbc animated:YES completion:nil];
}
You wouldn't even need this code if you control drag from the "Login" button to the tab bar controller, and choose "Modal". That will create a modal segue which will present the tab bar controller with no code at all.
If you just want to select another tab from the tabBar controller then use something like this:
UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
[tabBar setSelectedIndex:3];
Note that if the tabBar controller is the initial view controller you can grab an instance of it it the applicationDidFinishLaunching method and store it in the AppDelegate. Then you'll be able to access it like this:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
Remember to import the AppDelegate.h
I recommand you to use a Singleton shared instance to share multiple informations form multiple controllers.
It's a good design Pattern for you usage.
I'm writing samples of Design Patterns usage on cocoa (see https://github.com/leverdeterre/DesignPatterns -> Real singleton)

Creating and Removing UIViews Programmatically

I am learning how to manipulate view displays programmatically, I manage to display a new view in my appDelegate with the following block of code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navController = [[UINavigationController alloc] init];
loginController = [[LoginController alloc] init];
[navController pushViewController:loginController animated:NO];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES; }
I have added a button in this view that will remove this current view and supposedly programmatically display a new view, however, I only managed to remove the view and not display the new view.
my code to display the second view is the following:
HomeController *homeView = [[HomeController alloc] init];
[self.window addSubview:homeView.view];
[homeView.view release];
Please advise.. I've been searching for hours to no avail, using Switch Views Programmatically, iPhone Views, removeSuperview..
Basically I want to create a simple login flow, at app start I will display my first view (login form), after successful login I want to discard the old view and display the second view which is my home page.
You are on the right track with using the UINavigationController. In fact, you are almost there.
You already have two view controllers - one for the login page, and one for the home page. In the didFinishLaunchingWithOptions:, push both controllers onto the UINavigationController's stack: first the "home" controller, then the "login" one. Once the login controller detects that the login has been successful, call popViewControllerAnimated: or popToRootViewControllerAnimated: to get to the home page.

Modal view after start app does not appear

Before the users can use my app the have to login. My idea was that after the app starts the login view appears modally.
Before iOS 5 I had it all working with .xib files. Now I want to convert the views to a Storyboard for overview and better use of the new functionalities.
The app works with a splitview controller. The problem is that the login view is loaded, but never appears.
I tried it in the app delegate and I made a class for the splitviewcontroller and tried to load it in the viewDidLoad.
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
//load and push login
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
LoginViewController *loginViewController = [storyboard instantiateViewControllerWithIdentifier:#"loginViewController"];
NSLog(#"login push: %#", loginViewController);
[self presentModalViewController:loginViewController animated:YES];
NSLog(#"done push");
}
Log:
2012-01-13 10:18:08.217 App[1101:707] login push: <LoginViewController: 0x472fe0>
2012-01-13 10:18:08.330 App[1101:707] done push
I tried to load it in the Root or detail view, it works but its not the right place and xcode gives the message:
2012-01-13 10:18:08.807 App[1101:707] Unbalanced calls to begin/end appearance transitions for <MainSplitViewController: 0x464bc0>.
My first idea is to begin with the login view and after login push the splitview controller. But I found out that the splitview controller has to be the root view.
To push another ViewController in viewDidLoad is very early. Maybe the current ViewController is presented with animation and you try to present another ViewController with animation ...
You should try moving the display of your LoginController to viewDidAppear ...

How to switch page on iPhone?

HI, everyone,
I want to ask a question about the iPhone application. I am writing a program. The program will ask the user the enter some information and press enter, after that the program will process and get some information, then the program will display the tab page with 3 tabs.
However, I don't know how to switch the page after the user enter the information, I have write the following code.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview: tabBarController.view]; // add the tab page to the view controller
MyViewController *aViewController = [[MyViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];
UIView *controllersView = [myViewController view];
[window addSubview:controllersView]; // add the first page to the windows in order to let the user enter info.
[controllersView release];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
If I add the tabBarController first, the controllersView will not appear, if I change the seuqence, it don't know how to make the controllersView disappear to let the tabBarController appears.
And the process is done in the MyViewController.m. Can I
1) remove the controllersView in the MyViewController.m after I complete all the process and
2) display the tabBarController.view ?
Thank you very much.
You need to use a UINavigationController as the root view-controller for your window. You can then push the first UIViewController (MyViewController) on as the top view.
(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
When its time for the tab view to appear push it onto the Navigation Controllers stack. To get back to your top view pop the ViewController off the stack
[self.navigationController popViewControllerAnimated:YES];