使用 UIPickerview

发布于 2025-01-07 05:27:41 字数 1340 浏览 1 评论 0原文

我在 Xcode 4.2 的选项卡式应用程序中编写了这段代码,它工作得很好,但我希望它是一个选择器视图中的下拉菜单,但我不确定我需要添加什么代码你们可以帮忙这是我的代码吗

.h
#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController {

    IBOutlet UIPickerView *pickerView;
    NSMutableArray *list;
    IBOutlet UILabel *picklabel;
}

@end


.m

#import "FirstViewController.h"

@implementation FirstViewController

-(NSInteger)numberOfComponentsInPickerview:(UIPickerView *)thePickerView {
    return 1;
}



-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [list count];
}


-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [list objectAtIndex:row];

}

-(void)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row inComponent:(NSInteger)component {

    NSString *string = [NSString stringWithFormat:@"you selected: %@" , [list objectAtIndex:row]];
    picklabel.text = string;

}


- (void)viewDidUnload
{
    [super viewDidUnload];

    list = [[NSMutableArray alloc] init];
    [list addObject:@"Giraffe"];
    [list addObject:@"Water Bottle"];
    [list addObject:@"spinning Fan"];
    [list addObject:@"Hariy Monkey"];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

I have this code that i did on Xcode 4.2 in a tabbed application and it works perfectly but what i would like it to be is a drop down menu in to a picker view but I'm not to sure what code i will need to add can you guys help heres my code

.h
#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController {

    IBOutlet UIPickerView *pickerView;
    NSMutableArray *list;
    IBOutlet UILabel *picklabel;
}

@end


.m

#import "FirstViewController.h"

@implementation FirstViewController

-(NSInteger)numberOfComponentsInPickerview:(UIPickerView *)thePickerView {
    return 1;
}



-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [list count];
}


-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [list objectAtIndex:row];

}

-(void)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row inComponent:(NSInteger)component {

    NSString *string = [NSString stringWithFormat:@"you selected: %@" , [list objectAtIndex:row]];
    picklabel.text = string;

}


- (void)viewDidUnload
{
    [super viewDidUnload];

    list = [[NSMutableArray alloc] init];
    [list addObject:@"Giraffe"];
    [list addObject:@"Water Bottle"];
    [list addObject:@"spinning Fan"];
    [list addObject:@"Hariy Monkey"];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

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

发布评论

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

评论(1

怀里藏娇 2025-01-14 05:27:41

请按照以下步骤操作:

  • 选择每个选取器视图时,将该数据添加到一个可变数组
  • 每次使用该数据(可变数组)重新加载表视图时,
  • 选定的数据将添加到您的表格列表中。

享受编码的乐趣:)

Follow this steps:

  • On selection of each pickerview, add that data to one mutable array
  • Each time reload your table view with that data (mutable array)
  • Selected data will get added to your tabular list.

Enjoy Coding :)

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