//
// 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