开发者

How To Load Separate NIB/Class Using UISegmentedController

开发者 https://www.devze.com 2023-03-22 19:49 出处:网络
I have 3 separate view controllers with their own NIB file. I have a overview view in my app with a segmented control that you can toggle through. Instead of copying all 3 of these view\'s methods an

I have 3 separate view controllers with their own NIB file.

I have a overview view in my app with a segmented control that you can toggle through. Instead of copying all 3 of these view's methods and putting them into this overview's class, is it possible to just load each view as a subview of the overview view deepening on which segment is selected?

- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
{
    switch (index)
    {
        case 0:
        {
            MusclesTableViewController *musclesTableViewController = [[MusclesTableViewController alloc] initWithNibName:@"MusclesTableViewController" bundle:nil]; 
            [self.view addSubview: musclesTableViewController]; 
            [musclesTableViewController release];  
        }
            break;
        case 1:
            // load second nib and add it a开发者_JAVA百科s a subview    
            break;
        default:
            break;
    }
}


Connect an action to your segment controller:

[yourSegmentedControl addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged];

use your method to load the desired subview:

- (void)changeSegment:(id)sender {
    UISegmentedControl *segment = sender;

    switch ([segment selectedSegmentIndex]) {
        case 1:
            // load first nib and add it as a subview
        break;
        case 2:
            // load second nib and add it as a subview    
        break;
        default:
        break;
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消