2011年1月27日 星期四

[記事] HOWTO UIPickerView

In short :

1. New the class files and name it as something like PickViewController

2. Edit PickViewController.h

#import <UIKit/UIKit.h>
@interface SelFormUIViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
UIPickerView *startSitePicker;
NSArray *sites; 
}
@property (nonatomic, retain) IBOutlet UIPickerView *startSitePicker;
@property (nonatomic, retain) NSArray *sites;
@end

3. Modify PickViewController.xib

  1. drag a Pick View Component from library window to inspector window
  2. set dataSource and delegate to file owner and reference to startSitePick of file owner in connection window


4. Modify PickViewController.m (Remember to implement the 3 functions : (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView , (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component, NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component. Xcode won't generate them for you.

#import "SelFormUIViewController.h"
@implementation SelFormUIViewController
@synthesize sites;
@synthesize startSitePicker;
- (void)viewDidLoad {
NSArray *x = [[NSArray alloc] initWithObjects:@"111", @"2222", @"3333", @"4444", nil];
self.sites = x;
[x release];
    [super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    self.startSitePicker = nil;
    self.sites = nil;
    [super viewDidUnload];
}

- (void)dealloc {
[startSitePicker release];
[sites release];
    [super dealloc];
}

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [sites count];
}
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [sites objectAtIndex:row];
}
@end

5. build and run

Ref  :http://www.youtube.com/watch?v=I6kX4ZHo0SU&feature=channel (Single Pecker)

0 意見:

張貼留言