iPhone Tab Bar with Segmented Controller
In this tutorial will learn how to use the two controller at the same time, Tab bar is used to show the different view on click but in segmented controller you cannot recognize that its changing the view. Now to do this will create project based on window application and on that will add Tab bar controller and in that tab bar controller view will add the segmented controller and on click on segment button will print different text so that you can notice that the action written on segment controller is working and you can use it for loading the view this can be done by uncommenting the view code method.
Final View will look like this:
My project name is Tab and is based on Window application. (You can also choose the Tab bar application )Here will add Tab Bar Controller and the Segmented Controller and write the property for the Tab Bar and also synthesize it in .m file. After declaring the variable and the action for the segment, save it and then make connection with the interface Builder.
Add this to the .h file:
IBOutlet UISegmentedControl *myMent;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
In .m file will synthesize the Tab bar controller and after that will write the method for the segment action, in that method will create a variable for segment controller to check the index of segment controller so whenever the segment button is pressed it will print different text. And then will add Tab Bar Controller to the window by using Window addsubview:tabBarController.view . In viewDidLoad method will set that whenever you come to the segmented tab it will show you the first segment is pressed.
Add this to the .m file:
- (IBAction) segmentAction:(id)sender
UISegmentedControl* segCtl = sender ;
if( [segCtl selectedSegmentIndex] == 0 )
NSLog(@"hi this is first segment");
// [ self.view addSubview:first] ;// to add view use this statement first is the view controller name
if( [segCtl selectedSegmentIndex] == 1 )
NSLog(@"hi this is second segment");
// [ self.view addSubview:second] ;
- (void)applicationDidFinishLaunching:(UIApplication *)application
[myMent addTarget:self action:@selector(segmentAction:)
myMent.selectedSegmentIndex = 0 ;
Finally make connection as shown:
And to Run the project Press Build And Go Button
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.