XCode:如何在代码中直接用内容填充UIPickerView?

发布于 2024-12-03 11:04:15 字数 346 浏览 1 评论 0原文

我该怎么做?我想用欧元、美元、英镑等值填充它,并在点击相应行时将值粘贴到文本字段中。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
// Make a new view, or do what you want here
UIPickerView *picker = [[UIPickerView alloc] 
                        initWithFrame:CGRectMake(0, 244, 320, 270)];

[self.view addSubview:picker];


return NO;
}

How do I do that? i want to fill it with values like EURO, USD, POUND and so on and paste the value into a textfield when i tap on the corresponding row.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
// Make a new view, or do what you want here
UIPickerView *picker = [[UIPickerView alloc] 
                        initWithFrame:CGRectMake(0, 244, 320, 270)];

[self.view addSubview:picker];


return NO;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

仅此而已 2024-12-10 11:04:15
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
     myTextField.text= [PickerArray objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
     myTextField.text= [PickerArray objectAtIndex:row];
}
花桑 2024-12-10 11:04:15

您应该在委托方法 -(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 中实现,该方法将返回标题 @"EURO"< /code>, @"USD", @"POUND", @"RUB" 为您的行。

例如,

-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch(component)
    {
        case 0:
            return @"EURO";
        case 1:
            return @"USD";
        case 2:
            return @"POUND";
        case 3:
            return @"RUB";
    }
    return @"";
}

You should implement in your delegate method -(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component that will return titles @"EURO", @"USD", @"POUND", @"RUB" for your rows.

For example,

-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch(component)
    {
        case 0:
            return @"EURO";
        case 1:
            return @"USD";
        case 2:
            return @"POUND";
        case 3:
            return @"RUB";
    }
    return @"";
}
維他命╮ 2024-12-10 11:04:15

在您的委托方法中编写代码

-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{
返回字符串;
在数组

中添加这样的@“一”,@“二”,@“三”,@“四”

write code in your delegate method

-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{
return string;
}

In Array add like this @"one", @"two", @"three", @"four"

So尛奶瓶 2024-12-10 11:04:15

正如其他人已经写的那样,您必须添加上面提到的委托方法,以使用数组中的项目填充 PickerView。要将值写入文本字段,您可以通过两种方式执行此操作。您可以编写一个 pickerView didSelectRow: 委托方法,如下所示:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

myTextField.text= [myArray objectAtIndex:row];

}

每次您点击一行时,这都会更新文本字段。另一种方法是在按下按钮或执行其他操作时拉取 pickerView 的值。类似于

- (void)someAction:(id)sender
{

    NSInteger row = [myPicker selectedRowInComponent:0];
    myTextField.text = [myArray objectAtIndex:row];

}

As others have already written you have to add the above mentiond delegate method to fill up your PickerView with items from an array. For writing the value into a textfield, you can do this in 2 ways. You can either do write a pickerView didSelectRow: delegate method like this:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

myTextField.text= [myArray objectAtIndex:row];

}

This will update the textField each time you tap on a row. The other way is that you pull the value of the pickerView on a button press or on some other action. Similar to

- (void)someAction:(id)sender
{

    NSInteger row = [myPicker selectedRowInComponent:0];
    myTextField.text = [myArray objectAtIndex:row];

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