简体   繁体   English

UITabBarButton无需切换到另一个视图

[英]UITabBarButton without switching to another view

Does anyone know of a way to have a UITabBarButton that, rather than segueing to another view controller, will perform another function, eg call a method? 有谁知道有一种UITabBarButton的方法,而不是选择另一个视图控制器,而是执行另一个功能,例如调用方法?

My iPad app utilises a tab bar but the client wants the right-most button to perform a check for updates on a server; 我的iPad应用程序使用选项卡栏,但是客户端希望最右边的按钮在服务器上执行更新检查。 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. 我尝试删除segue,但是也删除了按钮。

-EDIT- screenshot and code snippet added for clarity. 添加了-EDIT-屏幕截图和代码段,以提高清晰度。 The tab labelled Sync is the one I want not to open a viewController: 标签为Sync的选项卡是我不想打开viewController的选项卡:

在此处输入图片说明

AppDelegate.h: AppDelegate.h:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

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

@end

AppDelegate.m 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;
}

Look at the UITabBarControllerDelegate method : 看一下UITabBarControllerDelegate方法:

– tabBarController:shouldSelectViewController:

If selectViewController == your last tab bar, return NO and perform others actions 如果selectViewController ==您的最后一个标签栏,则return NO并执行其他操作

EDIT : 编辑:

Look at the example I've made : 看我做的例子:

AppDelegate.h AppDelegate.h

@interface ISAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM