使用UISegmentedControl在两个视图之间切换 [英] Using UISegmentedControl to switch between two views

查看:56
本文介绍了使用UISegmentedControl在两个视图之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了UIViewController的子类.其中有两个属性:

I created a subclass of UIViewController. In it I have two properties of:

@property (nonatomic, retain) IBOutlet UIView *presetsView;
@property (nonatomic, retain) IBOutlet UIView *customView;

我在.xib中添加了一个新的UIView,并在其中添加了一些UI元素.我想隐藏一个视图,并根据按下UISegmentedControl的时间显示另一个视图.

I added a new UIView to my .xib and put some UI elements in it. I want to hide one view, and show the other based on when the UISegmentedControl is pressed.

我的问题是在Interface Builder中,IB提供的原始视图已与文件所有者连接了一个插座.因为我有自己的两个视图(预设和自定义),所以如何在IB中建立插座连接?

My question is in Interface Builder, my original View that was provided me by IB, has an outlet connected to it already to File's Owner. Because I have my own two views, presets and custom, how do I make the outlet connections in IB?

我尝试删除IB提供的原始视图,并将两个新的UIView拖动到画布上.然后,我将插座连接到每个插座.当我推送新的viewController时,出现一个错误,提示我的viewController没有任何视图.然后,当我将文件的所有者连接到为我想首先显示的视图显示的视图"出口时,该应用程序将运行.我不确定这是否是正确的方法,以及为什么它会是正确的方法.ViewController是否总是需要.view属性作为其出口?这就是为什么我需要这样做吗?谢谢.

I tried deleting the original view that was provided by IB and dragged two new UIViews to the canvas. I then connected an outlet to each. When I push my new viewController, I get an error that there is no view for my viewController. Then when I connect the File's Owner to the "view" outlet that shows up for the view I want to show first, the application runs. I wasn't sure if this was the correct way, and why it would be the correct way. Does the ViewController always need a .view property an outlet to it? Is that why I needed to do this? Thanks.

推荐答案

是的,每个UIViewController都有一个根视图.通过从nib文件加载该视图(并设置其 view 属性的结果)或通过调用该视图的 -loadView 方法,它必须能够构造该视图.以编程方式创建它.

Yes every UIViewController has a single root view. It must be able to construct that view when its -loadView method is called either by loading that view from a nib file (and setting its view property as a result) or by creating it programmatically.

在您的情况下,请保留 view 属性视图,不要尝试将其交换.该根视图将已经添加到窗口中,并且更改控制器的引用以指向其他对象只会导致混乱和不确定的行为.而是将两个视图都添加为控制器根视图的子视图,然后根据需要隐藏或显示.

In your case leave the view property view alone and don't try to swap that around. This root view will have already have been added to the window and changing the controller's reference to point to some other object will just cause confusion and undefined behavior. Instead add both of your views as subviews of the controller's root view and hide or show then as needed.

这篇关于使用UISegmentedControl在两个视图之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆