ios - 为什么我的 PickerView 不显示数据

标签 ios objective-c

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

我的 .h Controller 是

#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 Controller

//
//  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 应该显示数组中的名称列表,但在我的测试中没有显示任何内容。 但是按钮工作正常

最佳答案

分配你的选择器 View 委托(delegate)我猜你没有这样做。

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

关于ios - 为什么我的 PickerView 不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22867585/

相关文章:

ios - 在单个帖子请求中上传图像和一些其他参数

objective-c - 使用'now'的NSExpression函数

iphone - iOS:在 iOS 应用程序中处理下载的数据

ios - 非空值作为方法中的返回值不起作用?

iphone - 在 iOS 中显示默认铃声列表

ios - 如何创建随机闭合平滑 CGPath?

ios - 禁用表格 View 上的突出显示

ios - swift google maps 不会显示片段窗口

ios - 登录/注销用户出现推送通知问题

ios - 如何删除 objective-c 中的 UINavigationController/UINavigationBar 背景?