2012-03-23 6 views
2

私はいくつかの計算を行う簡単なアプリを作っています。これらの計算の一部はかなり複雑なので、結果を得るにはしばらく時間がかかることがあります。ユーザーが値を入力できるように私はUITableViewを使用しています。次に、 "計算"ボタンをクリックした後、簡単にUIViewUIActivityIndicatorViewにして、画面中央に置きます。そして、私はバックグラウンドで計算方法を呼び出します。いくつかのコード:[self.tableView reloadData]のEXC_BAD_ACCESS

self.activityIndicatorView = [[UIView alloc]initWithFrame:CGRectMake(320/2 - 50, (480 - 64)/2 - 50, 100, 100)]; 
    self.activityIndicatorView.layer.cornerRadius = 10.0f; 
    self.activityIndicatorView.backgroundColor = [UIColor colorWithRed:0. green:0. blue:0. alpha:0.7]; 
    UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    actInd.frame = CGRectMake((100 - actInd.frame.size.width)/2, (100 - actInd.frame.size.height)/2, actInd.frame.size.width, actInd.frame.size.height); 
    [actInd startAnimating]; 
    [self.activityIndicatorView addSubview:actInd]; 
    [self.activityIndicatorView bringSubviewToFront:actInd]; 
    [self.view addSubview:self.activityIndicatorView]; 
    [self.view bringSubviewToFront:self.activityIndicatorView]; 
    [self performSelectorInBackground:@selector(calculateAllThatShizzle) withObject:nil]; 

ご覧のとおり、かなり簡単です。

2012-03-23 11:06:32.418 MyApp[869:5c07] bool _WebTryThreadLock(bool), 0x6adb270: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now... 
1 WebThreadLock 
2 -[UITextRangeImpl isEmpty] 
3 -[UITextRange(UITextSelectionAdditions) _isCaret] 
4 -[UITextSelectionView setCaretBlinks:] 
5 -[UIKeyboardImpl setCaretBlinks:] 
6 -[UIKeyboardImpl setDelegate:force:] 
7 -[UIKeyboardImpl setDelegate:] 
8 -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] 
9 -[UIResponder _finishResignFirstResponder] 
10 -[UIResponder resignFirstResponder] 
11 -[UITextField resignFirstResponder] 
12 -[UITableView reloadData] 
13 -[ChildViewController calculateAllThatShizzle] 
14 -[NSThread main] 
15 __NSThread__main__ 
16 _pthread_start 
17 thread_start 

だから、問題はからキーボードで何かをしようとしていることである:キーボードがまだ表示されている場合、それはログ内のそれと-(void)calculateAllThatShizzle方法で[self.tableView reloadData]になるときしかし、それは(EXC_BAD_ACCESS)のクラッシュバックグラウンドスレッド。私は細胞をループして[cell.rightTextField resignFirstResponder]と呼ぶことを試みましたが、それは助けになりません。私もそれを試してみた

-(void)reloadTableViewData { 
    [self.tableView reloadData]; 
} 
-(void)calculateAllThatShizzle { 
    //some code omitted - code that uses UIKit 
    if ([dob timeIntervalSinceDate:calcDate]>0) { 
      [errorButton setTitle:@"Calculation Date must be more than Date of Birth" forState:UIControlStateNormal]; 
      errorButton.hidden = NO; 
      [self.activityIndicatorView removeFromSuperview]; 
      self.activityIndicatorView = nil; 
      return; 
     } 
    [self performSelectorOnMainThread:@selector(reloadTableViewData) withObject:nil waitUntilDone:NO]; 
} 

しかし、それは助けにもなりません。私は使用していますELCTextfieldCell

助けていただければ幸いです。

ありがとうございました

P.S. reloadDataのEXC_BAD_ACCESSに関するその他の質問を確認しましたが、問題があるとは限りません。

P.P.S.また、私はちょうどメインスレッドで計算を実行することができますが、その後、GUIが応答しなくなり、活動の指標には

+0

カスタムセルを使用しましたか? – hchouhan02

+0

@schそれは '[self.tableView reloadData];' – Novarg

+0

@ HChouhan02 yes、 'ELCTextfieldCell'の代わりに' - (void)calculateAllThatShizzle'メソッドにあります。ラベルとtextFieldを提供します。 [here here](http://www.cocoacontrols.com/platforms/ios/controls/elctextfieldcell) – Novarg

答えて

1

はあなたに応じて変更できます

for(int i=0;i<[tblViewData count];i++) 
{ 
    if([[(ELCTextfieldCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] rightTextField] isFirstResponder]) 
    { 
     [[(ELCTextfieldCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] rightTextField] resignFirstResponder]; 
    } 
} 

このコードを試してみてください表示されません。

+0

あなたの返信ありがとうございますが、動作しません。私は質問の最初のコード行の直前でそれを使用しました。ああ、それを考え出した。 – Novarg

+0

それは正しい答えです、私はちょうどプロジェクトをきれいにしなければなりませんでした。ありがとう! – Novarg

2

提案:あなたがcalculateAllThatShizzleを呼び出す前

  • はのtableView firstResponder 作る、すなわち辞任ので、彼らにキーボード
  • を却下して、後にcalculateAllThatShizzleへの呼び出しを延期するために、現在のファーストレスポンダを強制現在実行中のループが完了しました。つまり、キーボードを閉じるコードが適切な時間に実行されるようにします。

したがって、次のようなものがあります。

[[self tableView] becomeFirstReponder]; // will dismiss keyboard 
[[self performSelector: @selector(startBackgroundStuff) withObject: nil afterDelay: 0.0]; 

は、その後、あなたのstartBackgroundStuffに、あなたの中に置くことができます:あなたは間違いなくreloadDataメソッドがメインスレッドで呼び出される確認する方法で

[self performSelectorInBackground:@selector(calculateAllThatShizzle) withObject:nil]; 

を。あなたがやったやり方はOKです。 。

dispatch_async(dispatch_get_main_queue(), ^{ 
     [self reloadTableViewData]; 
    }); 

(私はどちらかが他のものより優れているかどうかわかりませんが、確かに確実に動作することは間違いありません。クラッシュを引き起こす可能性があるあなたには、いくつかの条件でのバックグラウンドからのUIKitコールを作っているcalculateAllThatShizzleで)

補足コメント

、すなわち[self.activityIndicatorView removeFromSuperview];。 UIKit呼び出しはメインスレッド上にある必要があります。

+0

答えに感謝しますが、 '[[self tableView] becomeFirstResponder];'はキーボードを却下しませんでした。補足コメントをいただきありがとうございます。私はそれをテストします – Novarg

+0

OK。私はそれが彼らがキーボードを却下しなければ驚くだろう、もちろん、それは実行ループが完了した後に行うことができます。そして、あなたには解決策があるように見えます。 – Obliquely

関連する問題