以下为控制器代码,主要用到的是UIPickerView

主要步骤:新建一个Single View Application

然后,如上图所示,拖进去一个UILabel Title设置为导航,再拖进去一个UILabel,用于显示效果,最后拖进去一个UIPickerView,设置好代理和dataSource,这应该都会。往后就是在代码中实现效果。代码注释很详细,看看都会懂,然后结合API,就可以举一反三了。自己也是慢慢学习的,然后自己练得。嘿嘿多多学习。加油!!!

#import <UIKit/UIKit.h>

@interface sdsViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
//这是显示效果的label
@property (retain, nonatomic) IBOutlet UILabel *textLabel;

//字体大小数组 
@property(nonatomic,retain) NSArray *fontArray;

//字体颜色数组 
@property(nonatomic,retain) NSArray *colorArray;

//字体大小数组 
@property (nonatomic, retain)NSArray *sizeArray;
@end

//
//  sdsViewController.m
//  UIPickerView
//
//  Created by Ibokan on 12-8-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "sdsViewController.h"

@implementation sdsViewController 
@synthesize textLabel; 
@synthesize fontArray;
@synthesize colorArray;
@synthesize sizeArray;

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad 
{
    [super viewDidLoad];
    //设置所有字体
    self.fontArray=[UIFont familyNames];
    
    //设置字体大小数组
    self.sizeArray=[NSArray arrayWithObjects:@"12",@"16",@"20",@"24",@"30",@"35",@"40",nil];
    
    //设置颜色数组
    self.colorArray=[NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor clearColor],[UIColor yellowColor],[UIColor brownColor], [UIColor blackColor],[UIColor purpleColor],[UIColor orangeColor],[UIColor darkGrayColor],[UIColor magentaColor],[UIColor darkTextColor],[UIColor brownColor],[UIColor lightGrayColor],nil];

// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload 
{
    //重新指向
    [self setTextLabel:nil];
    self.colorArray=nil;
    self.fontArray=nil;
    self.sizeArray=nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated 
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated 
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc { 
    
    //销毁对象
    [textLabel release];
    [fontArray release];
    [colorArray release];
    [sizeArray release];
    [super dealloc];
}

#pragma mark - 
#pragma mark UIPickerViewDelegate  UIPickerViewDataSource

//以下是适配器部分,即数据源

//返回有几列 
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    
    //返回有几列 ,注意
    return 3;
    
}

//返回指定列的行数 
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
     NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    
    if (component==0) {
        return  [self.fontArray count];
    } else if(component==1){
        
        return  [self.colorArray count];
    }
    else if(component==2){
        return [self.sizeArray count];
        
    }
    
    return 0;
}

//以下是代理部分,可以自定义视图

//返回指定列,行的高度,就是自定义行的高度 
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
   NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    
    
    return  40;
}

//返回指定列的宽度 
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
     NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    if (component==0) {
        //第0列,宽为180
        return  180;
    } else if(component==1){
        //第1列,宽为80
        return  80;
    }
    else{
        //第三列宽为60
        return 60;
    }
    
}

// 自定义指定列的每行的视图,即指定列的每行的视图行为一致 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
     NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    
    
    //思想就是:先创建一个View以指定列的宽度,和所在列行的高度,为尺寸
    //再建立一个label,在这个view上显示字体,字体颜色,字体大小,然后,把这个label添加到view中
    //返回view,作为指定列的每行的视图
    
    
    
    //取得指定列的宽度
    CGFloat width=[self pickerView:pickerView widthForComponent:component];
    
    //取得指定列,行的高度
    CGFloat height=[self pickerView:pickerView rowHeightForComponent:component];
    
    //定义一个视图
    UIView *myView=[[UIView alloc] init];
    
    //指定视图frame
    myView.frame=CGRectMake(0, 0, width, height);
    
    UILabel *labelOnComponent=[[UILabel alloc] init];
    
    labelOnComponent.frame=myView.frame;
    labelOnComponent.tag=200;
    
    
    if (component==0) {
        //如果是第0列
        
        //以行为索引,取得字体
        UIFont *font=[self.fontArray objectAtIndex:row];
        //在label上显示改字体
        labelOnComponent.text=[NSString stringWithFormat:@"%@",font];
        
    }
    else if(component==1){
        //如果是第1列
        //以说选择行为索引,取得颜色数组中的颜色,并把label的背景色设为该颜色
        labelOnComponent.backgroundColor=[self.colorArray objectAtIndex:row];
       
    }
    else if(component==2){
        //如果是第2列
        //label 上显示的是相应字体
        
        labelOnComponent.text=[self.sizeArray objectAtIndex:row];
      
      }
    
    [myView addSubview:labelOnComponent];
    
    //内存管理,建立后释放 
    [labelOnComponent release];
    [myView autorelease];
    
    return myView;
    
}

- (void)pickerView:(UIPickerView *)pickerView 
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component
{
    //取得选择的是第0列的哪一行
    int rowOfFontComponent = [pickerView selectedRowInComponent:0];
    //取得选择的是第1列的哪一行
    int rowOfColorComponent = [pickerView selectedRowInComponent:1];
    //取得选择的是第2列的哪一行
    int rowOfSizeComponent = [pickerView selectedRowInComponent:2];
    
    //取得所选列所选行的视图
    UIView *ViewOfFontComponent = (UILabel *)[pickerView viewForRow:rowOfFontComponent forComponent:0];
    UIView *ViewOfColorComponent =(UILabel *) [pickerView viewForRow:rowOfColorComponent forComponent:1];
    UIView *ViewOfSizeComponent = (UILabel *)[pickerView viewForRow:rowOfSizeComponent forComponent:2];

//取得取得所选行所选列上的视图的子视图
    UILabel *viewOnViewofFontComponent=(UILabel *)[ViewOfFontComponent viewWithTag:200];
    UILabel *viewOnViewOfColorComponent=(UILabel *)[ViewOfColorComponent viewWithTag:200];
    UILabel *viewOnViewOfSizeComponent=(UILabel *)[ViewOfSizeComponent viewWithTag:200];
    
    
    //最后将所选择的结果展现在label上,即字体样式,字体颜色,字体大小
    
    self.textLabel.font=[UIFont fontWithName:viewOnViewofFontComponent.text size:[viewOnViewOfSizeComponent.text floatValue]];
    
    self.textLabel.textColor=viewOnViewOfColorComponent.backgroundColor;
    
       
  
    
    
}

@end

效果图:

ios之UIPickView的更多相关文章

  1. IOS UIPickView+sqlite 选择中国全部城市案例

    1.案例简单介绍 通过读取文件.将中国全部城市写入sqlite数据库中,现通过UIPickView实现中国全部城市的选择,效果图例如以下所看到的 2.城市对象模型 中国全部城市数据请看http://b ...

  2. iOS基础问答面试

    <简书社区 — Timhbw>iOS基础问答面试题连载(一)-附答案:http://www.jianshu.com/p/1ebf7333808d <简书社区 — Timhbw> ...

  3. iOS pickerView(所有类型一网打尽)

    概述 关于PickView的所有类型都在这里 详细 代码下载:http://www.demodashi.com/demo/11017.html 首先看下项目的整体结构: 一.准备工作 UIPicker ...

  4. iOS 中这些是否熟练掌握——(2)

    接上一篇博文,本篇博文是作者原创,用于记录从网上查阅的一些资料,并对自己的知识体系进行一下总结,成文以供学习使用. 1.Cocoa Touch 包含了什么?不包含什么?与 Cocoa 有什么区别? 相 ...

  5. iOS可视化动态绘制连通图

    上篇博客<iOS可视化动态绘制八种排序过程>可视化了一下一些排序的过程,本篇博客就来聊聊图的东西.在之前的博客中详细的讲过图的相关内容,比如<图的物理存储结构与深搜.广搜>.当 ...

  6. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  7. 【疯狂造轮子-iOS】JSON转Model系列之一

    [疯狂造轮子-iOS]JSON转Model系列之一 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...

  8. iOS总结_UI层自我复习总结

    UI层复习笔记 在main文件中,UIApplicationMain函数一共做了三件事 根据第三个参数创建了一个应用程序对象 默认写nil,即创建的是UIApplication类型的对象,此对象看成是 ...

  9. iOS代码规范(OC和Swift)

    下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...

随机推荐

  1. Unity3D研究院之游戏对象的访问绘制线与绘制面详解(十七)

    一眨眼学习Unity3D 也有一段时间了,基本已经拿下了这套游戏引擎,回过头来想想以前写的RPG 游戏引擎,越来越发现以前写的就是垃圾.人果然是要不断学习与不断进步,好好学习,天天向上.哇咔咔- 加油 ...

  2. PJzhang:国内常用威胁情报搜索引擎说明

    猫宁!!! 参考链接: https://www.freebuf.com/column/136763.html https://www.freebuf.com/sectool/163946.html 如 ...

  3. bzoj1145[CTSC2008]图腾

    传送门 虽然是远古时期的ctsc,但是果然还是ctsc啊 前置芝士:树状数组 这个题最开始的思路很好想,由于之前写过一个类似处理的题,所以这个题我一开始就想到了思路. 首先,我们可以尝试讲图腾表示为x ...

  4. C - Catch That Cow POJ - 3278

    //标准bfs #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...

  5. 洛谷 P4362 [NOI2002]贪吃的九头龙

    https://www.luogu.org/problemnew/show/P4362 首先有个很显然的dp:ans[i][j][k]表示i节点用j号头,i节点为根的子树中共有k个点用大头时i节点为根 ...

  6. jQuery插件pagination.js源码解读

    pagination的github地址:https://github.com/gbirke/jquery_pagination 公司用的是1.2的版本,所以我就读1.2的了. jQuery.fn.pa ...

  7. JavaScript Allongé 序幕:值和表达式

    序幕:值和表达式   下面的内容非常基础,然而,就像大多数的故事一样,在最初的时候开始才是最好的开端. 想象一下,我们要去最喜欢的咖啡馆,他们会为你制作几乎任何你想要的饮料,从小而强烈的 Espres ...

  8. csu 1551: Longest Increasing Subsequence Again BIT + 思维

    预处理last[i]表示以第i个开始,的合法后缀. pre[i]表示以第i个结尾,的合法前缀. 那么每一个数a[i],肯定是一个合法后缀last[i] + 一个合法前缀,那么合法前缀的数字要小于a[i ...

  9. hihocoder #1190 : 连通性·四 点双联通分量

    http://hihocoder.com/problemset/problem/1190?sid=1051696 先抄袭一下 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描 ...

  10. H5存储

    1.localstorage ① 500万字符限制② 一般存储ajax请求返回数据,并且需要设置过期时间③ 具有清理机制,将过期数据清理④ 不存储敏感信息⑤ 不存储SEO依赖数据,至少不能严重依赖⑥ ...