Tab bar method? - iphone

I added this tab bar in view controller 1 and it works perfectly, the only issue is I want it to do the following function [self.navigationController popViewControllerAnimated:YES];, but in view controller2 when the tab bar button is touched. What would be the best course of action?
UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
homeNavigationController.tabBarItem = [[DSTabBarItem alloc] initWithFinishedSelectedImage:[UIImage imageNamed:#"home"]
finishedUnselectedImage:[UIImage imageNamed:#"home1"]
iconSize:CGSizeMake(76, 59)
tag:0];
[tabBarViewControllers addObject:homeNavigationController];

If you added tabbarcontroller programmatically . And In that class add the following method.
Then you navigation controller will changed to root view. And you can place your own between the if to your requirement.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([self.tabBarController.selectedViewController isKindOfClass:[UINavigationController class]])
{
[(UINavigationController*)self.tabBarController.selectedViewController popToRootViewControllerAnimated:YES];
}
}

DetailSettingsViewController *settings = [[DetailSettingsViewController alloc] initWithNibName:#"DetailSettingsViewController" bundle:nil];
[self.navigationController pushViewController:settings animated:YES];

Related

Calling poptoviewcontroller function from a viewcontroller

Let me explain clearly.
I have a tabbarcontoller in the viewcontroller which is the main view controller of the single view application project.
I added a tabbarcontroller to the viewcontroller as the subview. In the tabbarcontroller, I added two navigation controllers like below image,
I have added three(named First, Second, Third) more viewcontrollers as new file.
If I navigate from one viewcontroller to other in a tab using below,
third = [[Third alloc] initWithNibName:#"Third" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:third animated:YES];
If I switch to next tab and come back to the previous tab, it should popto the previous view controller, how to do this?
I tried
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
not succeed,
I also tried,
[third popto];
In the third viewcontroller,
-(void)popto
{
[self.navigationController popViewControllerAnimated:YES];
}
nothing happened.
Now, I have to click the tab again to poptoviewcontroller to the first viewcontroller.
Any ideas will be highly appreciated.
You should try using
[viewController.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
instead of
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
Try the below code snippet
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
UINavigationController *navController = (UINavigationController*)viewController;
if (navController && ([[navController viewControllers] count] > 0))
{
[navController popToRootViewControllerAnimated:NO];
}
return YES;
}
Hope it may work for you.
How about overriding UITabbarController for a custom one and implement the following method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
Next loop trough all the 'viewcontrollers' of the tabbar (in this case the navigation controllers) and call on all the navigation controllers popToRootViewController.
This might work.

UITabbarController + UINavigationController creating issue for push and pop

In my iPhone application I am using UITabbarController + UINavigationController, created in appDelegate.
My issue is as follows:
On screen- I have tableview. When one selects cell I am pushing the controller to screen-B. Now without poping it if I select another tab from tabbar, then view gets refreshed but navigationbar is not getting refreshed. It displays navigationbar of screen-Bscreen-B.
Used below code but nothing seems to solve my issue:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController isKindOfClass:[UINavigationController class]])
{
[viewController.navigationController popToRootViewControllerAnimated:NO];
}
}
try this in appdelegate
UINavigationController *screen1=[[UINavigationController alloc] initWithRootViewController:controller1];
UINavigationController *Bscreen=[[UINavigationController alloc] initWithRootViewController:controller2];
NSArray *tabbararray=[[NSArray alloc] initWithObjects: screen1,Bscreen,nil];
self.TabControllerObj.viewControllers=allviewcontrollers;
and make tabbar as root viewcontroller of your window

Changing UIViewController in UITabBarController on clicking the tab

I have UITabBarController with 4 tabs. I have individual 4 views on each of the tab of the UITabBarController. I want to change a UIViewController of second tab of the UITabBarController whenever i click the third tab of the UITabBarController.
You can do this by using UITabBarController's delegate as -
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([tabBarController selectedIndex] == 2)
{
NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[tabBarController viewControllers]];
NewViewController *nvc = [[NewViewController alloc] init];
[arr replaceObjectAtIndex:1 withObject:nvc];
[nvc release];
[tabBarController setViewControllers:arr];
}
}

How to get the navigation bar when the tabbar controller is clicked?

I have an application in which I am trying to dynamically switch the tabbar tab through the code. The tab switches properly. When I click on any tab the didSelectController method of the tab is called and my problem is when I click on any tab the tab on which I am performing switching of views, its navigation bar disappears and its tabbar image and title also disappears.
This is my code. In the appdelegate:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSString *clockswitch = [[NSUserDefaults standardUserDefaults]objectForKey:#"clock"];
UIViewController *desiredController = nil;
if ([clockswitch isEqualToString:#"digital"]) {
desiredController = [[DigitalClockViewController alloc] initWithNibName:#"DigitalClockViewController" bundle:nil ];
}
else {
desiredController = [[AnalogClockViewController alloc]initWithNibName:#"AnalogClockViewController" bundle:nil];
}
NSMutableArray *controllersCopy = [self.tabBarController.viewControllers mutableCopy];
[controllersCopy replaceObjectAtIndex:0 withObject:desiredController];
self.tabBarController.viewControllers = controllersCopy;
}
You code is changing the order in which your view controllers appear in the tab bar, not changing the selected view controller as you seem to wish. This may result in the same view controller appearing twice in the bar, which may have unintended side effects.
Use selectedIndex or selectedViewController to change the selection. You want to select one of the controller that is already there, not alloc/init a new one; and, as Johannes has said, the entry in the list of controllers with be a UINavigationController with a rootViewController that is an instance of your class (isKindOf:).
If you want each view controller within your tab bar controller to have a navigation bar, you have to "wrap" each view controller in a navigation controller and then but the navigation controller into the viewControllers property of your tab bar controller. You wrap it like so:
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:desiredController];
if ([clockswitch isEqualToString:#"digital"])
{
desiredController = [[DigitalClockViewController alloc] initWithNibName:#"DigitalClockViewController" bundle:nil ];
navigationController=[[UINavigationController alloc] initWithRootViewController:desiredController];
[self presentModelViewController:navigationController animated:YES];
[navigationController release];
[desired... release];
}
else {
desiredController = [[AnalogClockViewController alloc]initWithNibName:#"AnalogClockViewController" bundle:nil];
...//same code
}

iPhone - Dismissing previous ModalViewControllerAnimated:YES then poping a new ModalViewControllerAnimated:YES - fail

I have a main window that pops a modal view controller. When done in this modal view controller, it returns to the main window then dismisses itself.
Then the main windows pops a new Modal view controller with animated=YES.
The problem is that the dismiss call that is done inside the first modalviewcontroller applies to both and SecondController is never shown.
Putting the first dismiss before or after the parent call does not change anything.
If first dismiss is set wih animate= NO, everything works fine. But I need the animation.
Main.m
- (void) entry {
FirstController *nextWindow = [[FirstController alloc] initWithNibName:#"theNIB" bundle:nil];
nextWindow.caller = self;
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:nextWindow];
[self.navigationController presentModalViewController:navController animated:YES];
[nextWindow release];
[navController release];
}
- (void) thingsDoneInFirstModalController:(OBJECT)returnValue retval2:(OBJECT2)returnValue2 {
[self display2ndController];
}
- (void) display2ndController {
SecondController *nextWindow;
nextWindow = [[SecondController alloc] initWithNibName:#"NIB2" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:nextWindow];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
[nextWindow release];
}
1st ModalViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.navigationController dismissModalViewControllerAnimated:YES];
[self.caller thingsDoneInFirstModalController:theResult retval2:someMoreResult];
}
What may I do ?
I don't want to catch anything in view....Disapear...
Why does the dismiss colides as they are not called from the same navigation controller ?
The reason is likely the animation when you dismiss it. Try showing the second modal window using the performSelector:withObject:afterDelay:, a method inherited from NSObject. Reference here.