0

Does anyone know of a way to have a UITabBarButton that, rather than segueing to another view controller, will perform another function, e.g. call a method?

My iPad app utilises a tab bar but the client wants the right-most button to perform a check for updates on a server; however I can't seem to to figure out how to have a button that won't switch views when pressed. I tried deleting the segue but that removes the button as well.

-EDIT- screenshot and code snippet added for clarity. The tab labelled Sync is the one I want not to open a viewController:

enter image description here

AppDelegate.h:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;

@end

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self.window makeKeyAndVisible];       
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    tabBarController.delegate = (id)self;

   [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(applicationDidTimeout:) name:kApplicationDidTimeoutNotification object:nil];

    return YES;
}

and:

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

    NSLog(@"vc = %@", viewController);
    return YES;
}
Robert
  • 5,278
  • 43
  • 65
  • 115

1 Answers1

1

Look at the UITabBarControllerDelegate method :

– tabBarController:shouldSelectViewController:

If selectViewController == your last tab bar, return NO and perform others actions

EDIT :

Look at the example I've made :

AppDelegate.h

@interface ISAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.


    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    tabBarController.delegate = (id)self;

    return YES;
}

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

    // here check if "viewController" is your last tab controller, then return NO and perform some actions you need
    if (viewController = self.lastTabController) {
        // do some actions
        return NO;
    }

    return YES;
}
Yaman
  • 3,949
  • 4
  • 38
  • 60
  • Thanks. I'm a bit confused as to where to put the code. Should it be in the AppDelegate? – Robert Feb 07 '13 at 15:45
  • Thanks again. I tried implementing that and was expecting the `NSLog` statements to print out whenever I select a tab but nothing has. – Robert Feb 07 '13 at 16:25
  • I've tested with a new project and it works fine. I think it's now depending on your actual project implementation. Check if `self.window.rootViewController` is equal to your `UITabBarController`, check also if you well defining the `delegate` and the `protocol`. For more help, i'll need some more additional code and maybe a screenshot of your storyboard :) – Yaman Feb 07 '13 at 16:33
  • I've added a shot of the tab bar and my appDelegate code, if that's any help? =) – Robert Feb 07 '13 at 16:53
  • if you're not seeing any log, i think this is because the `delegate` is not properly set. Are you sure that your `rootViewController` is the `UITabBarController` ? – Yaman Feb 07 '13 at 17:12
  • Not sure, no. I selected the TBC in the storyboard and named it `tabBarController` but I know I've missed something. – Robert Feb 08 '13 at 09:20