我希望使用UIPickerView选择一个数字,并将所选的数字分配给标签。我想出了如何使用ib创业板和使用接口生成器来创建初始接口,它工作得很好。然而,我想纯粹使用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总计是一个共享客户端。
发布于 2014-08-20 03:14:47
下面是如何单独在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
endpicker_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
endhttps://stackoverflow.com/questions/25385916
复制相似问题