gpt4 book ai didi

ios - 如何从代码手动启动 UIPickerView?

转载 作者:行者123 更新时间:2023-11-28 21:29:44 25 4
gpt4 key购买 nike

在从服务器响应中接收到一些数组后,我需要通过代码以编程方式启动 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]; 启动选择器 View - 但在这种情况下应用程序崩溃。

是的,我订阅了 UIPickerViewDataSource, UIPickerViewDelegate。我从协议(protocol)中实现了方法

- (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];
}

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

已更新

如果为 textField 调用 becomeFirstResponder - 出现以下错误(应用程序崩溃):

*** 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

最佳答案

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

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

问题是您可能还尝试在同一个后台线程上调用 [_locationTextField becomeFirstResponder];。这需要在主线程上完成。

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

dispatch_async(dispatch_get_main_queue(), ^{
[_locationTextField becomeFirstResponder];
});

关于ios - 如何从代码手动启动 UIPickerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36652885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com