UIPickerView選擇控件實現選擇輪播效果(轉輪效果)

UIPickerView爲用戶提供了選擇器功能,使用戶以更好的體驗方式實現數據的選擇,如圖:



UIPickerView控件的使用方法:(創建好根視圖:MainViewController)

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController<UIPickerViewDelegate>
{
    UIPickerView *pickerView;
    UILabel *contentview;
    NSArray *content;  // 星座;
}

@end

實現部分:


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)loadView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    self.view = view;
    view.backgroundColor = [UIColor yellowColor];
    [view release];
    // 初始化數據, 這些數據將顯示在picker中
    content = [[NSArray alloc] initWithObjects:@"水瓶座", @"雙魚座", @"白羊座        ", @"金牛座", @"雙子座", @"巨蟹座", @"獅子座", @"處女座", @"天秤座", @"天蠍座", @"射手座", @"白羊座",nil];
    // 設置選擇器
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 150, 320, 216)];
    // 設置代理
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;
    [self.view addSubview:pickerView];
    contentview = [[UILabel alloc] initWithFrame:CGRectMake(80, 80, 100, 40)];
    contentview.backgroundColor = [UIColor clearColor];
    [self.view addSubview:contentview];
}
#pragma mark--處理方法
// 返回顯示的數列
- (NSInteger)numberOfRowsInComponent:(NSInteger)component
{
    return 1;
}
// 返回當前列顯示的行數
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [content count];
}
// 設置當前的內容,如果行沒有顯示則自動釋放
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [content objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    NSString *result = [pickerView pickView:pickerView titleForRow:row forComponent:component];
    
    NSString *result = nil;
    result = [content objectAtIndex:row];
    NSLog(@"result:%@", result);
    contentview.text = result;
    [result release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章