Interface Builder不允許連接到自定義UIView類?

[英]Interface Builder won't allow connections to custom UIView class?


Using Xcode 4.3.3, I can't figure out how to connect outlets in a custom UIView class with objects created in Interface Builder.

使用Xcode 4.3.3,我無法弄清楚如何將自定義UIView類中的出口與Interface Builder中創建的對象連接起來。

In one ViewController, I have a variety of buttons, sliders, etc that I'm trying to group into Views. So, within that ViewController in IB, I added 3 views. Only 1 view will be visible at any given time.

在一個ViewController中,我有各種各樣的按鈕,滑塊等,我試圖將它們分組到Views中。因此,在IB的ViewController中,我添加了3個視圖。在任何給定時間只能看到1個視圖。

I derived custom UIView classes to handle each of these 3 views. My view controller instantiates each of the classes. I selected the view(s) in IB, opened the Identity Inspector and set the Class(es) to my custom class(es). But when I tried dragging connections from the view and/or it's constituent controls to the custom view's .h file IB won't add a connection.

我派生了自定義UIView類來處理這3個視圖中的每一個。我的視圖控制器實例化每個類。我在IB中選擇了視圖,打開了Identity Inspector並將Class設置為我的自定義類。但是,當我嘗試從視圖和/或它的組成控件拖動連接到自定義視圖的.h文件時,IB不會添加連接。

IB allows me to add outlets/actions by dragging to the parent view controller's .h, but not to the custom view's .h file. I thought once I set the View's class to be my custom class, I could drag outlets for the view's components to my custom class rather than in the view controller.

IB允許我通過拖動到父視圖控制器的.h來添加插座/操作,但不能添加到自定義視圖的.h文件。我想過,一旦我將View的類設置為我的自定義類,我就可以將視圖組件的出口拖到我的自定義類而不是視圖控制器中。

This question seems identical to mine: how to connect UIview outlets to a custom subview The two solutions (manually adding outlets and setting the view's class in IB) did not change the behavior for me. Here are the manual outlets I added:

這個問題看起來與我的相同:如何將UIview插座連接到自定義子視圖這兩個解決方案(手動添加插座並在IB中設置視圖的類)並沒有改變我的行為。這是我添加的手動插座:

customView3.h

#import <UIKit/UIKit.h>

@interface customView3 : UIView

@property (retain) IBOutlet customView3 *view3;
@property (retain) IBOutlet UISlider *slider;

@end

customView3.m

#import "customView3.h"

@implementation customView3

@synthesize view3, slider;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code
    }
    return self;
}
@end

What am I missing here? What else do I need to set/check in order to add outlets to my custom UIView rather than the view controller?

我在這里想念的是什么?為了向我的自定義UIView而不是視圖控制器添加插座,我還需要設置/檢查什么?

5 个解决方案

#1


15  

Note that I did get this to work today, but had to insert/type the outlets by hand in the derived class, then I could drag from the header file to the UI element in the Storyboard, but not the other way around.

請注意,我今天確實讓它工作,但是必須在派生類中手動插入/輸入插座,然后我可以從頭文件拖到Storyboard中的UI元素,但不是相反。

#2


2  

I opened a bug with Apple. If anything interesting happens, I'll update this answer.

我向Apple公開了一個錯誤。如果發生任何有趣的事情,我會更新這個答案。

Updated 10/4/13: After much research, it seems the root of the problem is that IB believes that I'm trying to connect objects to properties within a class, that I'm going to then create a swarm of instances of this class and then IB won't be able to tell which instance should handle the messages. So, it disallows the connection on the belief that I'm (potentially) making simultaneous connections from one object to multiple copies of a property across several instances of the class.

更新於2013年10月4日:經過大量研究后,問題的根源似乎是IB認為我正在嘗試將對象連接到類中的屬性,然后我將創建一組這樣的實例class然后IB將無法告訴哪個實例應該處理消息。因此,它不相信我相信我(可能)在一個類的幾個實例中同時從一個對象到一個屬性的多個副本進行連接。

My view is that I only want a single instance of the custom UIView, and that I should be able to tell IB, "don't panic, you're only dealing with this one instance."

我的觀點是,我只想要一個自定義UIView的單個實例,並且我應該能夠告訴IB,“不要驚慌,你只是處理這個實例。”

I'm now focused on a new project and haven't had a chance to retry this in Xcode 5; however, given the info above, I'm not optimistic it will work in Xcode 5, either.

我現在專注於一個新項目,並沒有機會在Xcode 5中重試這個;但是,鑒於上面的信息,我不樂觀它也適用於Xcode 5。

#3


1  

What you're trying to do is certainly both sensible (IMO) and possible. XCode is however quite quirky when it comes to establishing connections to outlets in subviews.

你要做的當然是明智的(IMO)和可能的。然而,當在子視圖中建立與出口的連接時,XCode非常古怪。

I've found that cleaning, rebuilding and sometimes restarting XCode tends to help, but sometimes it just doesn't work. In some cases I got it working by manually creating the outlet and dragging from the outlet to the control rather than the other way.

我發現清理,重建和有時重新啟動XCode往往有所幫助,但有時它只是不起作用。在某些情況下,我通過手動創建插座並從插座拖動到控件而不是其他方式來使其工作。

This is not very helpful, I know, but I just wanted to make it clear that this is supposed to work and when it doesn't it is most certainly due to a bug in XCode.

我知道,這不是很有幫助,但我只是想明確表示這應該可以工作,當它沒有時,它肯定是由於XCode中的一個錯誤。

#4


0  

In my experience, you add IBAction and IBOutlet to subclasses of UIViewController and not subclasses of UIView. So within the MVC model, your "1" UIViewController has all of these IBOutlets in it and then you use the data from these IBOutlets to update your views.

根據我的經驗,您將IBAction和IBOutlet添加到UIViewController的子類,而不是UIView的子類。因此,在MVC模型中,您的“1”UIViewController中包含所有這些IBOutlet,然后您使用這些IBOutlet中的數據來更新您的視圖。

So you are fine creating the UIViews objects in IB and then creating the corresponding classes in code and then changing the class of the UIViews objects in IB to your UIView subclass. Just put all IBOutlets in the UIViewController that has all the UIViews under it, get the values you need from the IBOutlets and then pass them down to (set them in) your UIView subclasses.

因此,您可以在IB中創建UIViews對象,然后在代碼中創建相應的類,然后將IB中的UIViews對象的類更改為您的UIView子類。只需將所有IBOutlets放在UIViewController中,其中包含所有UIViews,從IBOutlets獲取所需的值,然后將它們傳遞給(設置它們)你的UIView子類。

Hope this helps.

希望這可以幫助。

#5


0  

I had the same problem and found a workaround:

我遇到了同樣的問題並找到了解決方法:

1) Open the Assistant Editor with customView3.xib on the left and customView3.h on the right

1)使用左側的customView3.xib和右側的customView3.h打開Assistant Editor

2) ctrl + drag from UISlider in customView3.xib to the customView3.h code on the right

2)ctrl +從customView3.xib中的UISlider拖動到右側的customView3.h代碼

3) An option to create a new Outlet connection will pop up.

3)將彈出創建新Outlet連接的選項。

4) Name the connection and click connect

4)為連接命名,然后單擊“連接”

It will then create a connected IBOutlet.

然后它將創建一個連接的IBOutlet。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/02/17/70688533d8d8593abe2fa25d19dc8c9e.html



 
  © 2014-2022 ITdaan.com 联系我们: