0

有谁知道有一个 UITabBarButton 的方法,而不是转到另一个视图控制器,而是执行另一个功能,例如调用一个方法?

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

-EDIT- 为清晰起见添加了屏幕截图和代码片段。标记的选项卡Sync是我不想打开 viewController 的选项卡:

在此处输入图像描述

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

1 回答 1

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;
}
于 2013-02-07T14:20:25.443 回答