简体   繁体   English

如何使用RubyMotion将UISegmentedControl插入NavigationBar?

[英]How can I insert a UISegmentedControl to a navigationBar using RubyMotion?

I've built an iOS application using Xcode which I'm now transposing over to RubyMotion. 我已经使用Xcode构建了一个iOS应用程序,现在将其转换为RubyMotion。

Using interface builder I was able to add a segmented control to my navigation bar on one of my view controllers. 使用界面生成器,我能够在我的一个视图控制器上的导航栏中添加分段控件。 When I try to recreate this programatically in RubyMotion the app crashes without reporting what the error was. 当我尝试在RubyMotion中以编程方式重新创建此应用时,该应用崩溃了,而没有报告错误是什么。

Can anyone point out where I'm going wrong? 谁能指出我要去哪里了? Also, is init the best place to declare this? 另外,init是宣布这一点的最佳场所吗? Or one of the view lifecycle callbacks such as viewDidLoad? 还是视图生命周期回调之一,例如viewDidLoad?

class MyController < UIViewController

  def init
    if super
      image = UIImage.imageNamed('tab_bar_icons/one.png')
      self.tabBarItem = UITabBarItem.alloc.initWithTitle('One', image: image, tag:1)
      self.navigationItem.titleView = searchTypeContol # when commented out, the app doesn't crash!
    end
    self
  end

  def searchTypeControl
    @searchTypeControl ||= begin
      _searchTypeControl = UISegmentedControl.alloc.initWithFrame( CGRectZero)
      _searchTypeControl.segmentedControlStyle = UISegmentedControlStyleBar
      _searchTypeControl.insertSegmentWithTitle('One', atIndex: 0, animated: false)
      _searchTypeControl.insertSegmentWithTitle('Two', atIndex: 0, animated: false)
      _searchTypeControl.insertSegmentWithTitle('Three', atIndex: 0, animated: false)
      _searchTypeControl.sizeToFit
      _searchTypeControl
    end
  end

end    

It was a typo... :/ 这是一个错字...:/

Line #7 should be searchTypeControl not searchTypeContol 第7行应该是searchTypeControl而不是searchTypeContol

I wish RubyMotion had reported this back to me :( 我希望RubyMotion将此事回报给我:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM