首页 > 代码库 > UIPickerView
UIPickerView
UIPickerView和UIDatePicker是类似的控件,只不过UIDatePicker是日期控件,只能放日期,而UIPickerView可以放任何东西。
UIPickerView高度固定,设置无效。
使用前时要添加代理
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
{
UIPickerView *picker;
}
@property (strong, nonatomic) UIWindow *window;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; picker =[[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, 200, 0) ]; picker.delegate=self; [self.window addSubview:picker]; return YES;}//设置UIPickerView有几个区域- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2;}//设置UIPickerView每个区域的行数- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if(component==0) { return 2; } else { return 3; }}//设置UIPickerView现实的标题- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if(component==0) { NSArray *array=@[@"1",@"2"]; return array[row]; } else { NSArray *array=@[@"1",@"2",@"3"]; return array[row]; }}//为UIPickerView设置视图,所以UIPickerView不当可以显示文字 还可以显示图片- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UIImageView *view1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"apple.jpg"]]; view1.frame=CGRectMake(0, 0, 80, 40); return view1;}//为UIPickerView设置多样化字符串- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0){ return NSAttributedString对象;}//获得UIPickerView的选择的区和行,进而获取及其内容- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ NSLog(@"----%d--%d",row,component);}
UIPickerView
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。