0

3 つのタブを持つアプリがあります。右または左にスワイプして別のタブに移動したい。

私のコード:

//Swipe Between Tabs
    let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
    let leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
    rightSwipe.direction = .Right
    leftSwipe.direction = .Left
    view.addGestureRecognizer(rightSwipe)
    view.addGestureRecognizer(leftSwipe)
    //end Swipe

そしてそれを実行する機能は

func handleSwipes(sender:UISwipeGestureRecognizer) {
    if (sender.direction == .Left) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("PantryList")
        let navigationController = UINavigationController(rootViewController: vc)

        self.presentViewController(navigationController, animated: true, completion: nil)
    }
    if (sender.direction == .Right) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("ToDoList")
        let navigationController = UINavigationController(rootViewController: vc)

        self.presentViewController(navigationController, animated: true, completion: nil)
    }
}

私の問題は、スワイプを使用すると下部の tabBarController が消えることです。私が見つけたものから、それは「presentViewController」メソッドと関係があります。これが原因で、tabBarController を失わずにそれを行う方法はありますか? 必要がない場合は、本当に prepareForSegueWithIdentifier を使用したくありません。そうしなければならない場合を除き、それは必要以上の作業のように思えます。

4

3 に答える 3

1

もちろん、現在のView Controllerの上にView Controllerを提示しているためです。を切り替えるには、メソッドUITabbarController viewControllersを使用できsetSelectedIndex:ます。この場合、最初の vc は 0 インデックス、2 番目と 3 番目はそれぞれ 1 と 2 になります。選択したインデックスをスワイプで切り替えるだけで完了です。幸運を!

于 2016-01-28T13:02:24.207 に答える
1
if (sender.direction == .Right) {
    self.navigationController.tabBarController.selectedIndex = 1
}
于 2016-01-28T13:03:44.600 に答える
0

私もあなたの問題はpresentViewController:にあると思います。スワイプ処理コードが UITabBarController にあると仮定すると、ここで行っていることは、タブコントローラー全体の上に VC をプッシュすることです。

選択したビュー コントローラーのビューを、表示される次の VC ビューに沿って (スワイプ移動によって指定された方向に) 移動してから、UITabBarController selectedViewController の値を新しい VC に変更します。

于 2016-01-28T12:58:51.690 に答える