web-dev-qa-db-ja.com

iOS UISearchControllerのクラッシュ:アプリケーションがモーダルビューコントローラーを表示しようとしました

Crashlyticsによると、次のクラッシュが発生しています(まれに)。

アプリケーションは、それ自体にモーダルビューコントローラーを提示しようとしました。提示コントローラーはです。

この問題をまったく再現できません。これが、UISearchコントローラーのセットアップ方法です。

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;

    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;

私はすべてアイデアがないので、どんな助けでもありがたいです。必要に応じてさらにコードを投稿します。

9
Curt Rand

IOS 11にアップデートしたときにこの問題が発生しました。私のシナリオは、テキストフィールドがあり、ユーザーがそれを編集し始めたときに、検索ビュー、基本的にはヘッダーとして検索バーを備えたテーブルビューがポップアップし、テーブルビューセルが表示されたというものでした。タップされたので閉じるはずです。

問題は、iOS 11以降、OSがfirstResponderの状態を復元しようとしていることのようです。短編小説。

私がactive = NOを追加したとき、それは私のdoedselectメソッドに役立ちました。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   self.searchController.active = NO; // Add this !
   ...

   [self dismissViewControllerAnimated:YES completion:nil];
}
15
Kastor

あなたが私のようで、別のコントローラーをモーダルに提示している間、searchControllerをactiveのままにする必要がある場合は、次の手順を実行して、モーダルなしで提示するのと同じ効果を得ます。直接行う:

クイックノート:Obj-Cに精通していないため、その答えを出すことはできませんが、ここに答えがありますSwift 4.必要に応じて、誰かがObj-Cを自由に編集して追加できますが、私は思いますSwiftであっても、目前の問題を解決する方法はここで明確です。

ポップアップしたいメニューがあるとしましょう。

let info = the info you need to pass
let currVC = self.getTopMostViewController()
let menuVC = currVC.storyboard?.instantiateViewController(withIdentifier: "myStringIdentifierSetInStoryboard") as? EventMenuViewController
guard menuVC != nil else { return }
menuVC!.info = info // Pass info necessary (i.e. what you would normally pass in prepare(for segue: ...). menuVC.info is a global variable from your class
currVC.present(menuVC!, animated: true, completion: nil)

GetTopMostViewController()の実装は異なる場合があります。鉱山は下にあり、 ここ から適応されています。

func getTopMostViewController() -> UIViewController {
    let anyVC = UIViewController()
    if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        return topController
    }
    return anyVC
}

お役に立てれば!これでは、iOS12およびSwift 4で説明されているエラーは発生しませんが、アクティブな検索コントローラーをモーダルで提示しようとすると、正確なエラーが発生しました。これが私をここに導いた理由です。

1
Josh Wolff

必ず使用してください

self.searchController = UISearchController()

の代わりに

self.searchController = UISearchController(searchResultsController: self)
0
Christo Smal