iPhone UIPickerView没有加载值

时间:2014-04-03 11:56:11

标签: ios iphone uipickerview

我尝试构建一个基本的应用程序原型(模仿真实的应用程序流程)。我创建了一个视图控制器类,并将UIPickerView链接到.h文件。写了逻辑,但仍然没有反映在选择器中。

我错过了什么吗?在这里你可以使用代码:

@implementation SelectCountryForViewAdsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1; // For one column
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.colorArray count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.colorArray objectAtIndex:row]; // If it's a string
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.colorArray  = [[NSArray alloc]         initWithObjects:@"Blue",@"Green",@"Orange",@"Purple",@"Red",@"Yellow" , nil];

}

并在.h文件中:

@interface SelectCountryForViewAdsViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>

@property (strong, nonatomic) IBOutlet UIPickerView *countrySelector;
@property (strong, nonatomic) NSArray *colorArray;

@end

感谢任何帮助。我是iOS开发人员的初学者。

4 个答案:

答案 0 :(得分:3)

请检查下图,您需要右键单击UIPicker视图并将其连接到文件所有者。

enter image description here

请查看以下链接了解更多详情

http://www.techotopia.com/index.php/An_iOS_5_iPhone_UIPickerView_Example

http://iosmadesimple.blogspot.in/2012/09/uipickerview-tutorial.html(更有帮助)

http://www.developersalmanac.com/uipickerview-tutorial/

答案 1 :(得分:1)

[countrySelector setDelegate:self];
 [countrySelector setDataSource:self];

//在Viewdidload方法中使用此代码

答案 2 :(得分:1)

在您的界面文件中添加UIPickerViewDataSourceUIPickerViewDelegate

viewDidLoad

self.countrySelector.delegate = self;
self.countrySelector.dataSource = self;

您可以直接从delegate文件设置dataSourcexib。但不要忘记在界面文件中提及它们。

答案 3 :(得分:0)

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f)];
    [lblRow setTextAlignment:NSTextAlignmentCenter];
    [lblRow setTextColor: [UIColor strawberryColor]];
    [lblRow setText:self.colorArray[row]];
    [lblRow setFont:[UIFont fontWithName:@"Avenir-Medium" size:18]];
    [lblRow setBackgroundColor:[UIColor clearColor]];

    return lblRow;
}