I have successfully shown a view upon launch that authenticates a user. Upon success, I want the presentModalViewController to no longer be visible and deallocate properly.
My code is as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the tab bar controller's view to the window and display.
[self.window addSubview:tabBarController.view];
Overview *overview = [[Overview alloc] initWithNibName:#"Overview" bundle:nil];
[self.tabBarController presentModalViewController:overview animated:YES];
[overview release];
[self.window makeKeyAndVisible];
return YES;
}
In your modal viewcontroller you need a piece of code doing:
[self dismissModalViewControllerAnimated:YES];
Related
I need a login viewcontroller,when log in,entry the tabbar controller,and the view in tabbarcontroller should get the data from the login viewcontroller and change the navigationbar(draged from nib)'s title,for example,show the user's name.I did it like :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[self.window addSubview:_tabbarController.view];
[self.window addSubview:_loginViewController.view];
[self.window makeKeyAndVisible];
return YES;}
I add two subview,so when I remove the loginviewcontroller,the root shows,in the LoginViewController.m,I did it like:
-(IBAction)ShangHaiButtonPressed:(id)sender{
[self.view removeFromSuperview];}
so how do i pass the value in login view to my tabbarcontroller?
Before remove from superview pass the value to tabbarcontroller
-(IBAction)ShangHaiButtonPressed:(id)sender{
[_tabbarController setUsername: [_loginViewController username]];
[_tabbarController setPassword: [_loginViewController password]];
[self.view removeFromSuperview];
}
I think it is not appropriate to remove the view directly. Why not use [_tabbarController presentModalViewController:_loginViewController]. and Dismiss it after button is pressed.
I have a viewcontroller containing a TabController. Before this loads, I want a user to login so I can check what they have access to. In my AppDelegate, bot the rootViewController (with the tabs) and the LoginViewController are declared, and they're also wired up in IB:
I have this in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// [window addSubview:[rootController view]];
[window addSubview:[loginViewController view]];
[self.window makeKeyAndVisible];
return YES;
}
My plan was to dismiss the login form after authenticating and show the rootController, but the rootController displays straight away. I was going to do:
-(IBAction)DidClickLoginButton:(id)sender {
NotesAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.window addSubview:[delegate.rootController view]];
[self dismissModalViewControllerAnimated:YES];
}
Is there an easier way to do this? I can't see why the LoginViewController isn't presented.
EDIT: Eventually got this working by adding it to the rootController in my AppDelegate's didFinishLaunchingWithOptions method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// [window addSubview:[rootController view]];
[self.window makeKeyAndVisible];
LoginViewController *loginViewController =[[LoginViewController alloc] initWithNibName:#"LoginView" bundle:nil];
[self.rootController presentModalViewController:loginViewController animated:YES];
return YES;
}
I think it's actually much easier to do things 100% programmatically rather than with Interface Builder. Either way, in application:didFinishLaunchingWithOptions:, you want to do something like this:
[rootViewController presentModalViewController:loginViewController animated:NO];
Then, after the user logs in, do:
[rootViewController dismissModalViewControllerAnimated:YES];
Yes it would. As you are adding the root controller view on the window, and this would make it appear above all (even above the login view) and then your login view gets dismissed behind the root view, which you cannot see.
EDIT:
One of the approach would be to have login controller above root in the beginning itself (root view controller presenting login view) and then happily dismiss the login view.
I need some help with modal view controllers, as I have not used Modal View Controllers before...
So this is how my application now is...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[_window addSubview:rootController.view];
rootController.selectedIndex =2;
NSLog(#"Displaying the APP delegate");
[self.window makeKeyAndVisible];
return YES;
}
I have a view for Login, titled LoginViewController . I want this to appear first as the first view, and on clicking the Login button (IBAction), I want it to show rootController.selectedIndex =2;
(Please ignore the login check as of now). I just want the login view controller to appear at first, and dismiss itself if I press Login, and then take the screen to my rootController (which is a UITabBarController)
In the viewDidLoad method of the first view in the tabBar present the modal view as follows :
LoginViewController *lvc = [[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:lvc animated:YES];
[lvc release];
To dismiss the modalView, in the IBAction of the login button just put in the following code :
[self dismissModalViewControllerAnimated:YES];
I'm trying to display an "About Page" in my application when pressing a button, but when I press the UIButton, the NSLog(#"Button pressed") is called, but the view won't load. Here is my code:
- (IBAction)pushAbout {
NSLog(#"Button pressed");
AboutView * aboutView = [[AboutView alloc] initWithNibName:#"AboutView" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[aboutView release];
}
I've seen this code in an example, but it was called in a UITableViewController instead of an ordianry UIView controller.
The class files "AboutView" were created as UIViewController subclass. They were created and left untouched
A guess: Your view is not in a UINavigationController, hence
self.navigationController
is actually nil, and nothing happens.
The solution would be to place the main view in a nav controller, and adding the nav controller's view to your application's main window.
A simple way of doing this:
In your app delegate, change
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController* navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
Or similar. Note that this is just to get you started, and maybe not the nicest way of doing it. (I assumed you've just created a view-based application in xcode.)
Yes you don't have a UINavigationController set.
Add
NSLog(#"nav? %#", self.navigationController);
to your action and you'll see that it dumps (null).
However, the AboutView.xib works fine if you enter this code:
[self presentModalViewController:aboutView animated:YES];
instead of
[self.navigationController pushViewController:aboutView animated:YES];
The view will show up. In you zipped example the AboutView.xib didn't contain a label, so don't wonder if it turns out to be a white page.
You can dismiss the presented modal view by using
[self dismissModalViewControllerAnimated:YES];
in your AboutViewController.
To get your hands on a UINavigationController I suggest creating an app with the Navigation-Based Application template.
I have custom view controller named DRTableViewController
In my app delegate, I use the following function to load
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
tvc = [[DRTableViewController alloc] init]; // tvc is created with xib
navCon = [[UINavigationController alloc] initWithRootViewController:tvc];
[self.window addSubview:[navCon view]];
[navCon release];
[self.window makeKeyAndVisible];
return YES;
}
but when I start my application, navigation controller appears but the view inside it is black,
when I use
[self.window addSubview:[tvc view]];
instead of [navCon view]; I can see my view without any problem
Thanks in advance
You need to retain your navigation controller so it is not released.
Create a property for you navigation controller and retain it in the application delegate.
A quick fix is to comment out the line,
[navCon release]
But this will introduce a memory leak.