gpt4 book ai didi

ios - 使用 UISegmentedControl 在 View 之间切换

转载 作者:行者123 更新时间:2023-11-29 10:27:54 24 4
gpt4 key购买 nike

从标题中可以清楚地看出,我正在尝试找到一种使用分段控件在 View 之间切换的方法。实际上,我已经得到了一些东西并且它正在工作。您可以在下面找到它:

- (IBAction)segmentChanged:(id)sender{
UISegmentedControl *segmentedControl = sender;
switch (segmentedControl.selectedSegmentIndex) {
case 0:
self.currencyContainer.hidden = NO;
self.goldContainer.hidden = YES;
self.alarmContainer.hidden = YES;
break;
case 1:
self.currencyContainer.hidden = YES;
self.goldContainer.hidden = NO;
self.alarmContainer.hidden = YES;
break;
case 2:
self.currencyContainer.hidden = YES;
self.goldContainer.hidden = YES;
self.alarmContainer.hidden = NO;
break;
default:
break;
}
}

因此,这取决于原理变得隐藏和可见。我认为它很常用,因为一般人都有这样的解决方案,正如我从互联网上搜索的那样。但是,我希望它略有不同。使用我的代码,所有的 View 都是一次加载的,但我想在只有 View 出现在屏幕上时加载。如果我从 0 切换到 1 索引段,那么我想加载第一个 View 并显示在屏幕上。

我该如何管理它?

谢谢。

最佳答案

我在这里回答了一个类似(不一样,但结果相似)的问题:How To Switch Views With NSNotifications

基本上,您可以使用 NSNotifications 切换 View 。您的第一个 View 将加载并使用 NSNotifications,您可以将通知发送到自定义类,该类通过更改 View 进行监听和响应;当您在 UISegmentControl 上选择一个索引时,您的 View 将会改变。

在您的代码中,在您的 NSObject 类 ButtonHandler 中,它看起来像这样:

- (IBAction)segmentChanged:(id)sender{
UISegmentedControl *segmentedControl = sender;
switch (segmentedControl.selectedSegmentIndex) {
case 0:
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyButtonPressed1" object:self];
break;
case 1:
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyButtonPressed2" object:self];
break;
case 2:
[[NSNotificationCenter defaultCenter] postNotificationName:@"notifyButtonPressed3" object:self];
break;
default:
break;
}
}

然后只需按照我在另一篇文章中列出的其余代码即可。

效果是这样的:

Switch Views with NSNotifications

更新

我已经在这个链接上创建了一个关于如何做到这一点的教程:iOS Custom Navigation with UISegmentedControl

要将 View 设置为与绑定(bind)的设备相同,请使用以下命令:

//this will get the size of your device view 
//and set it to width and height aka w & h
int w = self.view.frame.size.width;
int h = self.view.frame.size.height;

//THIS SETS THE SIZE AND POSITION OF THE NEW CONTENT
self.content.view.frame = CGRectMake(0, 0, w, h);

关于ios - 使用 UISegmentedControl 在 View 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31245281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com