如何使 UISegmentedControl 像 UITabBarController 一样工作?

发布于 2024-10-09 04:31:35 字数 137 浏览 1 评论 0原文

当选择不同的段时,如何使用 UISegmentedControl 加载不同的子视图?我是 Objective-C 和 iOS 编程的新手。

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

How can I use UISegmentedControl to load different subviews when different segments are selected? Im new to objective-c and iOS programming.

OR is there a way to make UITabBarController look like a UISegmentedControl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千鲤 2024-10-16 04:31:35

编程方法:

对于loadView 中的

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

For a programatic approach

in loadView:

{
    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;
                }
            }
    }
谈场末日恋爱 2024-10-16 04:31:35

您可以将 UIToolbar 添加到根控制器的视图。在其中,您将拥有一个 UISegementedControl ,其中包含根控制器处理的操作。根据单击的段,您将加载不同的视图并在 UIToolbar 下显示该视图(以及您希望视图位于下方的任何其他内容)。

希望这有帮助!

You could add a UIToolbar to your root controller's view. In it, you'd have a UISegementedControl with actions that the root controller handle. Depending on the segment clicked, you would load up a different view and display the view under the UIToolbar (and anything else that you want the view to be below).

Hope this helps!

拿命拼未来 2024-10-16 04:31:35

您应该考虑 craftterm 在这篇文章中的答案: UISegmentedControl 最佳实践

这将使您能够保持正常的 ViewController 行为(支持旋转、内存警告等),同时允许在其之上进行分段控制。

You should consider crafterm's answer in this post: UISegmentedControl Best Practice

This will allow you to maintain your normal ViewController behavior (support rotation, memory warnings, etc.) while allowing for the segmented control on top of it.

梦明 2024-10-16 04:31:35

好的,为此,您在视图中创建两个视图,并在 .h 文件中为这两个视图创建属性

将 IBAction 附加到分段控件并编写这样的代码

if(self.yourSegmentedControl.selectedSegmentIndex==0)
    {   
    view1.hidden=YES;
        view2.hidden=NO;
    }
    else if(self.categorySegmentedControl.selectedSegmentIndex==1)
    {  
    view2.hidden=YES;
        view2.hidden=NO:
    }

希望这会对您有所帮助。

Ok for this purpose you make two views in your view and make property for both in .h file
and
Attach an IBAction to the segmented control and write code like this

if(self.yourSegmentedControl.selectedSegmentIndex==0)
    {   
    view1.hidden=YES;
        view2.hidden=NO;
    }
    else if(self.categorySegmentedControl.selectedSegmentIndex==1)
    {  
    view2.hidden=YES;
        view2.hidden=NO:
    }

Hope this will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文