简体   繁体   English

使用UISegmentedControl在视图之间切换

[英]Switching Between Views with a UISegmentedControl

As it is clear from the title, I'm trying to find a way to switch between views using segmented control. 从标题可以明显看出,我正在尝试找到一种使用分段控件在视图之间进行切换的方法。 Actually, I got something already and it is working. 实际上,我已经得到了一些东西并且它正在工作。 You can find it below: 您可以在下面找到它:

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

So, it depends on the principle becoming hidden and visible. 因此,这取决于隐藏和可见的原理。 I think it is so common to use, because generally people had the solutions in this way, as I searched from the internet. 我认为使用起来如此普遍,因为正如我从互联网上搜索到的那样,通常人们以这种方式获得解决方案。 However, I want it to be slightly different. 但是,我希望它有所不同。 With my code, all the views are loaded at once, but I want to load when just the views come on the screen. 使用我的代码,所有视图都被立即加载,但是我只想在屏幕上出现视图时加载。 If I switch from 0 to 1 index segment, then I want to load 1st view and show on the screen. 如果我从0切换到1索引段,那么我想加载第一个视图并在屏幕上显示。

How can I manage it? 我该如何管理?

Thanks. 谢谢。

I have answered a similar (not the same, but similar result) question here: How To Switch Views With NSNotifications 我在这里回答了一个类似(但不相同,但结果相似)的问题: 如何使用NSNotifications切换视图

Basically you can switch views using NSNotifications. 基本上,您可以使用NSNotifications切换视图。 Your fist view will load and using NSNotifications you can send a notification to a custom class that listens and responds by changing the view; 您的拳头视图将加载,并使用NSNotifications您可以将通知发送到自定义类,该类通过更改视图来监听和响应; when you select an index on the UISegmentControl, your views would change. 当您在UISegmentControl上选择索引时,您的视图将改变。

It would look like this in your code, in your NSObject Class ButtonHandler : 在您的代码中,在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;
    }
}

Then simply follow the rest of the code I have set out in the other post. 然后只需遵循我在另一篇文章中列出的其余代码即可。

This is the effect: 这是效果:

使用NSNotifications切换视图

UPDATE UPDATE

I have created a tutorial on how to do this at this link: iOS Custom Navigation with UISegmentedControl 我在此链接上创建了有关如何执行此操作的教程: 使用UISegmentedControl的iOS自定义导航

To set the view to be the same as the device bound use the following: 要将视图设置为与设备绑定相同,请使用以下命令:

//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);

You can achieve this by using Lazy Stored Properties something like this : 您可以通过使用Lazy Stored Properties来实现此目的,例如:

lazy var currencyContainer:UIView = {
     var newCurrencyContainer = UIView()
     // your initialization code
     return newCurrencyContainer
}()

A lazy stored property is a property whose initial value is not calculated until the first time it is used. 惰性存储的属性是直到首次使用才计算其初始值的属性。 - Apple Docs -Apple文件

OR 要么

By using optional variable like this : 通过使用像这样的可选变量:

var goldContainer:UIView?

and later checking 然后检查

if let goldContainer = goldContainer {
     // view is already created
} else {
     // your custom init
     // goldContainer = UIView()
}

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM