我使用单视图应用程序的模板进行了应用程序。然后我添加了一个标签并将其连接到我的ViewController的.h文件。然后我做了一个拾取器,填充它,然后将它(和我制作的工具栏)设置到文本字段。但是当我点击文本字段时,选择器就是全黑的。如果这没有意义,代码将解释所有。
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
}
@property (weak, nonatomic) IBOutlet UITextField *habitField;
@property (weak, nonatomic) NSArray *PickerData;
@end
#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
模拟器看起来像这样
答案 0 :(得分:1)
因为我看到你忘了为选择器设置委托(UIPickerViewDelegate)
Picker.delegate = self;
请记住添加:)
@interface ViewController : UIViewController<UIPickerViewDelegate> {
}
干杯