- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在从服务器响应中接收到一些数组后,我需要通过代码以编程方式启动 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/
我正在编写一个汽车租赁应用程序,用户必须指定取车和还车位置。 第一步,用户必须选择取货/返回办公室的区域/区域。选择区域后,他必须从该区域可用的办事处列表中进行选择。 为了选择办公室,我使用了 4 个
我正在尝试创建一个文本框,当选择该文本框时,UIPickerView 将打开并提供可供选择的选项。选择后,UIPickerView 将隐藏,所选项目将显示在文本框中。当我运行此程序时,数组中的第一个元
我有多个 UIPickerView 的问题,我在 UICollectionView 上有 8 个,当我滚动第一个 PickerView 和第五个 PickerView 时,它开始自行滚动并采用与第一个
我正在阅读一本 Swift OOP 书籍,我理解实例方法具有将在函数中使用的参数的想法。不清楚的是,在遵循 UIPickerViews 和 UITableViews 的在线教程时,有些方法将 UIPi
我有两个不同的 UIPickerView用户选择第一个 UIPickerView来自 didSelectRow第一个选择器,我得到第二个的输入 UIPickerView .我尝试为单个选择器设置标签,
函数 pickerView() 在 ViewLoad 上调用的代码 private func createPickerView(){ picker = UIPickerView(frame:
我创建了 UIPickerView 如下。现在我想让它 resignFirstResponder 如果用户在其他地方点击然后 UIPickerView 我怎么能实现呢。我已经按照以下方式创建了 UIP
我的 View Controller 中有两个 UIPickerview,pickerView1 和 pickerView2。 我在这里想要实现的是,当我从 pickerView1 中选择一个字符串时
我们正在使用 UIPickerView允许用户从选项列表中进行选择。我们正在添加 UIPickerView作为容器 UIView 的 subview 。然后我们将一个 UITapGestureReco
好吧,我不知道如何处理这个问题。 我有一个 UITextField,我根据 XML 文件的定义将其动态添加到 UITableView,在 XML 文件中,我可以将列表指定为其中一种数据类型,然后我所做
我现在有2个UIPickerView,以后想再添加3个,代码如下: -(void)viewDidLoad{ Pos_x = 5; Pos_y = 70; for (i= 0; i<2; i++) {
UIPickerView 的 selectRow:inComponent:animated:调用pickerView:didSelectRow:inComponent: ? 否则,我可以自己打电话吗?
我有两个按钮,打开一个 UIPickerView @IBOutlet weak var convertFromButton: UIButton! @IBOutlet weak var convertT
祝大家有美好的一天 我尝试在主题行中解释我的查询。据我了解,从代码中看到,所有 pickerview 都在 ViewDidLoad() 中初始化,这意味着它们都有自己的数据集。作为数据验证的一部分(在
我试图在 PickerView 中显示血型,但由于某些原因,选项仅显示为问号! 这是我的代码: class RegisterViewController: UIViewController,
我正在尝试将工具栏添加到 UIPicker . 我已经看到正确的方法是这样: UIToolbar* toolbarWeight= [[UIToolbar alloc] initWithFrame:CG
我将如何向 uipickerview 添加手势事件来更改选项卡?我必须创建一个自定义类,但是,我不知道如何处理 uipickerview。我当前在 uiviews 中存在手势来执行此操作,但我在使用
我想做的是: 显示了一个 UIPickerView。如果用户触摸选定的行,则该行将被锁定(它是多组件选择器),并且其他组件可以自由旋转。如果该行已被锁定并且用户触摸了锁定的行,则该行将被解锁并可以自由
我有两种看法。第一个:第一个 View Controller 第二:SecondViewController FirstView Controller 是我的 UINavigationControll
使用 UISegmentedControl,您可以在不选择任何内容的情况下启动它。如何使用 UIPickerView 做到这一点?看起来显示默认为索引 0。 谢谢 最佳答案 选择器轮必须停留在某些值上
我是一名优秀的程序员,十分优秀!