CodeWeed
CodeWeed

Reputation: 1071

UISegmentControl switching between viewcontrollers

I have a navigation controller which has a UISegmentControl on its navigation bar, I have already set a UIViewController as the rootViewController to the navigation controller. Now I have two other UITableViewControllers that I need to switch between when the user selects the UISegmentControl.

I set the

[rootViewController.view addSubview:firstTableView.view]

when I handle the segmentcontrol value changed event, I set the other table view controller like this

[rootViewController.view removeFromSubview];
[rootViewcontroller.view addSubview:secondTableView.view];
[segmentControl setEnabled:YES forSegmentAtIndex:segmentControl.selectedSegmentIndex];

but when the segment control switches to the second table view, nothing !!! just blank.

Can anyone tell me how to do this ?

Upvotes: 0

Views: 143

Answers (2)

Kasaname
Kasaname

Reputation: 1501

-viewDidLoad()
{

[ self.view addSubview: firstTableView] ;
firstTableView.hidden = yes;
[ self.view addSubview: secondTableView] ;
secondTableView.hidden = yes;


}

-(IBAction) segmentAction:(id)sender 
{
    UISegmentedControl* control = sender ;

    if( [control selectedSegmentIndex] == 0 )
    {
       firstTableView.hidden = no;
       secondTableView.hidden = yes;

    }
    if( [control selectedSegmentIndex] == 1 ) 
    {  

       firstTableView.hidden = yes;
       secondTableView.hidden = no;
    }

}

Upvotes: 1

iDev
iDev

Reputation: 23278

In order to add a viewcontroller as a subview you can do it like this:

[rootViewController.view addSubview:firstTableView.view];
[rootViewController addChildViewController:firstTableView]; 

And for removing it:

[firstTableView.view removeFromSuperview];
[firstTableView removeFromParentViewController];

[rootViewcontroller.view addSubview:secondTableView.view];//now you can add second tableviewcontoller
[rootViewController addChildViewController:secondTableView];

Upvotes: 1

Related Questions