首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RubyMotion:“名称错误”

RubyMotion:“名称错误”
EN

Stack Overflow用户
提问于 2014-02-13 06:02:47
回答 1查看 128关注 0票数 0

有以下错误:

代码语言:javascript
复制
Terminating app due to uncaught exception 'NameError', reason: 'weather_controller.rb:3:in `viewDidLoad': uninitialized constant WeatherController::Name (NameError)

AppDelegate:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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

有什么想法吗?提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-13 06:38:57

setup_label方法中,您接受以下参数:framebgcolortext,其中text参数应该是字符串对象。

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21746428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档