使用切换控制器切换 2 个视图

发布于 2024-12-07 22:52:56 字数 130 浏览 0 评论 0原文

这是主视图,我想切换两个单独的视图;为 ON 状态和 OFF 状态。

代码会是什么样子?

使用开关控制器切换视图

This is the main view and I'd like to toggle two separated views; for ON state and for OFF state.

How the code would look like?

Toggle views with switch controller

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

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

发布评论

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

评论(3

小苏打饼 2024-12-14 22:52:56

执行以下操作

  - (void)viewDidLoad //of mainViewController {
        //do viewController1 alloc init     // Gray Color
        [self.view addSubview:viewController1.view];

        //do viewController2 alloc init     //Red Color
        [self.view addSubview:viewController2.view];

        [viewController2.view setHidden:YES];
    }

//assign this method to segmented controls' valueChanged event
    - (IBAction)segmentControlClicked:(id)sender {
        UISegmentedControl *segmentedControl = (UISegmentedControl *)sender; // if segmented control is not declared as an IBOutlet
        if (segmentedControl.selectedSegmentIndex == 0) {
            [viewController1.view setHidden:NO];
            [viewController2.view setHidden:YES];
        }
        else if (segmentedControl.selectedSegmentIndex == 1) {
            [viewController2.view setHidden:NO];
            [viewController1.view setHidden:YES];
        }
    }

确保“y”原点 viewController1 和 viewController2 位于分段控件下方,以便分段控件不会隐藏。

Do the following

  - (void)viewDidLoad //of mainViewController {
        //do viewController1 alloc init     // Gray Color
        [self.view addSubview:viewController1.view];

        //do viewController2 alloc init     //Red Color
        [self.view addSubview:viewController2.view];

        [viewController2.view setHidden:YES];
    }

//assign this method to segmented controls' valueChanged event
    - (IBAction)segmentControlClicked:(id)sender {
        UISegmentedControl *segmentedControl = (UISegmentedControl *)sender; // if segmented control is not declared as an IBOutlet
        if (segmentedControl.selectedSegmentIndex == 0) {
            [viewController1.view setHidden:NO];
            [viewController2.view setHidden:YES];
        }
        else if (segmentedControl.selectedSegmentIndex == 1) {
            [viewController2.view setHidden:NO];
            [viewController1.view setHidden:YES];
        }
    }

Make sure that the 'y' origin viewController1 and viewController2 are below the Segmented Control so that the segmented control is not hidden.

萌酱 2024-12-14 22:52:56

您可以在xib中获取两个视图,并在视图加载时将两个视图添加到主视图中,并为您不想显示的视图设置隐藏属性,然后为开关控制事件设置隐藏属性TRUE或FALSE。

you can take two view in xib and add both view in main view at view did load and set the property hidden for one which you don't want to show, and then set hidden property TRUE or FALSE for switch control event on off.

倾城花音 2024-12-14 22:52:56

尝试以下操作

- (IBAction)segmentControlClicked:(id)sender {
    if (!viewController1) {
        //do alloc init
    }
    if (!viewController2) {
        //do alloc init
    }

    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    if (segmentedControl.selectedSegmentIndex == 0) {
        [viewController1.view setHidden:NO];
        [viewController2.view setHidden:YES];
    }
    else if (segmentedControl.selectedSegmentIndex == 1) {
        [viewController2.view setHidden:NO];
        [viewController1.view setHidden:YES];
    }
}

Try the following

- (IBAction)segmentControlClicked:(id)sender {
    if (!viewController1) {
        //do alloc init
    }
    if (!viewController2) {
        //do alloc init
    }

    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    if (segmentedControl.selectedSegmentIndex == 0) {
        [viewController1.view setHidden:NO];
        [viewController2.view setHidden:YES];
    }
    else if (segmentedControl.selectedSegmentIndex == 1) {
        [viewController2.view setHidden:NO];
        [viewController1.view setHidden:YES];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文