简体   繁体   中英

UITabBarController add new UIViewController above storyboard ones (Swift)

I have a storyboard file with a UITabBarController connected to 4 view controllers, and I want to add number 5 programmatically. In Objective-C I did as below:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSArray *tabs =  self.viewControllers;

    //set tab controllers
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:(@"Settings_Storyboard_iPad") bundle:nil];
    AddFolderViewController *vc = [storyboard instantiateInitialViewController];
    self.viewControllers = [NSArray arrayWithObjects:[tabs objectAtIndex:0], [tabs objectAtIndex:1], [tabs objectAtIndex:2], [tabs objectAtIndex:3], vc, nil];
}

In a UITabBarController subclass. However in Swift I don't seem to be able to do it. It doesn't let me either access the indices of tabs, or append the controller .

override func viewDidLoad() {
         super.viewDidLoad()
    var viewControllers: [UIViewController] = self.viewControllers as! [UIViewController];

    let storyboard : UIStoryboard = UIStoryboard(name:"Main", bundle: nil)
    let thirdVC : ThirdViewController = storyboard.instantiateViewControllerWithIdentifier("ThirdVC") as! ThirdViewController


    let icon1 = UITabBarItem(title: "Third", image:UIImage(named: "image.png"), selectedImage:UIImage(named: "otherImage.png"))
    thirdVC.tabBarItem = icon1
    viewControllers.append(thirdVC)
    self.viewControllers = viewControllers

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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