I need the ability to bypass the default RootViewController you get when you pick a navigation controller project type in XCode. I need this because I want to go down a different path depending on whether the app has been configured (sign up/login screens if not). Could someone point me to an example where in the AppDelegate the NavigationController is hooked to another controller (in this case SignupController) via code?
Here is what I have, but it doesn't let me change the title. And in the MainWindow.xib, it's still tied into the default RootViewController.
(void)applicationDidFinishLaunching:(UIApplication *)application {
[[UIApplication sharedApplication]
// if no config, load up the SignupController
SignupController* signupController = [[SignupController alloc] initWithNibName:#"SignupController" bundle:nil];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
You can do something like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
if( hasConfig ) {
[window addSubview:[navigationController view]];
} else {
SignupController *signupController = [[SignupController alloc] initWithNibName:#"SignupController" bundle:nil];
[window addSubview:signupController.view];
[signupController release];
}
[window makeKeyAndVisible];
It becomes awkward to switch back to your navigation controller once sign in is complete. (Assuming you are showing a sign in screen.)
Why not use a modalviewcontroller?
In your RootViewController.m:
- (void)viewDidAppear {
[super viewDidAppear];
if( notLoggedIn ) {
SignupController *signupController = [[SignupController alloc] initWithNibName:#"SignupController" bundle:nil];
[self presentModalViewController: signupController animated:YES];
[signupController release];
}
}
SignupController.m
- (void)didSignInOk {
//this will dismiss the sign in screen
[self.parentViewController dismissModalViewController];
}
Related
In delegate class I wrote the code as follows
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
[self generateFirstScreen];
[self removeFirstScreen]; // On login check implement this method or u can directly write the snippet here as well.
[self prepareControllersOnTabs]; //your tab controller code function
[self.window makeKeyAndVisible];
return YES;
}
-(void) removeFirstScreen
{
[firstScreen removeFromSuperView];
self.window.rootViewController = self.tabBarController;
[firstScreen release];
}
-(void) generateFirstScreen
{
FirstScreen *firstScreen = [[FirstScreen alloc]init];
[self.navigationController pushViewController:firstScreen animated:YES];
[firstScreen release];
}
but generateFirstScreen works fine but removeFirstScreen gives an exception Please help me.
Specify exception...
Without addSubview how can you remove it from super. Do you want to use popViewController.?
Again you are allocating firstScreen only once & releasing it twice..!
Don't remove a screen if you're not sure it's added to a view, otherwise you get a crash.. you can specify a tag to this view and check the subviews of the main view to check whether your view is in there somewhere..
your generateFirstScreen method Change like below
FirstScreen *firstScreen = [[FirstScreen alloc]initWithNibName:#"FirstScreen" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:self.objLogin];
// Add the navigation controller's view to the window and display.
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
//call this metod when ever u want tabbar
-(void)tabBarControllerView
{ [self.navigationController.view removeFromSuperview];
[self.navigationController.view setHidden:YES] ;
self.tabBarController.selectedIndex = 0;
self.tabBarController.view.hidden=NO;
[window addSubview:self.tabBarController.view];
}
In my app delegate I load a view controller on top of my tabbar. This controller had three buttons on it, one to navigate to each tab. When the second button is pressed, I want to dismiss the view controller and go to the second tab. But this doesn't seem to work the normal way.
My AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//-- Insert a delay of 5 seconds before the splash screen disappears
[NSThread sleepForTimeInterval:3.0];
// Set the tab bar controller as the window's root view controller and display.
self.window.rootViewController = self.tabBarController;
// Set StartView to load first
StartViewController *startViewController = [[StartViewController alloc] initWithNibName:#"StartView" bundle: nil];
[window addSubview: [startViewController view]];
[window makeKeyAndVisible];
[self.tabBarController presentModalViewController:startViewController animated:NO];
[startViewController release];
return YES;
}
And here is my current IBAction, which doesn't seem to work:
- (IBAction) toSecondView:(id)sender
{
// Show status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
[(UITabBarController *)self.parentViewController setSelectedIndex:1];
[self dismissModalViewControllerAnimated:NO];
}
I tried these too, without success:
self.tabBarController.selectedIndex = 1;
and
[self.tabBarController setSelectedIndex:1];
Can anyone help me out and explain me what I'm missing?
This happend because a below of reason.
You have added ViewController Onto the Window As a subView, no need to add SubView because you are already presenting that ViewController as ModalViewController.
Please Try as below.
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//-- Insert a delay of 5 seconds before the splash screen disappears
[NSThread sleepForTimeInterval:3.0];
// Set the tab bar controller as the window's root view controller and display.
self.window.rootViewController = self.tabBarController;
// Set StartView to load first
StartViewController *startViewController = [[StartViewController alloc] initWithNibName:#"StartView" bundle: nil];
//[window addSubview: [startViewController view]]; no need to add subView here
[window makeKeyAndVisible];
[self.tabBarController presentModalViewController:startViewController animated:NO];
[startViewController release];
return YES;
}
-(IBAction) toSecondView:(id)sender
{
// Show status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
//create delegate's class object for accessing tabBarController
AppDelegate* delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
//instead of [(UITabBarController *)self.parentViewController setSelectedIndex:1];
//delegate.tabBarController your tabBarControler at which you have added viewController
[delegate.tabBarController setSelectedIndex:1];
[self dismissModalViewControllerAnimated:NO];
}
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'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'm wanting to have my app (on launch) load a view that I have created over the top of the original view, then when a button is clicked the top view will disappear and show the main view underneath. I know this is terribly simple, but how would I do this? Maybe push the view in viewDidLoad?
Use a navigation controller
MyViewController *myView = [[MyViewController alloc] init];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:myView];
My2ndView *secondView = ....
[navControl pushViewController:secondView animated:NO];
[window addSubView:navControl.view]
This can be incredibly simple. When the user clicks the button, just do this
secondViewController.view.hidden = YES;
Just add the two views in your delegate, and do that in your delegate.
If you prefer, just do it "in" secondViewController! self.view.hidden = YES;
It sounds like you're just doing something simple ...... no need to bother with a view controller.
You ask how to display the second view, just like this ..
-(BOOL)application:(UIApplication *)applic`ation
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[window addSubview:yourMainView.view];
[window addSubview:theTemporaryView.view]; // it goes on top
[window makeKeyAndVisible];
application.idleTimerDisabled = YES;
return YES;
}
-(BOOL)eliminateTempView // example, when the user clicks on the button
{
theTemporaryView.view.hidden = YES;
[theTemporaryView release];
}
Hope it helps!