码迷,mamicode.com
首页 > 其他好文 > 详细

UIPickerView

时间:2015-01-22 23:21:19      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/lvchenhao/p/4242870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!