动态选择 UIPickerView 中的一行

发布于 2024-09-19 02:38:08 字数 465 浏览 4 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烂人 2024-09-26 02:38:08

将按钮挂接到一个操作方法,然后在您的操作方法中只需调用相同的 selectRow:inComponent:animated: 方法,即可

- (IBAction)button1Action {
    [picker selectRow:20 inComponent:0 animated:YES];
}

将按钮挂接到 Interface Builder 中的该方法。

Hook the button to an action method, then in your action method simply call the same selectRow:inComponent:animated: method, i.e.

- (IBAction)button1Action {
    [picker selectRow:20 inComponent:0 animated:YES];
}

And you hook up the button to that method in Interface Builder.

笑咖 2024-09-26 02:38:08

还要确保选择器是用“IBOutlet”定义的,如下所示。并将选择器的参考插座连接到此:

@property (nonatomic, retain) IBOutlet UIPickerView *picker;

Also make sure that picker is defined with "IBOutlet" as follows. And connect the picker's referencing outlet to this:

@property (nonatomic, retain) IBOutlet UIPickerView *picker;
站稳脚跟 2024-09-26 02:38:08

您可以将与 viewDidLoad 中保留的相同代码放在单击按钮时触发的事件上选择 pickerRow 。

-(void)button1clickevent{
//FUNCTION MUST GET FIRE WHEN BUttON ! IS CLICKED
       [picker selectRow:50 inComponent:0 animated:YES];
}

快乐的编码...

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.

-(void)button1clickevent{
//FUNCTION MUST GET FIRE WHEN BUttON ! IS CLICKED
       [picker selectRow:50 inComponent:0 animated:YES];
}

HAPPY iCODING...

A君 2024-09-26 02:38:08

好的,现在我重新创建项目并重新输入代码:

- (IBAction) goToRowTwenty  
 {   
    [picker selectRow:20 inComponent:0 animated:YES];   
    [timerPickerView reloadComponent:0];  
 }  

它有效,我真的不知道发生了什么,但是谢谢你们回答我的问题。

OK, now I recreate the project and retype the code:

- (IBAction) goToRowTwenty  
 {   
    [picker selectRow:20 inComponent:0 animated:YES];   
    [timerPickerView reloadComponent:0];  
 }  

and it works, I really don't know what happened, but thank you guys for answering my question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文