前端开发前端组件怎么写中可以使用uipickerview组件吗

ios开发中UIPickerView使用例子-ios7教程-手机开发-壹聚教程网ios开发中UIPickerView使用例子
UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活,下面来看看它的用法。
系统的UIPickerView很简单,样式也是很简单单调,界面感觉很单调不怎么好看,有时候就需要我们来自己自定义,做出自己想要的样式。首先给出普通样式的UIPickerView示例,贴上代码:
#import &zidingyipikViewController.h&
@interface zidingyipikViewController ()&UIPickerViewDelegate,UIPickerViewDataSource&{
&&& //记录滚轮是否滑动
&&& NSString *guildS
&&& NSString *S
&&& NSMutableArray *dataMutA
&&& UIButton *bgB
@implementation zidingyipikViewController
- (void)viewDidLoad {
&&& [super viewDidLoad];
&&& dataMutArray = [NSMutableArray arrayWithArray:@[@&学生&,@&工人&,@&教师&,@&保安&,@&医生&,@&护士&,@&服务员&]];
&&& UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(80, 140, 70, 30)];
&&& [button setTitle:@&弹出框& forState:UIControlStateNormal];
&&& [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
&&& button.backgroundColor = [UIColor orangeColor];
&&& [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
&&& [self.view addSubview:button];
#pragma mark - 弹框
- (void)buttonAction{
&&& guildStr = @&0&;
&&& bgButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
&&& bgButton.backgroundColor = RGBA(0, 0, 0, 0.3);
&&& [bgButton addTarget:self action:@selector(bgButtonAction:) forControlEvents:UIControlEventTouchUpInside];
&&& [self.view addSubview:bgButton];
&&& UIView *cycanView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 180, kScreenWidth, 40)];
&&& cycanView.backgroundColor = [UIColor orangeColor];
&&& [bgButton addSubview:cycanView];
&&& UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, cycanView.height)];
&&& titleLabel.text = @&选择身份&;
&&& titleLabel.font = FONT(14);
&&& titleLabel.textColor = [UIColor whiteColor];
&&& titleLabel.textAlignment = NSTextAlignmentC
&&& [cycanView addSubview:titleLabel];
&&& UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, cycanView.height)];
&&& [cancelButton setTitle:@&取消& forState:UIControlStateNormal];
&&& [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
&&& cancelButton.titleLabel.font = FONT(14);
&&& [cancelButton addTarget:self action:@selector(bgButtonAction:) forControlEvents:UIControlEventTouchUpInside];
&&& [cycanView addSubview:cancelButton];
&&& UIButton *confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 48, 0, 48, cycanView.height)];
&&& [confirmButton setTitle:@&确定& forState:UIControlStateNormal];
&&& [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
&&& confirmButton.titleLabel.font = FONT(14);
&&& [confirmButton addTarget:self action:@selector(confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
&&& [cycanView addSubview:confirmButton];
&&& UIPickerView *selectPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 140, kScreenWidth, 140)];
&&& // 显示选中框
&&& selectPickerView.showsSelectionIndicator = NO;
&&& selectPickerView.backgroundColor = [UIColor whiteColor];
&&& selectPickerView.delegate =
&&& selectPickerView.dataSource =
&&& selectPickerView.autoresizingMask = UIViewAutoresizingFlexibleW
&&& [bgButton addSubview:selectPickerView];
#pragma mark - 隐藏弹框
- (void)bgButtonAction:(UIButton *)sender{
&&& [bgButton removeFromSuperview];
#pragma mark - 弹框确定按钮
- (void)confirmButtonAction:(UIButton *)sender{
&&& if ([guildStr isEqualToString:@&0&]) {
&&&&&&& selectStr = [NSString stringWithFormat:@&]];
&&& [self showHUDTextOnly:selectStr];
&&& [bgButton removeFromSuperview];
#pragma mark - UIPickerView代理方法
// pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
&&& return 1;
// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
&&& return dataMutArray.
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
&&& return kScreenWidth - 85 * 2;
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
&&& guildStr = @&1&;
&&& selectStr = [NSString stringWithFormat:@& objectAtIndex:row]];
&&& NSLog(@&selectStr:%@&,selectStr);
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
&&& return [dataMutArray objectAtIndex:row];
接下来,如果要改变样式,自定义自己需要的UIPickerView样式,只要重写方法就行了。方法:
//重写方法,改变
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
&&& UILabel *pickerLabel = (UILabel *)
&&& if (!pickerLabel){
&&&&&&& pickerLabel = [[UILabel alloc] init];
&&&&&&& pickerLabel.font = FONT(17);
&&&&&&& pickerLabel.textColor = [UIColor blackColor];
&&&&&&& pickerLabel.textAlignment = 1;
&&&&&&& [pickerLabel setBackgroundColor:[UIColor clearColor]];
&&& pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
&&& //在该代理方法里添加以下两行代码删掉上下的黑线
&&& [[pickerView.subviews objectAtIndex:1] setHidden:YES];
&&& [[pickerView.subviews objectAtIndex:2] setHidden:YES];
&&& UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(85, 55, kScreenWidth - 85 * 2, 1.8)];
&&& lineView1.backgroundColor = RGB(245, 245, 245);
&&& [pickerView addSubview:lineView1];
&&& UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(85, 82, kScreenWidth - 85 * 2, 1.8)];
&&& lineView2.backgroundColor = RGB(245, 245, 245);
&&& [pickerView addSubview:lineView2];
&&& return pickerL
// 当前屏幕宽度
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
// 当前屏幕高度
#define kScreenHeight [UIScreen mainScreen].bounds.size.height没有了
上一页:没有了 &&&&&下一页:相关内容UIPickerView里面的选择项内容怎么设置_xcode吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0可签7级以上的吧50个
本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:6,736贴子:
UIPickerView里面的选择项内容怎么设置
网上的那些都是配合好多好多内容的我只要一个设置内容,怎么设置?如果可以的话给我一个范例工程什么的
xcode,全球使用范围极广的语言,内容覆盖;c/手机/游戏/app/swift等,先就业后付款!达内xcode培训,名师授课,权威可靠,全日制/周末/晚班——免费试学!
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1; } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{//自定义一个view }- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{return 2;//}如果你会用tableView, 那你可以把第一个方法理解成numberOfSectionsInTableView, 第二个方法理解成ViewForHeader, 第三个方法理解成 numberOfRowInSection
贴吧热议榜
使用签名档&&
保存至快速回贴1544人阅读
iPhone开发有关(122)
一般是在&- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component&
中判断用户选择的组和行数。如果同时滚动两组或则两组以上,这个函数只会回调一次,所以如果只是简单取row和component的值,那其它组(component)的行数值(row)的更改就不会生效,所以正确的方式应该是:
& & NSInteger iRow0 = [pickerView
selectedRowInComponent:0];
NSInteger iRow1 = [pickerView selectedRowInComponent:1];
就是在本函数中取所有组的当前选中的行数,确保所有更改生效!
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:653979次
积分:7332
积分:7332
排名:第2051名
原创:115篇
转载:87篇
评论:37条
(2)(1)(1)(3)(1)(1)(1)(1)(1)(1)(3)(4)(5)(7)(3)(9)(14)(7)(8)(1)(5)(1)(6)(6)(19)(11)(25)(15)(16)(24)前两篇文章&&、&,学习了UIPickerView的单列选择器和双列选择器的使用。
现在我们一起学习相互依赖的多列选择器
&1、遵守协议
2、创建pickView
3、实现协议
//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件包含的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
return 2; // 返回2表明该控件只包含2列
//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件指定列包含多少个列表项
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
// 由于该控件只包含一列,因此无须理会列序号参数component
// 该方法返回teams.count,表明teams包含多少个元素,该控件就包含多少行
if (component == 0) {
return _areas.
return [[_teams objectForKey:_selectedAreas]count];
// UIPickerViewDelegate中定义的方法,该方法返回的NSString将作为UIPickerView
// 中指定列和列表项的标题文本
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row forComponent:(NSInteger)component
// 由于该控件只包含一列,因此无须理会列序号参数component
// 该方法根据row参数返回teams中的元素,row参数代表列表项的编号,
// 因此该方法表示第几个列表项,就使用teams中的第几个元素
if (component == 0) {
return [_areas objectAtIndex:row];
return [[_teams objectForKey:_selectedAreas]objectAtIndex:row];
// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
if (component == 0) {
_selectedAreas = [_areas objectAtIndex:row];
[self.pickView reloadComponent:1];
NSArray *tmp = component == 0 ? _areas: [_teams objectForKey:_selectedAreas];
NSString *tip = component == 0 ? @"区域":@"球队";
// 使用一个UIAlertView来显示用户选中的列表项
UIAlertView* alert = [[UIAlertView alloc]
initWithTitle:@"提示"
message:[NSString stringWithFormat:@"你选中的%@是:%@"
, tip ,[ tmp objectAtIndex:row]]
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
// UIPickerViewDelegate中定义的方法,该方法返回的NSString将作为
// UIPickerView中指定列的宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView
widthForComponent:(NSInteger)component
// 如果是第一列,宽度为90
if(component == 0) {
return 90;
return 210; // 如果是其他列(只有第二列),宽度为210
阅读(...) 评论()如何自定义 UIPickerView 的行_百度知道

我要回帖

更多关于 ios开发uipickerview 的文章

 

随机推荐