Add a Tab Bar Controller Programmatically to current App Flow

前端 未结 2 1478
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 21:17

I want to add a tab Bar Controller to my current App Flow. Currently I have a page with a button which on clicking opens a new viewcontroller with a web view where the user logs

相关标签:
2条回答
  • 2021-01-31 21:41

    Set Delegate UITabBarDelegate

    here TabBar Viewcontroller image http://prntscr.com/ba5oks

    #pragma mark- Tapbar delegate
    
    - (void)deselectTabBarItem:(UITabBar*)tabBar
    {
        tabBar.selectedItem = nil;
    }
    
    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
    {
        [self performSelector:@selector(deselectTabBarItem:) withObject:tabBar afterDelay:0.2];
    
        switch (item.tag) {
            case 0:
                //perform action
                break;
            case 1:
                //do whatever you want to do.
                break;
            case 2:
                //call method
                break;
            default:
                break;
        }
    }
    
    0 讨论(0)
  • 2021-01-31 21:50

    The way I've done this in the past is to create a UITabBarController subclass that contains all of the tabBar creation code you have above.

    Then use your UINavigationController to push the tabBar subclass to the screen.

    Here's a sample of my UITabBarController subclass:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIViewController *view1 = [[UIViewController alloc] init];
        UIViewController *view2 = [[UIViewController alloc] init];
        UIViewController *view3 = [[UIViewController alloc] init];
    
        NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
        [tabViewControllers addObject:view1];
        [tabViewControllers addObject:view2];
        [tabViewControllers addObject:view3];
    
        [self setViewControllers:tabViewControllers];
        //can't set this until after its added to the tab bar
        view1.tabBarItem = 
          [[UITabBarItem alloc] initWithTitle:@"view1" 
                                        image:[UIImage imageNamed:@"view1"] 
                                          tag:1];
        view2.tabBarItem = 
          [[UITabBarItem alloc] initWithTitle:@"view2" 
                                        image:[UIImage imageNamed:@"view3"] 
                                          tag:2];
        view3.tabBarItem = 
          [[UITabBarItem alloc] initWithTitle:@"view3" 
                                        image:[UIImage imageNamed:@"view3"] 
                                          tag:3];      
    }
    
    0 讨论(0)
提交回复
热议问题