【问题标题】:selectedIndex UItabBarController not workingselectedIndex UItabBarController 不工作
【发布时间】:2013-08-19 23:22:14
【问题描述】:

我知道这将是一个简单的解决方案,但由于某种原因,当我尝试使用其他解决方案解决我的问题时,它只是不起作用。在我的应用程序中,我设置了推送通知,并尝试设置从通知打开应用程序时将应用程序打开到某个 tabBarItem 的功能。这是我到目前为止的代码,请告诉我我是否使用了正确的方法。我对AppDelegate.m 不是很有经验,所以如果完全错误,请原谅我。

AppDelegate.m

#import <CoreLocation/CoreLocation.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate,CLLocationManagerDelegate>{

CLLocationManager *locationManager;
}


@property (strong, nonatomic) UIWindow *window;


@property(nonatomic,readonly) UITabBar *tabBar;
@property(nonatomic,strong) UITabBarController *tabBarController;
@end

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];


return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
/* this works just fine*/
NSLog(@"opening from a notification!!");
/*this is what doesnt work at all*/
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController ];
self.tabBarController.selectedIndex = 3;
/*also when i do nslog the selectedIndex it gives me this value "2147483647"*/
NSLog(@"%i is the tabbar item",tabBarController.selectedIndex);
}

【问题讨论】:

  • 你在哪里将它设置为 rootview 控制器?

标签: ios objective-c uitabbarcontroller appdelegate


【解决方案1】:

因为您从登录控制器推送到标签栏控制器,所以无法从应用委托中获取对标签栏控制器的引用,因为它在被推送之前不存在。我解决这个问题的方法是改变你的应用程序的结构。尽管您当前的结构没有任何问题(因为它会导致崩溃),但我不喜欢将登录控制器放在首位,因为它们仅用于登录一次,那么最好不要使用它们.我会让标签栏控制器成为窗口的根视图控制器,并在第一个标签中通过控制器的 viewDidAppear 方法以模态方式呈现登录控制器,没有动画——这将使登录控制器成为用户看到的第一件事。完成登录后,您只需关闭并返回第一个选项卡。

如果你使用这种结构,那么你可以使用我的建议来设置选定的索引:

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
    NSLog(@"opening from a notification!!");
    self.tabBarController = self.window.rootViewController;
    self.tabBarController.selectedIndex = 3;
    NSLog(@"%i is the tabbar item",tabBarController.selectedIndex);
}

【讨论】:

    【解决方案2】:

    对于本地通知:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
     // it from running if it doesn't exist (such as running under iOS 7 or earlier).
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    #endif
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    if ([UIApplication sharedApplication].scheduledLocalNotifications.count >= 1) {
            // Handle local notification received if app wasn't running in background
            UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
            tabBarController.selectedIndex = 1;
        }else{
        UIViewController *viewController =  [storyboard instantiateViewControllerWithIdentifier:@"tab"];
        self.window.rootViewController = viewController;
        [self.window makeKeyAndVisible];
    }
     return YES;
    }
    

    对于推送通知:

    (1)。

     - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
    NSLog(@"opening from a push/remote notification!!");
      UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
            tabBarController.selectedIndex = 1;
    NSLog(@"%i is the tabbar item",tabBarController.selectedIndex);
    }
    
                   **(OR).** `
    
     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
       int badgeCount = [UIApplication sharedApplication].applicationIconBadgeNumber;
     if (badgeCount >= 1) {
            // Handle local notification received if app wasn't running in background
            UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
            tabBarController.selectedIndex = 1;
        }else{
        UIViewController *viewController =  [storyboard instantiateViewControllerWithIdentifier:@"tab"];
        self.window.rootViewController = viewController;
        [self.window makeKeyAndVisible];
    }
    self.window.backgroundColor = [UIColor whiteColor];
    return YES;
    }
    

    `

    【讨论】:

      【解决方案3】:

      试试这个

      NSUInteger index = [self.tabBarController.tabBar.items indexOfObject:self.tabBarController.tabBar.selectedItem];    
      NSLog(@"Index: %d", index);
      

      【讨论】:

        【解决方案4】:

        您应该使用selectedViewController 属性,而不是分配selectedIndex

        self.tabBarController.selectedViewController = [self.tabBarController objectAtIndex:3];//or whatever the index the VC you want to select is
        

        您也不应该从 AppDelegate 分配 tabBarController。您需要获取 rootViewController,然后从那里获取已实例化的 tabBarController 的实例

        在此处查看接受的答案,了解如何从 appDelegate 获取 VC 的示例。

        How do I access my viewController from my appDelegate? iOS

        【讨论】:

        • 使用 selectedIndex 很好,问题是你所说的不分配标签栏控制器的新实例。如果是根视图控制器,他可以使用 self.window.rootViewController 来访问它。
        • @rdelmar 它实际上不是我的根视图控制器。你碰巧知道我应该如何把它联系起来吗?
        • @FabianBuentello,不知道你有什么控制器,以及如何从一个控制器转到另一个控制器。
        • @rdelmar 我只是有一个登录类型的 viewcontroller,它是初始视图控制器,然后从那里推送到 uitabbarcontroller
        【解决方案5】:
        self.tabBarController = [[UITabBarController alloc]init];
            self.tabBarController.delegate = mobileYakAppDelegate;
            self.tabBarController.viewControllers = ................your view controllers adding here .........;
            self.tabBarController.selectedIndex = 3;
            self.window.rootViewController = mobileYakAppDelegate.tabBarController;  
        

        试试这样对我来说很好用

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-23
          • 1970-01-01
          • 1970-01-01
          • 2014-11-29
          • 2014-06-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多