iOS UITextField.inputView 错误

标签 ios

我试图将自定义 inputView 添加到 tableViewCell 输入字段,但是当我这样做时,我的应用程序中出现混合 View 并且应用程序崩溃。我看过以前的解决方案,但没有找到适合我的解决方案。你能帮我解决这个问题吗?

#pragma mark - TableView data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.taskQuestionObjects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Do dequeue the custom cell here
    TaskManagerQuestionAndAnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:taskQuestionAnswerCellIdentifier];

    TaskManagerQuestion *questionObject = self.taskQuestionObjects[indexPath.row];

    cell.taskQuestionLabel.text = questionObject.title;

    cell.taskAnswerTextField.inputView = [[[NSBundle mainBundle] loadNibNamed:@"TaskManagerPickerKeyboardViewController" owner:self options:nil] objectAtIndex:0];

    return cell;
}

控制台日志:
    2014-09-12 12:26:19.615 TaskManager[24317:60b] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x167906e0 V:[UITableView:0x16bc6c00(417)]>",
    "<NSLayoutConstraint:0x167932d0 V:[_UILayoutGuide:0x16792e00]-(0)-[UITableView:0x16bc6c00]>",
    "<NSLayoutConstraint:0x16793360 V:[UITableView:0x16bc6c00]-(87)-[_UILayoutGuide:0x16792ff0]>",
    "<_UILayoutSupportConstraint:0x167917d0 V:[_UILayoutGuide:0x16792e00(64)]>",
    "<_UILayoutSupportConstraint:0x16791770 V:|-(0)-[_UILayoutGuide:0x16792e00]   (Names: '|':UIView:0x16791280 )>",
    "<_UILayoutSupportConstraint:0x16791890 V:[_UILayoutGuide:0x16792ff0(0)]>",
    "<_UILayoutSupportConstraint:0x16791830 _UILayoutGuide:0x16792ff0.bottom == UIView:0x16791280.bottom>",
    "<NSAutoresizingMaskLayoutConstraint:0x1677d2f0 h=--& v=--& V:[UIView:0x16791280(480)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x167906e0 V:[UITableView:0x16bc6c00(417)]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-09-12 12:26:38.039 TaskManager[24317:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'
*** First throw call stack:
(0x30228f83 0x3aaa3ccf 0x30228ec5 0x32a51535 0x32a514bf 0x32c19f71 0x32a57ac5 0x32c19879 0x32bd6b27 0x32af3d63 0x32af3b6d 0x32af3b05 0x32a45d59 0x326c362b 0x326bee3b 0x326beccd 0x326be6df 0x326be4ef 0x32a493e1 0x301f420b 0x301f36db 0x301f1ecf 0x3015cebf 0x3015cca3 0x350b6663 0x32aa914d 0xe85c1 0x3afb0ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

最佳答案

inputView 属性定义为 (UIView*),而不是 (UIViewController*)。在将其分配给 inputView 时,您需要使用加载的 View Controller 的 View 属性,而不是 View Controller 本身。
我认为您还应该将加载的 View Controller 存储为属性,每次重新加载单元格时从 Nib 加载它对应用程序性能不利。虽然我上次在表格 View 中尝试 loadNibNamed 是在 iOS 4.0 中,但当时它似乎没有使用任何缓存。尝试这样的事情怎么样:

// class extension for the lazy loaded property
@interface MyTableViewController()
@property (readonly) UIViewController *inputVC;
@end


@implementation MyTableViewController {
    UIViewController *_inputVC;
}

- (UIViewController *)inputVC {
    if (_inputVC == nil) {
        _inputVC = [[[NSBundle mainBundle] loadNibNamed:@"TaskManagerPickerKeyboardViewController" owner:nil options:nil] objectAtIndex:0];
    }
    return inputVC;
}

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    cell.taskAnswerTextField.inputView = self.inputVC.view;
    ...
}

@end

关于iOS UITextField.inputView 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25774472/

相关文章:

ios - 如何在 ios/swift 上添加自己的 header

ios - 如何使用自动布局在 UIPageViewController 中定位 subview ?

android - wit.ai 是否停止了 android-sdk 的开发

ios - 重定向到 native 邮件应用程序的 AppStore

c# - MVVM - 单击按钮时在哪里调用函数?

iphone - 如何在iOS中播放电影片段

ios - 2 带有动态内容的 ScrollView 内的 UIcollectionview

ios - 通过 TestFlight 安装 iOS 应用是否模拟应用升级?

ios - 如何在我的 Collection View 中显示数据获取和显示之前的事件指示器

ios - 如何使用 AVAssetWriter 向视频添加静态和动态叠加层?