1.建立專案(請參考HelloWorld教學),放置PickerView及Label。
2.建立屬性及參考接口(Referencing Outlet),在Referencing Outlet中New Referencing Outlet接口按下mouse拖移至DLIViewController.h,會新增下列程式如下;
@property (strong, nonatomic) IBOutlet UILabel *result;
3.對pickerView重覆步驟2,另外也在Outlets中將datasource及delegate兩個接口拖移至File's Owner。
4.輸入程式
5.DLIControllerView.h程式如下;
#import
@interface DLIViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>{
NSArray *array;
}
@property (strong, nonatomic) IBOutlet UIPickerView *picker;
@property (strong, nonatomic) IBOutlet UILabel *result;
@end
6.DLIControllerView.m程式如下;
#import "DLIViewController.h"
@interface DLIViewController ()
@end
@implementation DLIViewController
@synthesize picker;
@synthesize result;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
result.text = [array objectAtIndex:row];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [array objectAtIndex:row];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return 3;
}
@end
6.執行結果
沒有留言:
張貼留言