0

我有这个代码,我在 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;
}
4

1 回答 1

0

请按照以下步骤操作:

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

享受编码:)

于 2012-02-19T13:19:12.890 回答