RubyMotion: losing superview after opening a new screen

209 Views Asked by At

I'm using a combination of ProMotion, Teacup, and MMDrawerController. When I load the app, everything works fine, but when I try to change from one screen to another, I'm getting an error with Teacup because the superview is nil. The Teacup error is: *** Terminating app due to uncaught exception 'NoMethodError', reason: 'calculations.rb:15:in calculate:': undefined method 'bounds' for nil:NilClass (NoMethodError). That's being thrown because view.superview is null.

I'm a beginner, but it feels like the Teacup error is just the symptom of a bigger problem with the way I've set everything up, or possibly, an incompatibility with MMDrawerController.

So, the app loads, goes to WeekScreen, and on WeekScreen, I add a navbar with a button to go back to WeekScreen. When I click that button, I get the error. (I've simplified the code here -- I really don't have a one page app with a button to go back to the same screen)

screens/root_screen.rb

class RootScreen < MMDrawerController
  include PM::ScreenModule

  title 'Schedule'

  def self.new(args = {})
    alloc.init.tap do |root_screen|
      root_screen.on_create(args)
    end
  end

  def centerViewController=(centerViewController)
    super
    self.title = centerViewController.title
  end

  def on_create(args={})
    super

    self.leftDrawerViewController   = Screen::MenuScreen.new(nav_bar: false)
    self.rightDrawerViewController  = nil
    self.centerViewController = week_screen

    leftDrawerButton = MMDrawerBarButtonItem.alloc.initWithTarget self, action:"show_menu:"
    navigationItem.setLeftBarButtonItem leftDrawerButton, animated:true
  end

  def will_appear
    self.title = centerViewController.title
  end

  def show_menu(sender)
    toggleDrawerSide MMDrawerSideLeft, animated:true, completion: nil
  end

  def week_screen
    @week_screen ||= Screen::WeekScreen.new
  end
end

screens/week_screen.rb

module Screen
  class WeekScreen < PM::Screen

    title ''
    stylesheet :week_styles
    include Teacup::TableViewDelegate

    @@cell_identifier = nil
    @selected_date = nil

    def will_appear
      super

      # mm_drawerController.title = title
      view.subviews.each &:removeFromSuperview

      layout(view, :main_view) do |main_view|
        week_table

        subview(UIView, :program_nav) do
          @week_btn = subview(UIButton.buttonWithType(UIButtonTypeRoundedRect), :week_btn)
        end

        @week_btn.when_tapped do
          open WeekScreen.new(nav_bar: true)
        end
      end
    end

    # a bunch of table stuff removed here to keep this clean-ish
  end
end

styles/week_styles.rb

Teacup::Stylesheet.new :week_styles do
  style :main_view,
    frame: [[0,30], ["100%", "100% - 30]]

  style :program_nav,
    frame: [[0, "100% - 78"], ["100%", 78]],
    backgroundColor: "#f1f2f2".to_color,
    autoresizingMask: (UIViewAutoresizingFlexibleLeftMargin |
                   UIViewAutoresizingFlexibleRightMargin |
                   UIViewAutoresizingFlexibleTopMargin)

  style :week_btn,
    center_x: '50%',
    center_y: '50%',
    width: 36,
    height: 36,
    backgroundImage: UIImage.imageNamed("week-view-btn")
end
1

There are 1 best solutions below

0
On

Try moving the call to on_create to inside on_load. The screen's view property is nil inside your init.tap block.