首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RubyMotion和UIPickerView

RubyMotion和UIPickerView
EN

Stack Overflow用户
提问于 2014-08-19 14:23:42
回答 1查看 497关注 0票数 2

我希望使用UIPickerView选择一个数字,并将所选的数字分配给标签。我想出了如何使用ib创业板和使用接口生成器来创建初始接口,它工作得很好。然而,我想纯粹使用RubyMotion代码来完成它,而且我无法让它在我的生命中发挥作用。我管理的最好的方法是标签返回True,而不是数字。

对于选择视图委托方法,我使用了以下标准代码:

代码语言:javascript
复制
def pickerView(pickerView, numberOfRowsInComponent:component)
  101
end

def pickerView(pickerView, titleForRow:row, forComponent:component)
  row.to_s
end

def numberOfComponentsInPickerView (pickerView)
  1
end

def pickerView(pickerView, didSelectRow:row, inComponent:component)

end

def pickerView(pickerView, titleForRow:row, forComponent:component)
  " #{row+1}"
end

def submit
  totals.addTotals(myPicker.selectedRowInComponent(0))
end

然后标签文本填充如下:

代码语言:javascript
复制
numLabel = UILabel.new
numLabel.text = "Number Selected:  #{submit}"
numLabel.font = UIFont.boldSystemFontOfSize(18)
numLabel.frame = [[20,320],[260,340]]
numLabel.numberOfLines = 2
numLabel.adjustsFontSizeToFitWidth = 'YES'
self.view.addSubview numLabel

总计是一个共享客户端。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-20 03:14:47

下面是如何单独在RubyMotion中完成这一任务。注意,标签和选择器是在viewDidLoad中设置的。标签在pickerView:didSelectRow:inComponent:中更新

app_delegate.rb

代码语言:javascript
复制
class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
    @window.rootViewController = PickerDemo.new
    @window.makeKeyAndVisible
    true
  end
end

picker_demo.rb

代码语言:javascript
复制
class PickerDemo < UIViewController
  def viewDidLoad
    view.backgroundColor = UIColor.whiteColor
    @numLabel = UILabel.new
    @numLabel.text = "Number Selected:  0"
    @numLabel.font = UIFont.boldSystemFontOfSize(18)
    @numLabel.frame = [[20,100],[260,120]]
    @numLabel.numberOfLines = 2
    @numLabel.adjustsFontSizeToFitWidth = true
    view.addSubview(@numLabel)

    @picker = UIPickerView.new
    @picker.frame = [[0, 183], [320, 162]]
    @picker.delegate = self
    @picker.dataSource = self
    view.addSubview(@picker)
  end

  def pickerView(pickerView, numberOfRowsInComponent:component)
    101
  end

  def pickerView(pickerView, titleForRow:row, forComponent:component)
    row.to_s
  end

  def numberOfComponentsInPickerView(pickerView)
    1
  end

  def pickerView(pickerView, didSelectRow:row, inComponent:component)
    @numLabel.text = "Number Selected:  #{row}"
  end
end
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25385916

复制
相关文章

相似问题

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