Design-time properties in IB? - iphone

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

Related

Xcode design data

Can I add to my interface builder design data that appears only in design mode?
For example - add text to labels, items to a list view, pictures inside imageviews, etc., all in design mode while using Xcode. However, in runtime, none of it will appear.
If you are coming from the Android world, I'm looking for the equivalent of the "tools" xml attribute.
There is no Hidden during runtime option AFAIK. But you can use Hidden under Attribute inspector->View->Drawing to manually hide each UI elements.

Enterprise Architect class fill color

I have a very stupid question! I have created class diagram and right now I am trying to change class background fill color. I have not found any info in the help how to do that. Any suggestions?
Not a stupid question at all. There are a couple of different ways of doing this, depending on the scope you want.
To change the colour of one class in a single diagram, right-click it in the diagram. This opens up the context menu, but also opens a mini-toolbar. In this toolbar, the third icon from the left is the class fill colour. This toolbar can also be opened by selecting the class, and then clicking the middle of the three icons EA displays to the right of the class (the paintbrush).
Using this toolbar you can change the class colour locally. It does not affect how the class is displayed in other current or future diagrams, it is specific to the diagram you're in.
If you want to change the appearance of a class in all diagrams, right-click it and select Appearance -- Default Appearance. If you change the colour here, it will affect all future diagrams and all current diagrams except the ones where you've specified a local colour for the class as above.
Finally, you can change the EA-wide colour scheme in Tools -- Options -- Standard Colors. This affects all elements in all diagrams, unless you've specified something else for an element using either of the above methods.
In addition to these, if you create a profile you can specify a different appearance for a particular stereotype using shape scripts. But that's a different question.

What's the best way to customise all monotouch.dialog TableViewCells to the same style (Background, etc..)?

Im using Monotouch.Dialog. I'd like to customise the look and feel of the tables. Nothing too major, I can change the background of the tableview easily, the cell borders and use my own custom header and footer views.
What is the best way of customising ALL the cells displayed in a TableView?
Is there some point in the Monotouch.Dialog code I can intercept the generation of cells and customise them in one place, so they all, for example have the same background color, font type/color custom detail disclosure icon etc.
Or do I have to individually customise all the different types (BooleanElement, StringElement, FloatElement etc)
Note: Im aware I can use StyledString element, but this only covers the string element type. I want to customise all the cells displayed in my own custom style.
There is no easy way of doing this in the current codebase for MonoTouch.Dialog.
What you could try is the Beta for MonoTouch which comes with iOS5 bindings. If you are willing to only support iOS5, you can use the Appearance class to customize a few properties (not all, sadly). You can customize things like the background view across the board:
UIView.Appearance.BackgroundColor = UIColor.Red;
You could also take a look at https://github.com/RobertKozak/MonoMobile.Views which started as a fork from MonoTouch.Dialog but morphed into a similar but now totally different library. It has styling built in.

Best approach to create a "settings" dialog page in iPhone [duplicate]

This question already has answers here:
How to create a UITableView with editable components?
(2 answers)
Closed 3 years ago.
I need to present the user with a screen with controls (i.e. a dialog). It will display some labels and different types of controls so that the user can do some configuration:
select a color (custom color picker)
select a line width (slider)
select one value from a list of values (UISegmentedControl?)
enable / disable one option (On/Off)
...
I am evaluating different alternatives:
Create the layout with labels and controls in InterfaceBuilder
Use a UITableView, so that each cell contains a label and its control
I like option 2 because it is similar to iPhone Settings app, and it makes the screen look organized, giving you sections, scroll..., but it is a real pain to create in code all different cell layouts, taking into account each row heights...
The solution has to be easiliy maintainable, allowing easy reuse of code, DRY (low repetition), data-driven...
Is there a recommended or standard aproach to do this?
Instead of implementing it yourself you might also like to consider some of the open source solutions. I use InAppSettingsKit in my app. There are a few minor UI glitches but it works well. I also evaluated mySettings (indeed I submitted a couple of patches) and InAppSettings.
Even if you don't use them, you might be able to crib a few ideas from them.
You can create the tableview cells in IB and then just create IBOutlets to each cell, and then return each cell in the data source method (forgotten the exact name of it, its where you normally create cells)
You could create a cell in IB for each cell or create different types of cells in IB, say one slider based cell, etc and programmatically change the text, etc.
This is staticly creating a tableview, theres an Apple guide on it somewhere.
you said it. use a UITableView and create custom UITableViewCells (probably in Interface Builder!) and add these in.
Also, as far as selecting a value from a list of values.. a drill in UITableView (embed the root UITableView in a navigation controller) with all the options and then a checkmark is probably advisable (this is how most apps handle this behavior). The On/Off button is more suited for UISegmentedControl.
There are great examples of this in this book.
Good luck.

How can I enable multiple segments of a UISegmentedControl to be selected?

Lets say my UISegmentedControl has 8 numbered segments. I would like for the user to be able to turn on 2, 3, or more of them at once. Toggling them. Essentially like a bits in a byte. Is this possible? I believe it is on regular Mac OS X but I can't seem to find a way to do it in the iPhone SDK.
If I have to simulate this by putting buttons into a view, is there any way to do the following:
Round the corners of the view so that it looks like the "bar" style UISegmentedControl?
Use the built-in backgrounds the "bar" style has on the buttons?
Give the buttons a shadow like the whole "bar" style has? (not the text)
Update: The custom control I mentioned here no longer works under iOS 13.
This is the best custom control I've found that allows multiple segments to be selected concurrently:
https://github.com/yonat/MultiSelectSegmentedControl
This one is a subclass of UISegmentedControl, which is convenient. I've forked it here:
https://github.com/stewartmacdonald/MultiSelectSegmentedControl
and added some code examples to the ReadMe and added a method that allows you to get an NSArray of the titles of all selected segments.
This isn't possible using UIKit. I would recommend creating a custom control, or an array of UISwitch controls, representing each of the options in your UISegmentedControl.
I think the simplest way is to create your own segmentedcontrol with UIButton.
Marco
There appears to be a way to do this in XCode now. In the Attributes Inspector, there is a section with Segment, Title, Image, Behavior. The behavior options are "Enabled" and "Selected". You can selecte "Selected" for multiple segments.
I recommend you try https://github.com/tayhalla/THSegmentedControl
it is an actual subclass of UISegmentedControl so it fits nicely with Interface builder and everything.