开发者

Can I define an outlet in a .nib without specifying a controller?

I have a nib that has a button that I would like the relevant controller to have access to. But right now 开发者_开发问答the owner is a generic NSViewController, and inside AppDelegate I assign the nib to the controller like:

[browseViewController initWithNibName:@"BrowseView" bundle:[NSBundle mainBundle]];

But is it possible within the nib file to assign a button's referencing outlet generically? In other words, to give the outlet a name and point it to File's Owner, and then if/when the nib is connected to the controller if that controller has an outlet with the same name it just works?


The File Owner has to have the certain outlet and/or the action when you connect it in IB. Even if you could connect it to an outlet that does not exist, an exception will be raised when the nib is being loaded. I you want to check it make an outlet, connect it to an object, save the nib, delete the outlet from the object and run the application. NSUnknownKeyException will be raised.

The easiest way would be to use a common superclass of your controller. If the controllers cannot be derived from the common ancestor, the only workaround I see is to make a protocol where you define your outlet properties and actions. Make an object that conforms to the protocol

MyObject: NSObject < MyNibProtocol >

now use this object in the nib to connect the outlets and actions. However, the properties and methods cannot be optional, you have to implement them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜