iosアプリ作成をしていて、uitableviewを使用したページがあるのですが、
そちらでpresentの第一引数にalertControllerを設定して、
そのセルが押された際に、アラートが表示され「はい、いいえ」どちらかを選び初めて
画面遷移するような仕様にしていると、
タップしてからアラートが出るまでとても時間がかかったり、
そうかと思えば2回タップするとすぐに表示されたりする現象で悩まされていました。
するとリンク内容こちらのサイトに同じような現象が書かれていました。ですが、数年前の記事であること、objective-cであることなどでうまくいきませんでした。
ソースを下記に記載します。
コメントアウト部分の"ここの行"をこのように変えると良いというのを海外のサイトで目にしたので試しましたが、
アラートが出ずに、次の画面に遷移していまいそこで落ちてしまいます。
解決策を知っている方はおられないでしょうか?
DispatchQueue.main.async { self.present(alertController, animated: true, completion: nil) }
func tableView(_ table: UITableView, didSelectRowAt indexPath: IndexPath) { if status[indexPath.row] == 0 { print("アイウエオ") let alertController = UIAlertController(title: "", message: "遷移しますか?", preferredStyle: UIAlertControllerStyle.alert) let actionOK = UIAlertAction(title: "OK", style: UIAlertActionStyle.default ,handler:{ (action:UIAlertAction!) -> Void in self.appDelegate.id = self.user_id[indexPath.row] //この行追加 let storyboard:UIStoryboard = self.storyboard! let lp = storyboard.instantiateViewController(withIdentifier: "second") as! SecondViewController lp.modalTransitionStyle = UIModalTransitionStyle.partialCurl self.navigationController?.pushViewController(lp, animated: true) }) let cancelAction:UIAlertAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler:{ (action:UIAlertAction!) -> Void in print("Cancel") }) alertController.addAction(actionOK) alertController.addAction(cancelAction) self.present(alertController, animated: true, completion: nil)//ここの行 }else{ print("カキクケコ") //ここから追加部分 self.appDelegate.id = self.user_id[indexPath.row] let storyboard:UIStoryboard = self.storyboard! let lp = storyboard.instantiateViewController(withIdentifier: "second") as! SecondViewController lp.modalTransitionStyle = UIModalTransitionStyle.partialCurl self.navigationController?.pushViewController(lp, animated: true) } }
下のコード全部 DispatchQueue.main.async {の中に書くとどうなりますか?
ご返答感謝致します。試してみたところ、同じようにアラートが出る前にSecondViewに遷移してしまいました。
このコードは tableView(_:didSelectRowAt:) の中に書かれているのでしょうか?違う場合は、どこに書いているのか(どこから呼ばれているのか)を教えて下さい。
はい、その通りです。本文修正いたしました。
tableView(_:didSelectRowAt:)の中のコードはこれで全てでしょうか? status[indexPath.row] == 0 のとき、他に実行されるコードは無いですか?
appDelegateに保存させる記述など今回の件には不要だと削除していた部分が少しあったのですが、それを戻し本番と同じ状態にしました。お手数おかけいたします。
回答1件
あなたの回答
tips
プレビュー