开发者

Design-time properties in IB?

开发者 https://www.devze.com 2022-12-30 04:11 出处:网络
Coming from the .NET world into ObjC and iPhone development, I\'m used to providing design-time support for my UI classes. For example, say that I have a custom button class with a color property. I w

Coming from the .NET world into ObjC and iPhone development, I'm used to providing design-time support for my UI classes. For example, say that I have a custom button class with a color property. I want to be able to set this color in design time from IB. How can I accomplish this? Is it even possible? 开发者_JS百科Worth the effort?


It isn't possible to create custom IB plugins for iOS. The best you can do is use a generic UIView with a custom class in your nib and set the properties programatically.


You would usually create an IBPlugin for that, which can contain a single custom element or an entire collection.

Interface Builder Plug-In Programming Guide (Official Guide)

Custom views in Interface Builder using IBPlugins (Inofficial Tutorial)
(This tutorial should get you going! It covers custom attribute settings in IB.)

Be warned though: Xcode 4 does not support custom built IBPlugins!
(And sadly this still holds true for Gold Master. Hopefully there'll be an official/native solution once the NDA is lifted. Until then make sure to backup your IB before installing any copy of Xcode 4.)


Whilst waiting for the 'User Defined Runtime Attributes' feature to become more widely supported, you can still use the 'tag' property of UIView. This is just an integer, but you can use that as a key into a dictionary to access additional properties. So for example define your design-time attributes in a JSON file, load that into a NSDictionary and walk over the view hierarchy at runtime, applying the attributes you've defined.

It's not ideal but better than nothing, or implementing loads of tiny custom classes just to use a custom font or whatever.


This is actually very easy to do. In InterfaceBuilder, select the button and open the Attribute Inspector (hotkey: [command]+1). Select the first tab on the top of the Attribute Inspector window, and look at the section titled "View". The entry labeled "Background" will have a colored rectangle next to it, which will likely be split diagonally, half black and half white. That's an indication that the button's background is currently transparent. Click that rectangle, and the color selection box will appear. Don't forget to change the value of the "alpha" slider in the color selection box, or the button will remain completely transparent.

Here's an image of the attribute inspector, configured like I described.


For arbitrary types (such as an NSColor property), Interface Builder plugins are your only option, as others have mentioned.

However, for certain limited types, there is an easier solution. If your custom property is a string, numeric primitive, or boolean, you can use the "Runtime Attributes" inspector in Interface Builder to define these properties at design time.

However, I see that you have the 'iphone' tag on your questoin. Runtime Attributes are not currently available on iOS.

0

精彩评论

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