UITabBarButton无需切换到另一个视图

时间:2013-02-07 14:18:24

标签: ios xcode uitabbarcontroller uitabbaritem

有没有人知道如何使用UITabBarButton,而不是转到另一个视图控制器,将执行另一个功能,例如叫一个方法?

我的iPad应用程序使用标签栏,但客户端希望最右边的按钮执行服务器上的更新检查;但我似乎无法弄清楚如何按下按钮时不会切换视图。我尝试删除了segue,但也删除了按钮。

CNC中 为清晰起见,添加了屏幕截图和代码段。标有Sync的标签是我不想打开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;
}

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

    NSLog(@"vc = %@", viewController);
    return YES;
}

1 个答案:

答案 0 :(得分:1)

查看UITabBarControllerDelegate方法:

– tabBarController:shouldSelectViewController:

如果selectViewController ==您的上一个标签栏,return NO并执行其他操作

编辑:

看看我做的例子:

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;
}