iOS 基础小结之常用控件三

来源:互联网 发布:查看linux用户密码 编辑:程序博客网 时间:2024/06/01 10:13

UIPickerView

(1)

先声明要用的协议<UIPickerViewDataSource,UIPickerViewDelegate>

声明变量

@property (strong,nonatomic)NSArray * books;

@property (strong,nonatomic)NSArray * authors;

@property (strong, nonatomic) IBOutlet UIPickerView *picker;

- (void)viewDidLoad

{

    [superviewDidLoad];

    _authors = [NSArrayarrayWithObjects:@"li",@"zhang", @"liu",nil];

    _books = [NSArrayarrayWithObjects:@"111",@"222", @"333",nil];

    self.picker.dataSource =self;

    self.picker.delegate =self;

}

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 2;//两列,有几列返回几

}

- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if (component == 0) {

        return_authors.count;

    }

    return_books.count;

}//返回每一列的数目

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    if (component == 0) {

        return [_authorsobjectAtIndex:row];

    }

    return [_booksobjectAtIndex:row];

}//返回选择的列的选择的行

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    NSArray * tmp = component == 0 ? _authors : _books;

    NSString * tip = component == 0 ? @"authers " : @"books";    

    UIAlertView * alert = [[UIAlertViewalloc]initWithTitle:@"notice"message:[NSStringstringWithFormat:@"you have chose %@ : %@",tip,[tmpobjectAtIndex:row]] delegate:nilcancelButtonTitle:@"OK"otherButtonTitles: nil];

    [alert show];

}//获取选择的内容

(2)相互依赖的Picker

@property (strong,nonatomic)NSDictionary * books;

@property (strong,nonatomic)NSArray * authors;

@property (strong,nonatomic)NSString * selectedAuthor;

@property (strong, nonatomic) IBOutlet UIPickerView *picker;

- (void)viewDidLoad

{

    [superviewDidLoad];

    _books = [NSDictionarydictionaryWithObjectsAndKeys:[NSArrayarrayWithObjects:@"111",@"222",nil],@"li", [NSArrayarrayWithObjects:@"333",@"444",nil],@"zhang", [NSArrayarrayWithObjects:@"555",@"666", @"777",nil],@"liu",nil];

 _authors = [[_booksallKeys]sortedArrayUsingSelector:@selector(compare:)];

    _selectedAuthor = [_authorsobjectAtIndex:0];

    self.picker.dataSource =self;

    self.picker.delegate =self;

}

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 2;

}

- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if (component == 0) {

        return_authors.count;

    }

    return [[_booksobjectForKey:_selectedAuthor]count];

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    if (component == 0) {

        return [_authorsobjectAtIndex:row];

    }

    return [[_booksobjectForKey:_selectedAuthor] objectAtIndex:row];

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    if (component == 0) {

        _selectedAuthor = [_authorsobjectAtIndex:row];

        [self.pickerreloadComponent:1];

    }

    NSArray * tmp = component ==0 ? _authors : [_booksobjectForKey:_selectedAuthor];

    NSString * tip = component == 0 ? @"auther" : @"books";

    UIAlertView * alert = [[UIAlertViewalloc]initWithTitle:@"notice"message:[NSStringstringWithFormat:@"you have chose %@ : %@",tip,[tmpobjectAtIndex:row]] delegate:nilcancelButtonTitle:@"OK"otherButtonTitles: nil];

    [alert show];

}



0 0
原创粉丝点击