UITabBarController委托选择了Item

时间:2013-08-20 14:15:12

标签: objective-c delegates uitabbarcontroller

我想知道在tabbar中选择哪个项目。我添加了以下UITabBarController appdeletegate.m文件。标签项栏显示正确。在我选择了ITEM后我想知道哪个项目会选择。

appDelegate.m

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,
                                               [UIFont fontWithName:@"TrajanPro-Regular" size:16],UITextAttributeFont, nil];


    [[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
    UIImage *navBackgroundImage = [UIImage imageNamed:@"top-bar.png"];
    [[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

    //BulletinViewController *bulletin = [[BulletinViewController alloc] initWithNibName:@"BulletinViewController" bundle:nil];
    //UINavigationController *bulletinNav =  [[UINavigationController alloc] initWithRootViewController:bulletin];
    //bulletinNav.navigationBar.barStyle = UIBarStyleBlack;

    CGFloat verticalOffset = 4;
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];

    DiaController *contact  = [[DiaController alloc] initWithNibName:@"DialerViewController" bundle:nil];
    UINavigationController *contactNav =  [[UINavigationController alloc] initWithRootViewController:contact];
    contactNav.navigationBar.barStyle = UIBarStyleBlack;


    ChViewController *chat  = [[ChatViewController alloc] initWithNibName:@"ChatViewController" bundle:nil];
    UINavigationController *chatNav =  [[UINavigationController alloc] initWithRootViewController:chat];





    //SettingsViewController *setting   = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
    //UINavigationController *settingNav =  [[UINavigationController alloc] initWithRootViewController:setting];

    self.tabController = [[UITabBarController alloc] init];
    CGRect tabframe = self.tabController.tabBar.frame;
    tabframe.origin.y -= 10;
    tabframe.size.height += 10;
    self.tabController.tabBar.frame = tabframe;
    self.tabController.delegate = self;
    self.tabController.viewControllers = @[contactNav,chatNav];
    UIImage *tabBackgroundImage = [UIImage imageNamed:@"bottom-bar.png"];
    [self.tabController.tabBar  setBackgroundImage:tabBackgroundImage];

    self.window.rootViewController = self.tabController;
    [self.window makeKeyAndVisible];

DiaController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.title = @"New";
        [[self tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"common_c.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"common_c.png"]];
        //[[self tabBarItem] setTitleTextAttributes:[[Common sharedInstance]getTabDict] forState:UIControlStateNormal];
    }
    return self;
}

ChViewController.m

     - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.title = @"New";
        [[self tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"common_c.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"common_c.png"]];
        //[[self tabBarItem] setTitleTextAttributes:[[Common sharedInstance]getTabDict] forState:UIControlStateNormal];
    }
    return self;
}

AppDelegate.m我为did select项添加了以下方法。但它不起作用。如何以及在哪里我不能写,然后获得选定的项目?

- (void)tabController:(UITabBarController *)tabController didSelectViewController:(UIViewController *)viewController
{
        UINavigationController *navigationController = (UINavigationController *)viewController;
        [navigationController popToRootViewControllerAnimated:NO];

}

1 个答案:

答案 0 :(得分:1)

委托的选择器是

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

请注意“ - (void)”之后的“tabBarController”而不是“tabController”。

希望这有帮助!