动态选择 UIPickerView 中的一行
我有一个使用 UIPickerView 和 3 个按钮的 iPhone 应用程序。
该选择器有一个包含 100 行的组件(0、1、2、...、98、99)。
我想实现代码,以便当我按下按钮时,
- button1:将使选择器选择第21行(其值为20)
- button2:将使选择器选择第41行(其值为40)
- button3:将让选择器选择第 60 行 (其值为 60)
我知道如何让选择器在视图首次加载时选择一行 将以下代码放入 viewDidLoad() 方法中:
- (void)viewDidLoad
{
........
[picker selectRow:50 inComponent:0 animated:YES];
}
但是当我点击按钮时,我无法让选择器选择所需的行。
I have an iPhone app that use UIPickerView and 3 buttons.
This picker has one component with 100 rows (0, 1, 2, ..., 98, 99).
I'd like to implement the code so that, when I press on the buttons
- button1: will make the picker select row 21th (which has value 20)
- button2: will make the picker select row 41th (which has value 40)
- button3: will make the picker select row 60th
(which has value 60)
I know how to make the picker select a row when the view first load
by put the following code in to viewDidLoad() method:
- (void)viewDidLoad
{
........
[picker selectRow:50 inComponent:0 animated:YES];
}
but I could not make the picker select the desired row when I tap the buttons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将按钮挂接到一个操作方法,然后在您的操作方法中只需调用相同的
selectRow:inComponent:animated:
方法,即可将按钮挂接到 Interface Builder 中的该方法。
Hook the button to an action method, then in your action method simply call the same
selectRow:inComponent:animated:
method, i.e.And you hook up the button to that method in Interface Builder.
还要确保选择器是用“IBOutlet”定义的,如下所示。并将选择器的参考插座连接到此:
Also make sure that picker is defined with "IBOutlet" as follows. And connect the picker's referencing outlet to this:
您可以将与 viewDidLoad 中保留的相同代码放在单击按钮时触发的事件上选择 pickerRow 。
快乐的编码...
you can put the same code that you have kept in viewDidLoad for selecting pickerRow on the event that get fires when button is clicked.
HAPPY iCODING...
好的,现在我重新创建项目并重新输入代码:
它有效,我真的不知道发生了什么,但是谢谢你们回答我的问题。
OK, now I recreate the project and retype the code:
and it works, I really don't know what happened, but thank you guys for answering my question.