xcode 如何将 IBOutlet 连接到 UIView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14576515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:39:20  来源:igfitidea点击:

How do I connect a IBOutlet to a UIView?

xcodeuiviewios6uistoryboardiboutlet

提问by Jon Sullivan

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];

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

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

IBOutlet UIView *main;

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

但是当我尝试将 连接*mainUIView到情节提要上的视图时,它不会让我...非常感谢大家。

回答by foundry

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

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

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

一条线索是您使用预处理器提示“IBOutlet”。IB 用于界面生成器 == 图形创建(使用故事板或 xib 文件)。

If creating in storyboard...

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

  • create an IBOutlet property as you have done, although the full correct syntax is
    @property (nonatomic, weak) IBOutlet UIView *main;

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

  • CTRL-drag from the view to the viewController. You should get presented with a list of suitable items to connect to. Your IBOutlet should be in the list. select it.
  • 像您一样创建一个 IBOutlet 属性,尽管完全正确的语法是
    @property (nonatomic, weak) IBOutlet UIView *main;

  • 将“自定义视图”视图从对象库拖到故事板场景中。

  • 按住 CTRL 键从视图拖动到 viewController。您应该会看到要连接的合适项目的列表。您的 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.

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

You won't need to do this:

你不需要这样做:

  [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.

因为它已经创建并添加到您的故事板中。确保它在您的视图层次结构中按您的预期定位。

If creating in code...

如果在代码中创建...

Declare a property in your @interface

在@interface 中声明一个属性

  @property (nonatomic, strong) UIView *main;

(No need for IBOutletas 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).

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

Before you add this line

在添加此行之前

  [self.view addSubview : view];

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).

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

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

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

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

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

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

  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];

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.

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

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

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

The one thing you can'tdo 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!

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

回答by Lin

I am late to this. But let me post what I found out recently anyways.

我迟到了。但无论如何,让我发布我最近发现的内容。

I have this situation :

我有这种情况:

1) BaseViewController that contains MenuView(Custom UIView)

1)包含MenuView(自定义UIView)的BaseViewController

2) MenuView contains menuTable which holds all the menu items

2) MenuView 包含 menuTable ,其中包含所有菜单项

3) On StoryBoard I have BaseViewController scene with MenuView and MenuTableView

3) 在 StoryBoard 上我有带有 MenuView 和 MenuTableView 的 BaseViewController 场景

I could have IBOutlet reference to menuTable that we normally do by dragging the table on storyboard to the BaseViewController.h. But I want custom MenuView to be responsible for populating the MenuTable as a separation concern. In order to do that I need IBOutlet reference connection from MenuView.h to the menu table on storyboard.

我可以通过将故事板上的表格拖到 BaseViewController.h 来让 IBOutlet 引用我们通常所做的 menuTable。但我希望自定义 MenuView 负责填充 MenuTable 作为分离问题。为了做到这一点,我需要从 MenuView.h 到故事板上的菜单表的 IBOutlet 参考连接。

So this is what I did :

所以这就是我所做的:

1) First create IBOutlet property of the table in MenuView.h @property (weak,nonatomic) IBOutlet UITableView *menuTable;

1)首先在MenuView.h中创建表的IBOutlet属性@property (weak,nonatomic) IBOutlet UITableView *menuTable;

2) Once the property is created, there is a little circle just left side of the property declaration signifying that this is IBOutlet variable.

2) 创建属性后,属性声明左侧有一个小圆圈,表示这是 IBOutlet 变量。

3) Now with StoryBoard open and MenuView.h open in Assistant Editor in XCode, I can click on the circle and drag it to table on the storyboard. (Note: If I try drag the table from StoryBoard to MenuView.h , it doesn't let me to. Note sure why)

3) 现在在 XCode 的助理编辑器中打开 StoryBoard 并打开 MenuView.h,我可以单击圆圈并将其拖到故事板上的表格中。(注意:如果我尝试将表从 StoryBoard 拖到 MenuView.h ,它不允许我这样做。请注意为什么)

That's it. Now I've made the IBOutlect connection from custom UiView to StoryBoard.

就是这样。现在我已经建立了从自定义 UiView 到 StoryBoard 的 IBOutlect 连接。

Note : I am using XCode 7 and targeting iOS 7 and above devices. (If that makes any difference)

注意:我使用的是 XCode 7 并针对 iOS 7 及更高版本的设备。(如果这有什么不同)

回答by CRDave

If you are new to Xcode or Storyboards than you should take a look at this basic Tutorial:

如果您是 Xcode 或 Storyboards 的新手,那么您应该看看这个基本教程:

Beginning Storyboards in iOS 5 Part 1

iOS 5 第 1 部分中的开始故事板

Beginning Storyboards in iOS 5 Part 2

iOS 5 中的开始故事板第 2 部分

回答by bindsniper001

Are you declaring the view as a property?

您是否将视图声明为属性?

The syntax should be something like this:

语法应该是这样的:

@property (nonatomic, weak) IBOutlet UIView *main;