1

我使用单视图应用程序的模板制作了一个应用程序。然后我添加了一个标签并将其连接到我的 ViewController 的 .h 文件。然后我做了一个选择器,填充它,然后将它(和我制作的工具栏)设置到文本字段。但是当我点击文本字段时,选择器是全黑的。如果这没有意义,代码将解释一切。

。H

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
}

@property (weak, nonatomic) IBOutlet UITextField *habitField;

@property (weak, nonatomic) NSArray *PickerData;

@end

.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

    NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
    self.PickerData = array;

    UIToolbar *toolBar = [[UIToolbar alloc] init];
    toolBar.barStyle = UIBarStyleBlackOpaque;
    [toolBar sizeToFit];

    [toolBar setBackgroundImage:[UIImage imageNamed:@"red_navigation_bar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                           target:self
                                                                           action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                            target:self
                                                                            action:@selector(releasePicker)];

    UIPickerView *Picker = [[UIPickerView alloc] init];

    doneButton.image = [UIImage imageNamed:@"button.png"];

    [toolBar setItems:@[flexSpace, doneButton] animated:YES];
    self.habitField.inputAccessoryView = toolBar;

    [self.habitField setInputView:Picker];

}

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

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

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

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [self.PickerData objectAtIndex:row];
}

@end

模拟器长这样

在此处输入图像描述

4

1 回答 1

1

如我所见,您忘记为选择器设置委托( UIPickerViewDelegate )

        Picker.delegate = self;

记得添加:)

@interface ViewController : UIViewController<UIPickerViewDelegate> {

}

干杯

于 2013-08-14T23:48:09.337 回答