UINavigationController issue - if i return to first view, i see the color of second view - ios

I am creating UINavigationController and it's UIViewControllers like this. First with yellow color, second with blue color.
Then adding it to stack. Yellow is displayed for a while, then the second blue.
What's the problem - if i press the button in top bar to get back to previous Controller, i don't see yellow background but blue, thought the title of window is "One" which is correct.
Why is this happening? Thx
UIViewController *one = [[UIViewController alloc] init];
[one.view setBackgroundColor:[UIColor yellowColor]];
[one setTitle:#"One"];
UIViewController *two = [[UIViewController alloc] init];
[two.view setBackgroundColor:[UIColor blueColor]];
[two setTitle:#"Two"];
UINavigationController * navController = [[UINavigationController alloc] init];
[self.view addSubview:navController.view];
[navController pushViewController:one animated:YES];
[navController pushViewController:two animated:YES];

I figured out what's the reason.
Because navController was relased from the memory.

Related

UINavigationController could't push two vc at same time

UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor redColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor blueColor];
[self.navigationController pushViewController:vc1 animated:YES];
[self.navigationController pushViewController:vc2 animated:YES];
This is my code, when i run this , i get a red ViewController rather than a blue one. So i want to know what's problem is?
You're pushing two controllers with animation, in which case, during the animation of the first one, you can't push the second one.
Either push the first one without the animation, or just use setViewControllers: method.

Why my navbar doesn't show up?

I'm trying to set my window's root view controller to a UITabBarController with a UINavigationController but for some reason the navigation bar doesn't show up. Anybody knows why?
Thanks
Here is my method:
- (void) showUserOnlyScreen
{
UITabBarController *tabbarController = [[UITabBarController alloc]init];
SSHomeViewController *homeVC = [[SSHomeViewController alloc]init];
UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:homeVC];
[tabbarController setViewControllers:#[homeNav]];
self.window.rootViewController = tabbarController;
[self.window makeKeyAndVisible];
}
Your code is OK.
Check if the UINavigationBar is transparent by applying a border on it:
homeNav.navigationBar.layer.borderWidth = 2.0f;
homeNav.navigationBar.layer.borderColor = [UIColor redColor].CGColor;
Check also if you mistakenly hide the navigationbar on your SSHomeViewController controller with this:
[self.navigationController setNavigationBarHidden:YES animated:NO];

How to get a new view controller instead of parent view for a 1st tab in tabbar view controller?

I struck in a problem with using tab bar controller to my view controller.
Here is my problem
I have a view controller for that i embedded a tab bar controller and i have added four tabs for it and three views are created for the other three tabs, here is my Exact problem. i don't want my 1st tab bar controller select default if i click on my 1st tab i want to show the other view it should not display my parent view where i am showing all the tabs in a parent view.
Please give me some solution how to overcome this. hope my question is clear with details.
Somehow I am able understand your problem. Without posting code sometime its very difficult to identify exact problem.
I am sharing a code with you , assume that App launch a single view controller as "Login View Controller" which is follow by "Dashboard View Controller". Now you want to show TabBar after user login with four different tabs.
Please called below method "setUpTabBar" within Dashboard View Controller ViewDidLoad.
- (void)setUpTabBar
{
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = #"Tab1";
vc1.view.backgroundColor = [UIColor clearColor];
UINavigationController *nav1 =[[UINavigationController alloc] initWithRootViewController:vc1];
nav1 =[self setTrasparentNav:nav1];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = #"Tab2";
vc2.view.backgroundColor = [UIColor clearColor];
UINavigationController *nav2 =[[UINavigationController alloc] initWithRootViewController:vc2];
nav2 =[self setTrasparentNav:nav2];
UIViewController *vc3 = [[UIViewController alloc] init];
vc3.title = #"Tab3";
vc3.view.backgroundColor = [UIColor clearColor];
UINavigationController *nav3 =[[UINavigationController alloc] initWithRootViewController:vc3];
nav3 =[self setTrasparentNav:nav3];
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = #[nav1,nav2,nav3];
tabBar.selectedIndex = 0;
tabBar.view.frame = self.view.bounds;
[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];
}
You can change default selected tab bar using tabBar.selectedIndex = 0;
Hope this will help you.
Update (As requested by questionnaire):-
UIView *viewBottom=[[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height- 60.0, self.view.bounds.size.width, 60.0)];
viewBottom.backgroundColor =[UIColor grayColor];
UIButton *btnAdd =[UIButton buttonWithType:UIButtonTypeSystem];
btnAdd.frame=CGRectMake(10, 10, 60.0, 40.0);
[viewBottom addSubview:btnAdd];
[self.view addSubview:viewBottom];
If you are trying from code, first see how to make uitabbarcontroller in code: How to programmatically add a UITabBarController & UINavigationController in AppDelegate?

Left Side menu with TAbbar in iphone

I want to implement the Left Side menu. On my top of screen, there is a TabBar with a menu button.
When the menu tab bar button click, the left Side menu will open.
I search a lot for this but all of them uses the Navigation Controller instead of TAbbar.
I try so many custom controls & SASlideview but don't understand how to work with tabbar.
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:[self navigationController],
[self navigationController], nil]];
SideMenuViewController *leftSideMenuController = [[SideMenuViewController alloc] init];
SideMenuViewController *rightSideMenuController = [[SideMenuViewController alloc] init];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:[self navigationController]
leftMenuViewController:leftSideMenuController
rightMenuViewController:rightSideMenuController];
self.window.rootViewController = container;
How can I do this?

How do i make the more button go always to more view?

I'm developing an app for ios with a Tab Bar. I have more than 5 buttons on the bar, so on the iphone i have the button more.
Now, suppose i have this buttons: Button1 Button2 Button3 Button4 More (and inside More) Button5 Button6.
If i click More and then Button5 i go in the view relative to Button5. Then i click Button2 (that is not in the More) and i go in the view relative to Button2.
So far so good.
Now if i click More i go not to More Tab but back to the view relative to Button5.
How do i make the more button go always to the more view?
You don't need to add more button. Just set the view controllers to the UITabBarController
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
and it will automatically create a more button if you have more than 5 view controllers! i.e. the count of NSArray is greater than 5.
Another way you could do is, whenever the user presses more, the first button gets removed and other buttons gets added.
Basically you can create an array and keep all the buttons inside it. And then based on the button pressed you can navigate to that particular view.
For Ex:
Initially you have: Button1 Button2 Button3 Button4 Next
After Clicking Next: Prev Button3 Button4 Button5 Button6
I used this code in my app delegate.m to solve the problem
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
UITabBarController* tabBarController2 = (UITabBarController*)self.window.rootViewController;
if (tabBarController2.selectedIndex < 4) {
[tabBarController2.moreNavigationController popViewControllerAnimated:NO];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[UIViewController alloc] init];
UIViewController *viewController2 = [[UIViewController alloc] init];
UIViewController *viewController3 = [[UIViewController alloc] init];
UIViewController *viewController4 = [[UIViewController alloc] init];
UIViewController *viewController5 = [[UIViewController alloc] init];
UIViewController *viewController6 = [[UIViewController alloc] init];
UIViewController *viewController7 = [[UIViewController alloc] init];
UIViewController *viewController8 = [[UIViewController alloc] init];
UIViewController *viewController9 = [[UIViewController alloc] init];
[viewController1.view setBackgroundColor:[UIColor whiteColor]];
[viewController2.view setBackgroundColor:[UIColor redColor]];
[viewController3.view setBackgroundColor:[UIColor greenColor]];
[viewController4.view setBackgroundColor:[UIColor grayColor]];
[viewController5.view setBackgroundColor:[UIColor blueColor]];
[viewController6.view setBackgroundColor:[UIColor yellowColor]];
[viewController7.view setBackgroundColor:[UIColor brownColor]];
[viewController8.view setBackgroundColor:[UIColor magentaColor]];
[viewController9.view setBackgroundColor:[UIColor purpleColor]];
[viewController1 setTitle:#"one"];
[viewController2 setTitle:#"two"];
[viewController3 setTitle:#"three"];
[viewController4 setTitle:#"four"];
[viewController5 setTitle:#"five"];
[viewController6 setTitle:#"six"];
[viewController7 setTitle:#"seven"];
[viewController8 setTitle:#"eight"];
[viewController9 setTitle:#"nine"];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, viewController4, viewController5, viewController6, viewController7, viewController8, viewController9];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
I have added a sample AppDelegate code which I tried and its working absolutely fine for me. Let me know what problem your having in this.

Resources