2015-05-26 2 views
0

У меня есть 2 вкладки экраны,Как переключаться между закладках экранами swipeing

Я хочу красть право на второй вкладке экрана

и проведите пальцем влево на первой вкладке экрана

Но я не» t знать, как вызвать открытый экран второй вкладки в handle_swipe метод

Любая идея? Спасибо

AppDelegate

ruby class AppDelegate def application(application, didFinishLaunchingWithOptions:launchOptions) @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) @window.makeKeyAndVisible first_tab = FirstTabController.alloc.init second_tab = SecondTabController.alloc.init @tabbar = UITabBarController.alloc.init @tabbar.viewControllers = [first_tab, second_tab] @tabbar.wantsFullScreenLayout = true @window.addSubview @tabbar.view

FirstTabController

`` `рубин

class FirstTabController < UIViewController 
    def init 
    if super 
     self.title = "First Tab" 
     self.tabBarItem.image = UIImage.imageNamed('FirstTab.png') 
    end 
    self 
    end 

    def viewDidLoad 
    view.backgroundColor = UIColor.whiteColor 
    @label = UILabel.new 
    @label.text = 'First Tab' 
    @label.frame = [[50,50],[250,50]] 
    view.addSubview(@label) 

    # right 
    recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:') 
    self.view.addGestureRecognizer(recognizer) 
    # left 
    recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:') 
    recognizer.direction = UISwipeGestureRecognizerDirectionLeft 
    self.view.addGestureRecognizer(recognizer)  
    end 

    def handle_swipe(sender) 

    if(sender.direction == UISwipeGestureRecognizerDirectionRight) 
     p "Swiped right" 
     # @secondary_controller = SecondaryController.alloc.init 
     @secondary_controller = SecondTabController.alloc.init 
     # self.navigationController.pushViewController(@secondary_controller, animated: true) 
    end 

    end 

end 

` ``

ответ

1

Вы можете попробовать добавить жест распознаватель к мнению UITabBarController , и в этом случае вы необходимо подклассифицировать его и переместить метод handle_swipe на этот контроллер.

Если вы попробуете этот маршрут, я также переведу код, который создает и добавляет распознаватели жестов в метод viewDidLoad подкласса UITabBarController.

+0

привет, я уже есть метод __handle_swipe__, но не знаете, как открыть другой __UIViewController__ here.what это синтаксис? спасибо – newBike

+0

Большое спасибо, я нашел обходное решение, чтобы получить ti. – newBike

0

Наконец-то я закончил его, установив UITabBarController в качестве глобальной переменной, а затем изменил свой индекс на событие салфетки.

Есть ли более эффективная практика для достижения этой цели?

Я думал, что использование глобальной переменной может быть не очень хорошей идеей.

Но я не знаю альтернативного пути.

`` `дифф

--- a/ch_2/12_tabbars/app/first_tab_controller.rb 
+++ b/ch_2/12_tabbars/app/first_tab_controller.rb 
@@ -13,5 +13,22 @@ class FirstTabController < UIViewController 
    @label.text = 'First Tab' 
    @label.frame = [[50,50],[250,50]] 
    view.addSubview(@label) 
+ 
+ # right 
+ recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:') 
+ self.view.addGestureRecognizer(recognizer) 
+ # left 
+ recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:') 
+ recognizer.direction = UISwipeGestureRecognizerDirectionLeft 
+ self.view.addGestureRecognizer(recognizer) 
+ end 
+ 
+ 
+ def handle_swipe(sender) 
+ 
+ if(sender.direction == UISwipeGestureRecognizerDirectionRight) 
+  p "Swiped right" 
+  $tabbar.selectedIndex =1 
+ end 
    end 
end 
diff --git a/ch_2/12_tabbars/app/second_tab_controller.rb b/ch_2/12_tabbars/app/second_tab_controller.rb 
index 18ce656..8e718b0 100644 
--- a/ch_2/12_tabbars/app/second_tab_controller.rb 
+++ b/ch_2/12_tabbars/app/second_tab_controller.rb 
@@ -13,5 +13,24 @@ class SecondTabController < UIViewController 
    @label.text = 'Second Tab Controller' 
    @label.frame = [[50,50],[250,50]] 
    view.addSubview(@label) 
+ 
+ # right 
+ recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:') 
+ self.view.addGestureRecognizer(recognizer) 
+ # left 
+ recognizer = UISwipeGestureRecognizer.alloc.initWithTarget(self, action:'handle_swipe:') 
+ recognizer.direction = UISwipeGestureRecognizerDirectionLeft 
+ self.view.addGestureRecognizer(recognizer) 
+ 
+ 
    end 
+ 
+ 
+ def handle_swipe(sender) 
+ 
+ if(sender.direction == UISwipeGestureRecognizerDirectionLeft) 
+  p "Swiped left" 
+  $tabbar.selectedIndex = 0 
+ end 
+ end 
end 

` ``

Смежные вопросы