Search code examples
iphoneiosobjective-crubymotion

How to start NavigationController after button press in rubymotion


I'm trying a simple app in RubyMotion for the first time. I simply want to be able to click a button and then start a UINavigationController

Steps: - User presses a button - screen moves to left and new navigation starts

I can do this fine if I start the navigation from the first view itself but I want to be able to start it at button push.

Here is what I have so far

AppDelegate:

#below commented line starts the navigation from the first view
#navController = UINavigationController.alloc.initWithRootViewController(HomeController.alloc.init)
@window.rootViewController = HomeController.alloc.init
true

HomeController:

  def viewDidLoad
    self.title = "ONE"
    button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
    button.frame = [[15,300], [280,50]]
    button.setTitle("Move to next view", forState: UIControlStateNormal)
    button.addTarget(self, 
                     action: "startNavigationOne:",
                     forControlEvents: UIControlEventTouchUpInside)

    view.addSubview(button)
  end

  def startNavigationOne (sender)
   #what can I do here to start the navigation?

end


Solution

  • Haven't tested this code and writing from memory, but it should work.

    def startNavigationOne (sender)
      # Create your next controller and its navigation controller
      next_controller = UIViewController.alloc.initWithNibname(nil, bundle: nil)
      nav = UINavigationController.alloc.initWithRootViewController(next_controller)
    
      # Now set it as the root view controller
      UIApplication.sharedApplication.delegate.window.rootViewController = nav
    
      # The current UIViewController will be deallocated when this method exits
    end