且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将IBOutlet连接到UIView?

更新时间:2022-06-22 09:42:46

您的困惑点在于在代码中创建UI对象与使用Interface Builder / storyboard以图形方式创建它们之间。

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

一个线索是您使用预处理器提示'IBOutlet'。 IB用于Interface Builder ==图形创建(使用storyboard或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(非原子,弱)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];

您需要考虑:您是否添加了storyboard / 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 / storyboard中添加它并将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.

如果要在代码中创建视图并立即将其添加到视图层次结构中,则声明属性的替代方法是在视图上设置(数字)标记属性,然后你可以使用它的superview的 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

可以做的一件事't 做的是在代码中创建视图,然后使用故事板操纵它。

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!