【问题标题】:How To Load Separate NIB/Class Using UISegmentedController如何使用 UISegmentedController 加载单独的 NIB/类
【发布时间】:2011-07-24 23:15:18
【问题描述】:

我有 3 个独立的视图控制器,它们都有自己的 NIB 文件。

我的应用中有一个概览视图,其中包含一个可以切换的分段控件。与其复制所有这 3 个视图的方法并将它们放入此概览的类中,是否可以仅将每个视图加载为概览视图的子视图,加深选择哪个段?

- (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 as a subview    
            break;
        default:
            break;
    }
}

【问题讨论】:

    标签: iphone objective-c cocoa-touch uiviewcontroller


    【解决方案1】:

    将动作连接到您的分段控制器:

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

    使用您的方法加载所需的子视图:

    - (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;
        }
    
    }
    

    【讨论】:

    • 我使用的段实际上只有这个委托方法,你能把它放到你的代码中吗?谢谢。 - (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
    • 您可以使用switch根据选择的segment index添加一个子视图。
    • 好的,我不这样做,你能展示如何加载笔尖并添加为子视图吗?然后我不接受答案。谢谢。
    • 这可以通过网站上的简单搜索找到。 BTW YourViewController *controller = [[YourViewController alloc] initWithNibName:@"YourViewControllerNibFile" bundle:nil]; [self.view addSubview:controller]; [控制器发布];
    • 谢谢,我在我的问题中添加了代码。我试过了,但什么也没发生。
    猜你喜欢
    • 2015-01-21
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多