Navigation bar not changing views - ios

I'm doing an app with a Nav bar which will switch between the first and third views (the second and the first ones will be switched by a tab bar).
In the FirstViewController.h:
#property(strong,nonatomic) ThirdViewController *thirdViewController;
In the viewDidLoad method of the FirstViewController I made it:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Model"
style:UIBarButtonItemStylePlain target:self action:#selector(goToThirdView:)];
And also...
- (void)goToThirdView:(id)sender
{
ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
self.thirdViewController = thirdViewController;
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.view removeFromSuperview];
[self.view insertSubview:self.thirdViewController.view atIndex:0];
[UIView commitAnimations];
}
My AppDelegate is looking like this:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
But it's not working, when I click in the button, nothing happens. Any idea ?
Thank you in advance.

You should leave the selector for the button as goToThirdView:, but make that method look like this:
- (void)goToThirdView:(id)sender {
ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
[self.navigationController pushViewController:thirdViewController animated:YES];
}

Related

navigation controller without using story boards

SecondViewController *testAppViewController2Obj =[[SecondViewController alloc]initWithNibName:#"TestAppViewController2" bundle:nil];
[self.navigationController pushViewController:testAppViewController2Obj animated:YES];
ContainerViewController *container = [[ContainerViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:container];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 65)];
container.navigationItem.title = #"Frames";
self.window.rootViewController = nav;
i hope it works
In order to push a new view controller you should first create a view controller, VC1, link it with a navigation controller and then link the navigation controller to the rootViewController in the AppDelegate
#AppDelegate
FirstViewController *VC1 = [FirstViewController new];
UINavigationController *nVC = [UINavigationController alloc] initWithRootViewController:VC1];
self.window.rootViewController = nVC;
Then when VC1 gets pushed, you can create another view controller and push it like this:
#Now in VC1
SecondViewController *VC2 = [SecondViewController new];
[self.navigationController pushViewController:VC2 animated:YES];

Show UITabBar without beeing RootViewController

I'm having an LoginViewController which is the RootViewController. This ViewController should not have the TabBar included. The Rest of the ViewController should contain the UITabBar, But it is not showing. If i make the tabBar the rootController it will show the tabBar in the viewcontrollers. This would also make the firsttab the rootviewcontroller which it should not.
My question is then how can i make the loginview the rootViewController without an tabBar and still show the tabBar in the other viewcontrollers?
My code:
tabBarController = [[UITabBarController alloc] init];
MenuViewController *firstTab = [[MenuViewController alloc] initWithNibName:#"MenuViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstTab];
FixtureViewController *secondTab = [[FixtureViewController alloc] initWithNibName:#"FixtureViewController" bundle:nil];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondTab];
WorldCupViewController *thirdTab = [[WorldCupViewController alloc] initWithNibName:#"WorldCupViewController" bundle:nil];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:thirdTab];
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:loginView];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[navController, navController2, navController3];
navController.tabBarItem.image = [UIImage imageNamed:#"message-7"];
navController2.tabBarItem.image = [UIImage imageNamed:#"football-32"];
navController3.tabBarItem.image = [UIImage imageNamed:#"trophy-32"];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:110/255.0f green:89/255.0f blue:196/255.0f alpha:1.0f]];
[self.window setRootViewController:navController4];
[self.window makeKeyAndVisible];
What you need to do is transition the application from the LoginViewController to the Tabbar. What I would suggest doing is replacing the LoginViewController with the TabBar as the rootViewController.
Here is some sample code, perform this action in your AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:loginView];
[self.window setRootViewController:navController4];
[self.window makeKeyAndVisible];
return YES;
}
-(void)transitionToTabBar
{
// Set the TabBar how you are in your sample code, this is just an example.
[self.window setRootViewController:[[UITabBarController alloc] initWithNibName:#"SomeNib" bundle:Nil]];
[UIView transitionWithView:self.window duration:0.5f options:UIViewAnimationOptionTransitionCurlDown animations:^{
[self.window makeKeyAndVisible];
} completion:nil];
}
You should use main View controller (for MenuViewController) and by using segue you can present another view controller which has TabBar in it.

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

Add a new viewcontroller to a tab controller

I have a tab bar controller created by a MainWindow.xib. I have 4 view controllers in it. Now i want to add a 5th item programatically (because I dont know which class I will have to use until compile time)
This is my code:
UIViewController * login = [[LoginUserViewController alloc] initWithNibName:#"LoginUserViewController" bundle:nil];
NSMutableArray * viewControllersArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[viewControllersArray addObject:login];
[self.tabBarController setViewControllers:viewControllersArray
animated:YES];
But I get
[LoginUserViewController viewControllers]: unrecognized selector sent to instance 0x95791b0'
When I reach this code
UINavigationController *navController = [tabBarController.viewControllers lastObject];
LoginViewController * log = [navController.viewControllers objectAtIndex:0];
Where am I going wrong? Any ideas?
Thanks a lot
If this is all your code, it does not look like you are instantiating the navigation controller. Look at:
initWithRootViewController:
in the UINavigatorClass. I would replace:
UINavigationController *navController = [tabBarController.viewControllers lastObject];
with:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: [tabBarController.viewControllers lastObject]];
Edit:
One more thoughts:
Looks like you lucked out on the "tabBarController" property since you might have synthesized it as #synthesize tabBarController=tabBarController;
I strongly recommend you use the latest version of XCode which will automatically do the #synthesize for you. The line before last in your code should be self.tabBarController
You forgot that last tab is LoginUserViewController and not an instance of UINavigationController.
After adding LoginUserViewController to tab bar controller, your last view controller in tab bar controller's array will be LoginUserViewController and not an instance of UINavigationController
UINavigationController *navController = [tabBarController.viewControllers lastObject];
Hence above line will return LoginUserViewController's object in navController variable.
RecordsViewController *recordsViewController = [navController.viewControllers objectAtIndex:0];
Hence above line will cause crash as LoginUserViewController don't have viewControllers property.
Try this....
- (void) setUpTabBar {
FirstViewController *firstViewController = [[FirstViewController alloc]init];
firstViewController.title = #"First View";
firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
SecondViewController *secondViewController = [[SecondViewController alloc]init];
secondViewController.title = #"Second View";
secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];
ThirdViewController *thirdViewController = [[ThirdViewController alloc]init];
thirdViewController.title = #"Third View";
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];
ForthViewController *forthViewController = [[ForthViewController alloc]init];
forthViewController.title = #"Forth View";
forthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2];
UINavigationController *forthNavController = [[UINavigationController alloc]initWithRootViewController:forthViewController];
tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, forthNavController, nil];
tabBarController.delegate = self;
[self sizeViewToAvailableWindow:[tabBarController view]];
[firstNavController release];
[firstViewController release];
[secondNavController release];
[secondViewController release];
[thirdNavController release];
[thirdViewController release];
[forthNavController release];
[forthViewController release];
}

The correct way of adding a UINavigationController to a existing UIViewController

I'm trying to add a UINavigationController to my existing view controller by adding this in ViewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
navController = [[UINavigationController alloc]init];
[self.view addSubview:navController.view];
}
But by doing it this way, it completely blocks my view. It puts a UINavigationBar on the top but the rest of the view doesn't respond to input.
This is how I am presenting the view. The SecondViewController is the one I want to have a NavController:
UITabBarController *tabController = [[UITabBarController alloc] init];
FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"CardsViewController" bundle:nil];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:#"First"
image:[UIImage imageNamed:#"img1.png"] tag:1];
[viewController1 setTabBarItem:tab1];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"ShoppingViewController" bundle:nil];
UINavigationController *SecondViewNavCont = [[UINavigationController alloc]initWithRootViewController:viewController2];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:#"Second"
image:[UIImage imageNamed:#"img2.png"] tag:2];
[SecondViewNavCont setTabBarItem:tab2];
UIViewController *viewController3 = [[UIViewController alloc] init];
UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:#"Third"
image:[UIImage imageNamed:#"img3.png"] tag:3];
[viewController3 setTabBarItem:tab3];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2,
viewController3,
nil];
[self.view addSubview:tabController.view];
You cannot add it to the current view controller
What you need to do is instead of presenting the ViewController is to add this viewcontroller to the navigationview controller and present that
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:YourPresentedViewController];
//then present the navController
[self presentModalViewController:navController animated:YES];
Now when you present it do the following
NSArray arrayWithObjects:viewController1,
SecondViewNavCont,
viewController3,
nil];
You can try this code for load uiviewcontroller to uinavigationcontroller:
yourviewController *viewcontroller=[[yourviewController alloc] initWithNibName:#"yourviewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller];
[self presentModalViewController:navController animated:YES];
(or) you want to load uinavigationcontroller in application startup try below code in your application delegate class:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller.view];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Welcome!
You need to set the RootViewController for the navController object. Then further you can push UIViewcontroller objects using pushViewController method.
You need to add a view for that Navigation Controller. The added Navigation Controller has no view so it was overlaying on the UIViewController
You need to add
[[UINavigationController alloc] initWithNibName:#"ViewControllerName" bundle:nil];

Resources