1
votes

I am new to objective-c and Xcode. As far as I know, the way of coding on a view controller created by storyboard, is to create a custom subclass of UIViewController ,connect it to the VC and make outlets or actions.

When it comes to a tab bar controller or a navigation controller, I wonder how to program on these controllers. For example: I drag a tab bar controller on to storyboard and want to set the selected images of the tab bar items, which (I was told) can't be set by storyboard attributes inspector, I need to use the initWithTitle:image:selectedImage: method by code. So my question is : where should I put the initWithTitle:image:selectedImage: ? Should I create a custom subclass of UITabBarController, make outlet on the subclass file and program in the file? Or there're other easy ways instead? (Someone told me to code in AppDelegate file, but I failed. If it is the right way, please tell me more detail.)

Thanks.

1

1 Answers

3
votes

You need to create a subclass of UITabBarController. In the viewDidLoad: method, you can create your tab bar items like this:

//create your view controllers for each tab
UIViewControllerOne *vcOne = [[UIViewControllerOne alloc]init];
vcOne.tabBarItem = [[UITabBarItem alloc] initWithTitle@"VC One" image: yourImage      selectedImage: yourSelectedImage];

UIViewControllerTwo *vcTwo = [[UIViewControllerTwo alloc]init];
vcTwo.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"VC Two" image: yourImage selectedImage: yourSelectedImage];

//set your tab controllers view controllers
[self setViewControllers:@[vcOne, vcTwo]];
[self setSelectedIndex:0]; //initially select the first tab

Then in your storyboard or nib where you dragged and dropped an instance of UITabBarController, you need to change the class of the tab bar controller to your subclass under the identify inspector. hopefully this helps your get started but it may be worth looking into a tutorial of some sort.

EDIT This is technically a tutorial for storyboards but the example uses a tab bar controller and may be helpful. Ray Wenderlich is a great resource for tutorials and such. http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1