【问题标题】:RubyMotion: 'Name Error'RubyMotion:“名称错误”
【发布时间】:2014-02-13 14:38:57
【问题描述】:

出现如下错误:

Terminating app due to uncaught exception 'NameError', reason: 'weather_controller.rb:3:in `viewDidLoad': uninitialized constant WeatherController::Name (NameError)

AppDelegate:

class AppDelegate

 def application(application, didFinishLaunchingWithOptions:launchOptions)

  puts "Hello!  You just launched: #{App.name}, \n location: (#{App.documents_path})"
  @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)

  @window.backgroundColor = UIColor.lightGrayColor
  @window.rootViewController = MyController.alloc.init
  @window.makeKeyAndVisible

  true
 end
end

my_controller.rb:

class MyController < UIViewController
 def viewDidLoad
  @name_label = setup_label [[10, 10], [300, 50]], UIColor.orangeColor, Name
  @place_label = setup_label [[10, 80], [300, 50]], UIColor.yellowColor, Place
  @temp_label = setup_label [[10, 150], [300, 50]], UIColor.greenColor, Temperature
end

 def setup_label frame, bgcolor, text
  label = UILabel.alloc.initWithFrame(frame)
  label.textColor = UIColor.darkGrayColor
  label.backgroundColor = bgcolor
  label.textAlignment = UITextAlignmentCenter
  label.text = text.to_s

  view.addSubview label
  label
 end
end

有什么想法吗?提前致谢

【问题讨论】:

    标签: rubymotion


    【解决方案1】:

    在您的 setup_label 方法中,您接受以下参数,framebgcolortext,其中您的 text 参数假定为 String 对象。

    因此,您的viewDidLoad 方法应该如下

    def viewDidLoad
      @name_label = setup_label [[10, 10], [300, 50]], UIColor.orangeColor, "Name"
      @place_label = setup_label [[10, 80], [300, 50]], UIColor.yellowColor, "Place"
      @temp_label = setup_label [[10, 150], [300, 50]], UIColor.greenColor, "Temperature"
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多