Rubymotion - 未定义的方法`fontWithName'对于UIFont:Class

时间:2014-05-29 09:59:48

标签: ios rubymotion

我在控制器中有以下方法,它将视图标签添加为导航标题

def viewSetupTitle

    @titleLabel = UILabel.alloc.initWithFrame([[0, 0], [300, 40]])
    @titleLabel.text = "My App"
    @titleLabel.textColor = UIColor.redColor
    @titleLabel.backgroundColor = UIColor.clearColor

    @titleLabelFont = UIFont.fontWithName("Avenir-Black")
    @titleLabel.font = @titleLabelFont

    @titleLabel.adjustsFontSizeToFitWidth = true

    self.navigationItem.titleView = @titleLabel

end

当我运行rake时,我收到以下错误:

2014-05-29 10:54:00.519 My App[14839:70b] query_controller.rb:19:in `viewSetupTitle': undefined method `fontWithName' for UIFont:Class (NoMethodError)
    from query_controller.rb:8:in `viewDidLoad'
    from app_delegate.rb:9:in `application:didFinishLaunchingWithOptions:'
2014-05-29 10:54:00.520 My App[14839:70b] query_controller.rb:19:in `viewSetupTitle': undefined method `fontWithName' for UIFont:Class (NoMethodError)
    from query_controller.rb:8:in `viewDidLoad'
    from app_delegate.rb:9:in `application:didFinishLaunchingWithOptions:'
2014-05-29 10:54:00.523 My App[14839:70b] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'query_controller.rb:19:in `viewSetupTitle': undefined method `fontWithName' for UIFont:Class (NoMethodError)
    from query_controller.rb:8:in `viewDidLoad'
    from app_delegate.rb:9:in `application:didFinishLaunchingWithOptions:'

如果我在UIFont类上调用fontWithNamefontName或任何其他方法,我会收到类似的错误。

我很困惑,因为如果我在我的代码中运行puts UIFont.instance_methods.inspect,我可以看到所有上述方法(以及根据Apple文档应该存在的其他方法)。

我哪里错了?

谢谢

1 个答案:

答案 0 :(得分:2)

您根本没有添加方法的第二部分(RubyMotion iOS / OS X方法包括看起来像关键字参数或选项的哈希)。完整的方法名称为UIFont#fontWithName:size:

您需要添加尺寸。 UIFont.fontWithName("Avenir-Black", size: 14),因为它不是可选参数,但实际上是方法名称的一部分。