0

my application story board is as following

storyboadrd

it is a tab bar controller based application with one of the tabs embedded in a navigation controller. when the user click on the first tab (view1) and click a button inside this view, he will be moved to view2. instead of using the back button to return to view1, I want the user to click on the tab item in order to return to view1 which works fine. However, I want to view an alert when the user clicks on the tab and he is in View2. I am using shouldSelectViewController and didSelectViewController delegate methods to check what tab is clicked and view the alert. The problem is that I can't reach View2 from these methods in the delegate to inform the application to view the alert only when user is in view2 and clicks the tab.

I tried to use this code inside shouldSelectViewController

    if (tabBarController.selectedIndex == 0) {
    NSLog(@"Delegate nav title: %@", tabBarController.selectedViewController.navigationItem.title);
}

these lines always return the title of view1

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alsmayer
  • 236
  • 1
  • 4
  • 13
  • same problem i faced. now i fixed that problem. can u see the Link [Tabbar component using to fix this problem][1] [1]: http://stackoverflow.com/questions/23698697/how-to-make-sliding-menu-with-tabs-in-ios/23804738#23804738 – HariKrishnan.P May 22 '14 at 12:14
  • thank you for help, but it does not seem to be the same problem. I am trying to get information about a viewController stacked in a NavigationController from the AppDelegate – Alsmayer May 23 '14 at 10:29

2 Answers2

1

As they are in the same navigation controller, they have the same navigation item. Each view can configure this navigation item but it is generally the same object. That is why it returns the same title. Try setting up the navigation item title in second view controller's viewDidLoad method.

verges
  • 21
  • 2
0

I finally found a solutino to my problem first I added a static variable to View2 and called it inView2 as following in View2.h

@interface
{}

+ ( BOOL ) isInQuizViewController;
+ ( void )setInQuizViewController:(BOOL)inQuizVal;
//...
@end

in View2.m

@implementation
   static BOOL inView2 = NO;

+(BOOL)isInView2
{
    return inView2;
}
+ ( void )setInView2:(BOOL)Val
{
    inView2 = val;
}

these two methods are for setting and getting the value of inView2 which tells me whether or not the user is currently in View2

in View1.h create an IBAction associated with the button that will transmit from View1 to View2 and connect it to your storyboard

- (IBAction)GoToView2:(id)sender;

go to View1.m and import

#import "View2.h"

and implement your IBAction method to set the InView2 to YES

- (IBAction)GoToView2:(id)sender {
[View2 setInView2:YES];
}

then in the delegate.m

#import "View2.h"
@implementation AppDelegate

UITabBarController * _tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _tabBarController = (UITabBarController *)_window.rootViewController;
    _tabBarController.delegate = self;

    // Override point for customization after application launch.
    return YES;
}

I defined a global _tabBarController and set it to _window.rootViewController and set its delegate to this delegate and remember to import "View2.h"

going to the ShouldSelectViewController method (note that this method is called before the transition to the selected tab ViewController, hence it is perfect for decision making of whether or not the selected tab viewController should be displayed to the user). so in ShouldSelectViewController method I did the following

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {



    if(tabBarController.selectedIndex == 2)
    {
        if ([View2 isInView2]) {


        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"alert"
                                                            message:@"are you sure you want to exit?"
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Yes",nil];
        [alertView show];

        return NO;
        }
    }
    return YES;

}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if(buttonIndex == 1)
    {//ok button pressed




//        NSInteger destinationTabIdx = 2;
//        UIView * fromView = tabBarController.selectedViewController.view;
//        UIView * toView = [[[[[tabBarController.viewControllers objectAtIndex:destinationTabIdx] navigationController] viewControllers] objectAtIndex:0] view];

        UINavigationController *nav = (UINavigationController *)_tabBarController.selectedViewController;

        NSLog(@"vc title: %@",nav.title);
//        [UIView transitionFromView:fromView toView:toView duration:0.8
//                           options: UIViewAnimationOptionTransitionNone
//                        completion:^(BOOL finished) {
//                            if (finished) {
//                                tabBarController.selectedIndex = destinationTabIdx;
//                            }
//                        }];
        [nav popViewControllerAnimated:YES];
        [QuizViewController setInQuizViewController:NO];
        NSLog(@"app delegate transistion done");


    }

}
Alsmayer
  • 236
  • 1
  • 4
  • 13