Switching to tabBarController but no tab bar - iphone

I apologize for asking two similar questions but I don't think I worded it correctly the first time and am still struggling to find the answer.
I am in a viewcontroller that is in a project containing a tabBarController. I want to switch from the viewController to one of the viewcontrollers contained in the tabbarcontroller. The problem is it either doesn't show up at all or if I present the normal viewcontroller there is no tab bar.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2,viewController3);
}
I want to switch from my Tag view controller to FirstViewController
Tag.m
-(IBAction)save:(id)sender{
FirstViewController*vc =[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self.tabBarController.viewControllers popToViewController:vc animated:YES];
}
Update:
I solved this by adding my ViewController to a TabBar within my .m file then
[_tabBarController setSelectedIndex:0];
[self presentViewController: _tabBarController animated:YES completion:NULL];
Thanks for the responses I got on this question.

You can change rootViewController property of mainWindow of AppDelegate some thing like this.
AppDelegate * appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDel.window setRootViewController:tabBarController];
Enjoy..

Related

Pushing from One View to Another via UIButton (UINavigationController)

I've tried every solution on Google and nothing seems to work. So far I've implemented a UINavigationController with the App Delegate, now all I want to accomplish is changing to the WebViewController by clicking the UIButton I've created in the interface builder, but the button doesn't seem to do anything when I run the application. Keep in mind that I want it to push to my WebViewController view.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m
- (IBAction)createFile:(id)sender {
WebViewController *webViewController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:NSBundle.mainBundle];
[self.navigationController pushViewController:webViewController animated:YES];
}
Note: In the interface builder I've already connected createFile to the button.
I understand that this is usually something that comes known as super easy but for some reason I've just never gotten it to work. Thanks in advance.
EDIT: I added the retaining property, sythesized it and added to my code in the ViewController.m file:
WebViewController *webViewController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:NSBundle.mainBundle];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:webViewController];
[self.navigationController pushViewController:webViewController animated:YES];
But now the app crashes on the button click and returns with a SIGABRT: "Pushing the same view controller instance more than once is not supported (WebViewController: 0x1ed70e80)"
give this a try
-(IBAction)createFile:(id)sender{
WebViewController *webViewController = [[WebViewController alloc]
initWithNibName:#"WebViewController" bundle:nil];
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:webViewController animated:YES];
}
One thing i noticed is that you are not retaining your navigationController in the appDelegate. So what may be happening is that your the navigationControllers view may be present and retained (from the all to addSubView) but the navigation controller may have been deallocated.
in your AppDelegate try making the Navigation Controller a retaining property
#property (nonatomic, strong) UINavigationController *navigationController
in your .m file
#synthesize navigationController
and then
Edit: Updated for pushing a view controller
WebViewController *webViewController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:NSBundle.mainBundle];
[self.navigationController pushViewController:webViewController animated:YES];

How to code a back button on the a navigation bar using a bar button item

I'm having trouble coding a button to go to the previous page. I was able to do it to go to the next page thinking if I did the same thing but changed it a bit it would work in reverse. Unfortunately, I come up with a lot of errors I can't resolve because it won't allow me to use the release function.
This is this the code that helps it to work going to the next page fine:
#import "ViewController.h"
#implementation ViewController
-(IBAction)btnClicked:(id) sender
{
//add the view of the view controller to the current View---
if (menuView==nil) {
menuView =
[[MenuView alloc] initWithNibName:#"MenuView"
bundle:nil];
}
[self.view addSubview:menuView.view];
}
-(void)dealloc {
[menuView release];
[super dealloc];
}
How do I do it so that a back button will go to the previous page though.
It's pretty simple, use this :
-(IBAction)back:(id) sender
{
[menuView.view removeFromSuperview];
}
But, I would suggest not using addSubview: for many views as it would be complex way to do. Use UINavigationController as #Paul.s suggested.
The way you are doing this is not quite correct and I would suggest doing some reading to get familiar with iOS programming.
Your program structure should be: create a navigation controller (2) to manage the stack of view controllers giving it a viewController (1) to act as it's root.
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// 1
FirstViewController *firstViewController = [[FirstViewController alloc] init];
// 2
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[firstViewController release]; firstViewController = nil;
self.window.rootViewController = navigationController;
[navigationController release]; navigationController = nil;
[self.window makeKeyAndVisible];
return YES;
}
This will display your first view controller inside a UINavigationController. A UINavigationController is responsible for managing a stack of UIViewController's and giving you UI to navigate back down the stack as well as calling all the appropriate presentation related methods on a UIViewController at the correct times e.g. viewDidLoad. You should check out The View Controller Programming Guide for lots of info
Then inside your first view controller you do something like this to respond to the button:
- (IBAction)buttonClicked:(id)sender;
{
SecondViewController *secondViewController = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release]; secondViewController = nil;
}
This creates a new view controller and pushes it onto the stack.

What can I do if my navigationController returns "(null)"? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why can't I push a new view controller onto the current view?
Why is my new view controller not appearing?
I need to push my view controller from my navigationController, but after having done an NSLog statement to find out why nothing was showing with the following code, I realised that it was returning null:
-(IBAction)doChangePasscode{
NSLog(#"Change Passcode Screen Loaded!");
ChangePasscode *cpscreen = [[ChangePasscode alloc] initWithNibName:#"ChangePasscode" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:cpscreen animated:YES];
NSLog(#"%#",self.navigationController);
}
Why is this happening? What can I do to return a proper value other than (null)?
Like everyone has explained in this and Why is my new view controller not appearing?, you need to have a navigation controller first. Use that nav controller to push your view controllers and then your view controllers will no longer have null for the navigationController property.
In this example someSecondViewController would be the self in your code above:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
self.nav = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
[rootViewController release];
[self.window addSubview:nav.view];
[self.window makeKeyAndVisible];
}
- (void)someOtherMethod {
SecondViewController *someSecondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.nav pushViewController:someSecondViewController animated:YES];
[someSecondViewController release];
}
Please review these:
View Controller Programming Guide for iOS
UINavigationController Docs
UIViewController Docs
I am assuming you mean returning nil, that means the viewController does not have a navigation controller, you can solve it by having your viewController be the root of a newly created UINavigationController like this:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myRootViewController];
Then for myRootViewController self.navigationController will return the navigation Controller.
I am assuming that viewController is the first one in your app so you want to have this on your appDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyViewController *viewController = [[[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil] autorelease];
UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
self.window.rootViewController = myNavController;
[self.window makeKeyAndVisible];
return YES;
}
Then if that IBAction is on MyViewController self.navigationController will return the navigation Controller instead of nil.
Hope that helps.

iphone: need to implement navigationController inside viewcontroller which appears after selecting a tab in tabbar

I am pretty new to UITabBarController. I was trying to provide a navigation system in a viewController corresponding to a tab in tabViewController
created an instance of navigation controller in viewDidLOad
[testLabel setText:#"Test"];
self.navigator=[[UINavigationController alloc] initWithRootViewController:self];
[super viewDidLoad];
on button click I do this
NSLog(#"I am here");
StartWordPickerVC *aStartWordPickerVC=[[StartWordPickerVC alloc] initWithNibName:#"StartWordPickerVC" bundle:nil];
[self.navigator pushViewController:aStartWordPickerVC animated:YES];
[aStartWordPickerVC release];
But when I click button nothing happens
Can you please help me out in this
Thanks
Just add this in AppDelegate.h
UINavigationController *navigationController;
Just add this in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
}

Adding UINavigationController to existing UIViewController

How do I add an existing UIViewController (which is presented using presentModalViewController) to a UINavigationController?
When user tap on button, a new copy of my detail view need to be pushed. (In other words, pushViewController displaying pushViewController, modally, in a UINavigationController).
Easiest way to enable this functionality?
how do you create your modal viewcontroller? Just wrap the controller into a UINavigationController
Let's assume your modal code is like this:
MyExampleViewController *vc = [[[MyExampleViewController alloc] initWithNibName:#"MyExample" bundle:nil] autorelease];
[self presentModalViewController:vc animated:YES];
Then change it into something like this:
MyExampleViewController *vc = [[[MyExampleViewController alloc] initWithNibName:#"MyExample" bundle:nil] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
[self presentModalViewController:navController animated:YES];
I think you need to add a navigation controller in your delegate, after that you can push the view. So that you can push the view from anywhere in your application.
on AppDelegate.h
UINavigationController *navig;
on AppDelegate.M
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navig = [[UINavigationController alloc] initwithRootViewController:viewController.view];
//[navig pushViewController:viewController animated:YES];
[self.window addSubview:navig.view];
[self.window makeKeyAndVisible];
return YES;
}