我想使用UIPickerView选择一个数字并将选定的数字分配给标签。我研究了如何使用ib gem和使用界面构建器来创建初始界面并且它工作正常。但是,我想纯粹使用RubyMotion代码来完成它,我不能让我的生活得到它的工作。我管理的最好的是标签返回True而不是数字。
我使用以下标准代码进行选择器视图委托方法:
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
然后标签文本填充如下:
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
总计是共享客户。
答案 0 :(得分:3)
以下是单独使用RubyMotion的方法。请注意,标签和选择器在viewDidLoad
中设置。标签在pickerView:didSelectRow:inComponent:
<强> app_delegate.rb 强>
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 强>
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