Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从代码手动启动UIPickerView?_Ios_Objective C_Uipickerview - Fatal编程技术网

Ios 如何从代码手动启动UIPickerView?

Ios 如何从代码手动启动UIPickerView?,ios,objective-c,uipickerview,Ios,Objective C,Uipickerview,在从服务器响应接收到一些数组后,我需要从代码中以编程方式启动pickerView 我使用以下代码 _locationsPickerData = [NSArray arrayWithArray:offeredLocations]; UIPickerView *pickerView = [[UIPickerView alloc] init]; UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGe

在从服务器响应接收到一些数组后,我需要从代码中以编程方式启动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];
我试图通过
[\u locationTextField becomeFirstResponder]启动picker视图-但在这种情况下应用程序会崩溃

是的,我订阅了
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的第一响应者-出现以下错误(应用程序崩溃):

***无效断言失败_UIPerformResizeOfTextViewForTextContainer(NSLayoutManager*,UIView*,NSTextContainer*,nsInteger)(,/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIFoundation/UIFoundation-432.1/UIFoundation/TextSystem/NSLayoutManager\u Private.m:1551
2016-04-15 20:01:10.587 Weather My Way[5909:2305576]***由于未捕获的异常“NSInternalinconsistencException”而终止应用程序,原因是:“仅在主线程上运行!”
***第一次抛出调用堆栈:
(0x181d7ee38 0x1813e3f80 0x181d7ed08 0x182704190 0x186df6570 0x186df6250 0x186e28fcc 0x186e4d0a4 0x186e4c7a0 0x186ee3b18 0x186fda090 0x186f5159c 0x186F51A11C 0x186fd8b34 0x1000cdc20 0x1000bae90 0x18236b53c 0x18237e0bc 0x182738510 0x18268a900 0x18267aed8 0x18273a904 0x10018da3c 0x10019a554 0x10019C664)0x1819e1020)
libc++abi.dylib:以NSException类型的未捕获异常终止

您需要在主线程上完成UI工作

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

您需要在主线程上完成UI工作

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

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

dispatch_sync(dispatch_get_main_queue(), ^{
    /* Do UI work here */
});
很可能(正确地)您正在后台线程上从服务器下载数据。那很好

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

dispatch_sync(dispatch_get_main_queue(), ^{
    /* Do UI work here */
});
将该行(以及任何其他也需要位于主线程上的行)用以下内容包裹:

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

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

dispatch_sync(dispatch_get_main_queue(), ^{
    /* Do UI work here */
});
很可能(正确地)您正在后台线程上从服务器下载数据。那很好

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

dispatch_sync(dispatch_get_main_queue(), ^{
    /* Do UI work here */
});
将该行(以及任何其他也需要位于主线程上的行)用以下内容包裹:

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

用有关崩溃的详细信息更新您的问题。完整的错误消息是什么?哪一行代码实际导致了崩溃?请用有关崩溃的详细信息更新您的问题。完整的错误消息是什么?哪一行代码导致了崩溃?非常感谢,你说得对。我以前犯过这个错误,后来忘了)非常感谢,你说得对。我以前犯过这个错误,后来忘了。)