IOS UIPickerView 学习笔记


//
//  ViewController.m
//  UIScrollView
//
//  Created by hundsun on 14-12-27.
//  Copyright (c) 2014年 Robinson. All rights reserved.
//

#import "ViewController.h"

#define MAIN_SCREEN_WIDTH  [[UIScreen mainScreen]bounds].size.width;
#define MAIN_SCREEN_HIGTH  [[UIScreen mainScreen]bounds].size.width;

//IPhone5适配项
#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double )568) < DBL_EPSILON )
//IPhone4适配项
#define IS_IPHONE_4 (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double )480) < DBL_EPSILON )

@interface ViewController ()

@end

@implementation ViewController

- (void)dealloc{

    [_pickView release];
    _pickView.delegate = nil;
    
    [super dealloc];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
 <p class="p1"><span class="s1">   </span>//<span class="s2">实例方法</span></p><p class="p1"><span class="s1"> </span>//   books =[[NSArray alloc]initWithObjects:@"123",@"456",@"789", nil];</p><p class="p2">    </p><p class="p3"><span class="s3">    </span><span class="s4">//</span>奔溃?<span class="s4">  </span>类方法<span class="s4">/</span>变量会自动释放,倘若要第二次使用的话,<span class="s4"> </span>所以一般要<span class="s4">retain</span>下。然后在<span class="s4">dealloc</span>中释放下</p><p class="p4"><span class="s1">    </span><span class="s5">books</span><span class="s1"> = [[</span>NSArray<span class="s1"> </span>arrayWithObjects<span class="s1">:</span><span class="s6">@"111"</span><span class="s1">,</span><span class="s6">@"22"</span><span class="s1">,</span><span class="s6">@"333"</span><span class="s1">, </span><span class="s7">nil</span><span class="s1">]</span>retain<span class="s1">];</span></p>
    
    _pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 300, 300, 300)];
    _pickView.delegate = self;
    _pickView.dataSource = self;
    
    [self.view addSubview:_pickView];
    
    UILabel *Lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 150, 320/2, 30)];
    Lable.font = [UIFont systemFontOfSize:16.0f];
    Lable.text = @"PickerView Demo";
    Lable.textColor = [UIColor redColor];
    [self.view addSubview:Lable];
    [Lable release];
    
   
    
}

#pragma mark -- 
#pragma mark UIPickerViewDataSource @required

// returns the number of 'columns' to display.返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 1;
}

// returns the # of rows in each component..返回多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return  [books count];
}


#pragma mark --
#pragma mark UIPickerViewDelegate  @optional

// returns width of column and height of row for each component.
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{

    return MAIN_SCREEN_WIDTH;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{


    return  30;
}

// these methods return either a plain NSString, a NSAttributedString, or a view (e.g UILabel) to display the row for the component.
// for the view versions, we cache any hidden and thus unused views and pass them back for reuse.
// If you return back a different object, the old one will be released. the view will be centered in the row rect
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSLog(@"1------%@-----",[books objectAtIndex:row]);
    return [books objectAtIndex:row];
}

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

    
    if (row < [books count]) {
        
        UIAlertView *alertView = [[[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"您选中的是:%@",[books objectAtIndex:row]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil]autorelease];
        
        [alertView show];
    }
    NSLog(@"2------%@-----",[books objectAtIndex:row]);
}

@end







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值