2013-08-20 35 views
0

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

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我已经添加了以下方法做了选择项。但它不工作。如何和在哪里我不能写,然后得到选定的项目?

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

} 

回答

1

为代表的选择是

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

注意 “tabBarController” 后右 “ - (空)” 而不是 “tabController”。

希望这会有所帮助!

+0

感谢它现在的工作 – vijay

相关问题