如何手动从代码启动UIPickerView?

时间:2016-04-15 16:58:14

标签: ios objective-c uipickerview

我需要在从服务器响应

接收一些数组后以编程方式从编码中启动pickerView

我使用以下代码

_locationsPickerData = [NSArray arrayWithArray:offeredLocations];
UIPickerView *pickerView = [[UIPickerView alloc] init];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(locationsPickerDoneAction:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:@[flexibleSpace, doneButton]];
toolbar.translucent = YES;

pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.accessibilityIdentifier = @"locationsPicker";

[_locationTextField setInputView:pickerView];
[_locationTextField setInputAccessoryView:toolbar];

我尝试按[_locationTextField becomeFirstResponder];启动选择器视图 - 但在这种情况下应用程序崩溃。

是的,我订阅了UIPickerViewDataSource, UIPickerViewDelegate。 我从协议

实现了方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    // the number of columns of data
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    // the number of rows of data
    return _locationsPickerData.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    // the data to return for the row and component (column) that's being passed in
    return _locationsPickerData[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _locationTextField.text = _locationsPickerData[row];
}

如何从代码手动启动选择器视图?

已更新

如果为textField调用yesFirstResponder - 出现以下错误(app crashes):

*** Assertion failure in void _UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *, UIView<NSTextContainerView> *, NSTextContainer *, NSUInteger)(), /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIFoundation/UIFoundation-432.1/UIFoundation/TextSystem/NSLayoutManager_Private.m:1551
2016-04-15 20:01:10.587 Weather-My-Way[5909:2305576] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'
*** First throw call stack:
(0x181d7ee38 0x1813e3f80 0x181d7ed08 0x182704190 0x186df6570 0x186df6250 0x186e28fcc 0x186e4d0a4 0x186e4c7a0 0x186ee44a8 0x186ee3b18 0x186fda090 0x186f5159c 0x186f51a1c 0x186fd8b34 0x1000cdc20 0x1000bae90 0x18236b53c 0x18237e0bc 0x182738510 0x18268a900 0x18267aed8 0x18273a904 0x10018da3c 0x10019a554 0x10019172c 0x10019c66c 0x10019c364 0x1819e1470 0x1819e1020)
libc++abi.dylib: terminating with uncaught exception of type NSException

2 个答案:

答案 0 :(得分:2)

错误告诉您问题所在。您正尝试在主线程以外的线程上执行UI任务。

您很可能(并且正确地)在后台线程上从服务器下载数据。这很好。

问题是您可能还尝试在同一个后台线程上调用 end id.thomas years_exp 2 2007 136 14 3 2008 136 15 4 2009 136 16 5 2010 136 17 6 2011 136 18 7 2012 136 19 8 2013 136 20 9 2014 136 21 10 2013 172 14 11 2014 172 15 。这需要在主线程上完成。

用这个包裹该行(以及任何其他需要在主线程上的行):

[_locationTextField becomeFirstResponder];

答案 1 :(得分:0)

您需要在主线程上进行UI工作。

dispatch_sync(dispatch_get_main_queue(), ^{
    /* Do UI work here */
});