使用UIPicker视图



我在一个选项卡式应用程序中对Xcode 4.2做了这段代码,它运行得很好,但我希望它是一个选择器视图的下拉菜单,但我不确定我需要添加什么代码

.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
    IBOutlet UIPickerView *pickerView;
    NSMutableArray *list;
    IBOutlet UILabel *picklabel;
}
@end

.m
#import "FirstViewController.h"
@implementation FirstViewController
-(NSInteger)numberOfComponentsInPickerview:(UIPickerView *)thePickerView {
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [list count];
}

-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [list objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *string = [NSString stringWithFormat:@"you selected: %@" , [list objectAtIndex:row]];
    picklabel.text = string;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    list = [[NSMutableArray alloc] init];
    [list addObject:@"Giraffe"];
    [list addObject:@"Water Bottle"];
    [list addObject:@"spinning Fan"];
    [list addObject:@"Hariy Monkey"];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

按照以下步骤操作:

  • 在选择每个pickerview时,将该数据添加到一个可变数组中
  • 每次使用该数据(可变数组)重新加载表视图时
  • 选定的数据将添加到您的表格列表中

享受编码:)

最新更新