2016-04-15 6 views
0

私は[_locationTextField becomeFirstResponder];によってピッカービューを起動しようとしたサーバ応答UIPickerViewをコードから手動で起動するには?

からいくつかの配列を受信した後、私は、次のコード

_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]; 

を使用するコードからプログラム的pickerViewを起動する必要がある - しかし、このような場合、アプリケーションがクラッシュしました。

はい、私は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]; 
} 

プロトコルからメソッドを実装しましたか?

UPDATED

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 
+0

更新。完全なエラーメッセージは何ですか?実際にクラッシュするのはどのコードですか? – rmaddy

+0

更新された質問 –

答えて

2

エラーはあなたの問題を伝えています。メインスレッド以外のスレッドでUIタスクを実行しようとしています。

ほとんどの場合、バックグラウンドスレッドでサーバーからデータをダウンロードしている可能性があります。それは良い。

問題は同じバックグラウンドスレッドで[_locationTextField becomeFirstResponder];を呼び出す可能性があります。それはメインスレッドで行う必要があります。

ラップその行(ともメインスレッド上にある必要は他のもの)これで:クラッシュに関する詳細は、あなたの質問

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

ありがとうございました。前にこのミスをして、それを忘れてしまった) –

0

あなたは、メインスレッド上で、あなたのUIの作業を行う必要があります。

dispatch_sync(dispatch_get_main_queue(), ^{ 
    /* Do UI work here */ 
}); 
関連する問題