为什么我的PickerView不显示数据

时间:2014-04-04 15:58:25

标签: ios objective-c

我的选择器View应该显示数组中的名称列表,但我的测试中没有显示任何内容。 但按钮工作正常

我的.h控制器

#import <UIKit/UIKit.h>

@interface BIDSingleComponentPickerViewController : UIViewController
    <UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) IBOutlet UIPickerView *singlePicker;
@property (nonatomic, strong) NSArray *pickerData;

- (IBAction)buttonPressed:(id)sender;

@end

我的.m控制器

//
//  BIDSingleComponentPickerViewController.m
//  TabView
//
//  Created by y on 14-3-8.
//  Copyright (c) 2014年 Ruolin. All rights reserved.
//

#import "BIDSingleComponentPickerViewController.h"

@interface BIDSingleComponentPickerViewController ()

@end

@implementation BIDSingleComponentPickerViewController

@synthesize singlePicker;
@synthesize pickerData;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *array = [[NSArray alloc] initWithObjects:@"Luke", @"Leia", @"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];
    self.pickerData = array;
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    self.singlePicker = nil;
}

- (IBAction)buttonPressed:(id)sender
{
    NSInteger row = [singlePicker selectedRowInComponent:0];
    NSString *selected = [pickerData objectAtIndex:row];
    NSString *title = [[NSString alloc] initWithFormat:@"Your selection is %@",selected];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:@"Thank you for choosing" delegate:nil cancelButtonTitle:@"Thank you" otherButtonTitles:nil];

    [alert show];
}


#pragma mark -
#pragma mark Picker Data Source Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

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

#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [pickerData objectAtIndex:row];
}


@end

我的选择器View应该显示数组中的名称列表,但我的测试中没有显示任何内容。 但按钮工作正常

1 个答案:

答案 0 :(得分:1)

分配你的选择器视图委托我想你不是这样做的。

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *array = [[NSArray alloc] initWithObjects:@"Luke", @"Leia", @"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];

    self.singlePicker.delegate = self;
    self.singlePicker.dataSource = self;

    self.pickerData = array;
    // Do any additional setup after loading the view from its nib.
}

界面构建器方式:

enter image description here