我正在尝试创建一个文本字段,使用完成按钮而不是键盘打开选择器视图。用户从选择器视图中选择一个类别,并在按下完成按钮时填充文本字段。我阅读了很多关于这个主题的帖子并遵循了这些说明。但我总是遇到同样的问题(见打击)。
我创建了pickerView并用我的数组初始化它。我知道我必须将textFieldCategory的inputView设置为pickerView。当我运行代码时,一切正常,直到我单击类别文本字段。然后应用程序崩溃,我得到以下错误输出:
015-03-23 19:09:18.663 TestApp [903:63946] ***由于未捕获的异常'UIViewControllerHierarchyInconsistency'而终止应用程序,原因:'子视图控制器:应该有父视图控制器:但是请求的父级是: UIInputWindowController:0x7f83f184d400
另外,我是否将方法调用(self.textFieldCategory.inputView = self.picker;)放置到正确的位置?我想我做到了,但我不断收到错误。我读了几篇关于这个特定错误的帖子,并按照说明解决了这个问题。但是所描述的情况与xcode的旧版本略有不同。
我甚至没有找任何人向我展示正确的代码,但如果有人能指导我走向正确的方向,那将非常感激。
以下是我目前在ViewController.m文件中的内容:
#import "AddNewObjectViewController.h"
@interface AddNewObjectViewController ()
{
NSArray *_pickerData;
}
//IBAction from textFieldName
- (IBAction)editingChanged:(UITextField *)sender;
@property (weak, nonatomic) IBOutlet UITextField *textFieldName;
@property (weak, nonatomic) IBOutlet UITextField *textFieldCategory;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@end
@implementation AddNewObjectViewController
- (IBAction)editingChanged:(UITextField *)textFieldName
{
//Activate saveButton if at least one character is entered in textFieldName
_saveButton.enabled = textFieldName.text.length > 0;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//disable saveButton
[_saveButton setEnabled:NO];
//Initialize Data
_pickerData = @[@"Category 1", @"Category 2", @"Category 3", @"Category 4"];
//Connect Data
self.picker.dataSource = self;
self.picker.delegate = self;
self.textFieldCategory.inputView = self.picker;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _pickerData.count;
}
// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _pickerData[row];
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if (sender != self.saveButton) return;
if (self.textFieldObjectName.text.length > 0) {
self.myObject = [[MyObject alloc] init];
self.myObject.myObjectName = self.textFieldName.text;
self.myObject.myObjectCategory = self.textFieldCategory.text;
}
}
@end
答案 0 :(得分:0)
好的,我只知道它崩溃的原因。我在Storyboard中创建了textfield和pickerView。为了使它工作,我不得不从superview中删除pickerView:
[self.picker removeFromSuperview];
我在viewDidLoad下添加了这个方法,现在当我单击文本字段时,应用程序不再崩溃,但会启动pickerView!