如何将 IBOutlet 连接到 UIView? [英] How do I connect a IBOutlet to a UIView?

查看:30
本文介绍了如何将 IBOutlet 连接到 UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在看到动画后切换视图:

I'm trying to switch views after an animation as seen:

[UIView beginAnimations: @"Fade Out" context:nil];
[UIView setAnimationDelay:1.0];
[UIView setAnimationDuration:0.25];
splash.alpha = 0.0;
[UIView commitAnimations];
[self.view addSubview : view];

[self.view addSubview : view]; 我必须为要添加的视图添加一个名称,所以我制作了一个 IBOutlet 像这样:(在第一个视图控制器的 .h 文件上)

At the [self.view addSubview : view]; I have to add a name for the view for it to add, so I made an IBOutlet like so: (on the first view controller's .h file)

IBOutlet UIView *main;

但是当我尝试将 *main UIView 连接到故事板上的视图时,它不会让我...非常感谢各位.

But when I try to connect the *main UIView to the view on the storyboard, it wont let me... Thanks so much guys.

推荐答案

您的混淆点在于在代码中创建 UI 对象与使用界面生成器/故事板以图形方式创建它们之间.

Your point of confusion is between creating UI objects in code vs creating them graphically using Interface Builder / storyboard.

一个线索是您对预处理器提示IBOutlet"的使用.IB 用于界面生成器 == 图形创建(使用故事板或 xib 文件).

One clue is your use of the preprocessor hint 'IBOutlet'. IB is for Interface Builder == graphic creation (using a storyboard or xib file).

如果在故事板中创建...

  • 像您所做的那样创建一个 IBOutlet 属性,尽管完全正确的语法是
    @property (nonatomic,weak) IBOutlet UIView *main;

将自定义视图"视图从对象库拖到故事板场景中.

drag a "custom view" view from the object library to your storyboard scene.

IBOutlet 的全部目的是为您提供对故事板场景中可在代码中使用的项目的引用.

The entire purpose of an IBOutlet is to give you a reference to an item in your storyboard scene that you can use in your code.

您不需要这样做:

  [self.view addSubview : view];

因为它已经创建并添加到您的故事板中.确保它在视图层次结构中的位置与您期望的一样.

as it is already created and added in your storyboard. Make sure it is located as you expect in your view hierarchy.

如果在代码中创建...

在@interface 中声明一个属性

Declare a property in your @interface

  @property (nonatomic, strong) UIView *main;

(不需要 IBOutlet,因为您没有在故事板中将其链接起来.如果您没有立即将其分配给视图层次结构,则声明为强"而不是弱").

(No need for IBOutlet as you aren't linking it up in your storyboard. Declared 'strong' instead of 'weak' in case you don't immediately assign it to a view hierarchy).

在添加此行之前

  [self.view addSubview : view];

您需要考虑:您是否添加了一个在您的故事板/xib 中不存在的新视图?如果是这样,则在创建它之前无法添加它.(在故事板中添加 IS 视图没有意义,因为它已经存在于故事板场景中).

you need to consider: are you adding a new view that does not exist in your storyboard/xib? If so, you cannot add it until you have created it. (Adding a view that IS in your storyboard doesn't make sense, as it is already there, in your storyboard scene).

因此 - 正如您在代码中所做的那样 - 您需要在添加视图之前创建视图.

So - as you appear to be doing this in code - you need to create the view before adding it.

  UIView* myView = [[UIView alloc] init];

设置它的 frame 属性,以便我们知道当你将它添加到视图层次结构时它会出现在哪里

set it's frame property so that we know where it will appear when you add it to the view hierarchy

  myView.frame = CGRectMake (CGFloat x, CGFloat y, CGFloat width, CGFloat height);

将新创建的视图分配给属性

Assign your newly-created view to the property

  self.main = myView;

将其添加到您的视图层次结构

Add it to your view hierarchy

  [self.view addSubview : myView];

现在你可以使用self.main在代码中引用它.这就像您将它添加到 IB/故事板并通过 CRTL 将参考链接拖到您的 IBOutlet 中一样.

Now you can refer to it in code by using self.main. This is just as it would be if you were to have added it in IB/storyboard and CRTL-dragged a reference link to your IBOutlet.

如果您在代码中创建视图并立即将其添加到视图层次结构中,则声明属性的另一种方法是在视图上设置(数字)标记属性,然后您可以使用它的超级视图的 viewWithTag: 方法

If you are creating your view in code and immediately adding it to a view hierarchy, an alternative to declaring a property is to set the (numerical) tag property on the view, then you can refer to the view using it's superview's viewWithTag: method

不能做的一件事是在代码中创建视图,然后使用情节提要对其进行操作.

The one thing you can't do is create the view in code, then manipulate it using the storyboard.

我希望这些都是有用的,我担心我可能会进一步混淆你!

I hope this is all useful, I fear I may be confusing you further!

这篇关于如何将 IBOutlet 连接到 UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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