Cocoa-Matic

iPhone, iPod touch, iPad tutorials, examples and sample code. Come and get it!

Friday, August 20, 2010

Create UIPickerView programmatically

The UIPickerView is one of the input controls in iOS for displaying and selecting a list of data. Most tutorials implement this control in Interface Builder, which is fine, but I like to have more control and do most everything in code. There are several components to doing this 100% programmatically, which I will demonstrate below...

The first thing we have to do is include the UIPickerViewDelegate in the interface of our class .h file.
@interface myViewController : UIViewController<UIPickerViewDelegate>
Read more »

Labels: , ,

Friday, July 30, 2010

Creating UIViews programmatically

If you want to create a new UIViewController object without using a Nib file in Interface Builder, you must use the loadView method. It's rather simple, but key to doing things programmatically.
- (void)loadView {
 CGRect viewFrame;
 viewFrame = CGRectMake(0, 0, MASTER_WIDTH, MASTER_HEIGHT);
 self.view = [[[UIView alloc] initWithFrame:viewFrame] autorelease];
}
(Note: MASTER_WIDTH and MASTER_HEIGHT are simply constants defining the width and height of the screen.)

Once your view is created, you are free to implement, as usual, viewDidLoad, viewWillAppear, viewDidAppear, etc.

Labels: , , ,


« Older Entries