Reputation: 4100
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
.
Upvotes: 0
Views: 877
Reputation: 2854
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
}
Upvotes: 3