iphone - 如何使 UISegmentedControl 像 UITabBarController 一样工作?

标签 iphone objective-c ios uisegmentedcontrol

如何在选择不同的段时使用 UISegmentedControl 加载不同的 subview ?我是 Objective-C 和 iOS 编程的新手。

或者有没有办法让 UITabBarController 看起来像 UISegmentedControl?

最佳答案

对于程序化方法

在加载 View 中:

{
    NSArray *segments = [NSArray arrayWithObjects:@"Left", @"Right", nil];


    segmentedControl = [[UISegmentedControl alloc]initWithItems:segments];
    [segmentedControl addTarget:self
    action:@selector(changeSubViews)
    forControlEvents:UIControlEventValueChanged];
    contentView = [UIView alloc]initwithFrame:(the frame where you want the subViews to be displayed)];
    [self.view addSubView:contentView];

    }

    - (void)changeSubViews
        {
            switch(segmentedControl.selectedSegmentIndex)
            {
            case 0:
                {
                    [rightView removeFromSuperView];
                    if (leftView ==nil){leftView alloc, init;}
                    [contentView addSubView:leftView];
                    break;
                }
            case 1:
                {
                    [leftView removeFromSuperView];
                    if (rightView ==nil){rightView alloc, init;}
                    [contentView addSubView:rightView];
                    break;
                }
            }
    }

关于iphone - 如何使 UISegmentedControl 像 UITabBarController 一样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4524103/

相关文章:

iphone - 使用谷歌地图中的参数开始逐向行驶

ios - 是否有可能在 iOS 代码中获取按钮单击的确切位置(快速)

ios - 长按幻灯片放映图片

iphone - Objective C - objectForKey 问题

objective-c - NSArray 索引 1 超出空数组的范围

ios - 在完成 block 中识别 NSURLSession

ios - 如何检测 UIWebView 中的点击事件

ios - 颠倒呈现 UIActivityViewController

javascript - iPad JS 上的闪存

iphone - iOS 电话中断后 AUGraph 停止运行