如何创建 3 组件 UIPickerView(模板任何人???)iPhone

发布于 2024-11-18 11:47:07 字数 4276 浏览 2 评论 0原文

IPHONE 问题...我尝试了几种不同的模板并从其他人那里获得了帮助,但我似乎无法使其正常工作,而且应该不会太困难。我想添加一个带有“3 个组件”的 UIPickerView,每个组件都是数值(例如,他们在每个组件中选择 50-100 之间),并且它显示他们在消息窗口中选择的内容。 (计算部分我稍后会弄清楚,但首先我需要让组件正确注册第三列,出现一个又一个问题......有什么想法吗?

*我不是最有经验的,并听说这是一个从一些非常聪明的人那里获得答案的好地方,我真的很感谢任何人可以为 3 组件 UIPickerView 提供的任何帮助或大纲“模板”... 提前致谢>>

劳伦斯是我的 ViewController.m 代码:

#import "DoubleComponentPickerViewController.h"

@implementation DoubleComponentPickerViewController

@synthesize doublePicker;
@synthesize oneTypes;
@synthesize twoTypes;
@synthesize threeTypes;


// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}


 -(IBAction)buttonPressed
{
    NSInteger oneRow = [doublePicker selectedRowInComponent:
                          kOneComponent];
    NSInteger twoRow = [doublePicker selectedRowInComponent:
                          kTwoComponent];
    NSInteger threeRow = [doublePicker selectedRowInComponent:
                          kThreeComponent];
    NSString *one = [oneTypes objectAtIndex:oneRow];
    NSString *two = [twoTypes objectAtIndex:twoRow];
    NSString *three = [threeTypes objectAtIndex:threeRow];

    NSString *message = [[NSString alloc] initWithFormat:
     @"First = %@         Second = %@          Third = %@",one, two, three];

    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Please Confirm Numbers" message:message delegate:nil cancelButtonTitle:@"Confirm" otherButtonTitles:nil];
    [alert show];
    [alert release];
    [message release];
}


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    NSArray *oneArray = [[NSArray alloc] initWithObjects:
    @"91",@"92",@"93",@"94",@"95",@"96",nil];
    self.oneTypes = oneArray;
    [oneArray release];

    NSArray *twoArray = [[NSArray alloc] initWithObjects:
    @"97",@"98",@"99",@"100",@"101",@"102",@"103", nil];
    self.twoTypes = twoArray;
    [twoArray release];

    NSArray *threeArray = [[NSArray alloc] initWithObjects:
    @"102",@"103",@"104",@"105",@"106",@"107", nil];
    self.threeTypes = threeArray;
    [threeArray release];
    [super viewDidLoad];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [doublePicker release];
    [oneTypes release];
    [twoTypes release];
    [threeTypes release];
    [super dealloc];
}


 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  {
    return 3;
  }

 -(NSInteger)pickerView:(UIPickerView *)pickerView
   numberOfRowsInComponent:(NSInteger)component
  {
      if (component == kOneComponent)
        return[self.oneTypes count];
        return[self.twoTypes count];
        return[self.threeTypes count];

      if (component == kTwoComponent)
        return[self.oneTypes count];
        return[self.twoTypes count];
        return[self.threeTypes count];

      if (component == kThreeComponent)
        return[self.oneTypes count];
        return[self.twoTypes count];
        return[self.threeTypes count];
  }

 -(NSString *)pickerView:(UIPickerView *)pickerView
  titleForRow:(NSInteger)row
    forComponent:(NSInteger)component 
  {
    if (component == kThreeComponent)
    return [self. threeTypes objectAtIndex:row];
    return [self. twoTypes objectAtIndex:row];
    return [self. oneTypes objectAtIndex:row];

  }



@end

IPHONE problems ...I have tried several different templates and help from others, but I just can not seem to get this working, and it shouldn't be too difficult. I am wanting to add a UIPickerView with "3 Components" each are of numeric values (ex. they choose between 50-100 in each) and it shows what they selected in msg window. (The calculation part I will figure out later, but first I need to get the components registering the 3rd column properly, having problems after problems.... ANY IDEAS??

* I am not the most experienced, and heard that this was a great place to get answers from some very smart people. I truly appreciate any help or an outline "TEMPLATE" anyone could offer for a 3 component UIPickerView... THANKS IN ADVANCE>>> Lawrence

This is my ViewController.m code:

#import "DoubleComponentPickerViewController.h"

@implementation DoubleComponentPickerViewController

@synthesize doublePicker;
@synthesize oneTypes;
@synthesize twoTypes;
@synthesize threeTypes;


// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}


 -(IBAction)buttonPressed
{
    NSInteger oneRow = [doublePicker selectedRowInComponent:
                          kOneComponent];
    NSInteger twoRow = [doublePicker selectedRowInComponent:
                          kTwoComponent];
    NSInteger threeRow = [doublePicker selectedRowInComponent:
                          kThreeComponent];
    NSString *one = [oneTypes objectAtIndex:oneRow];
    NSString *two = [twoTypes objectAtIndex:twoRow];
    NSString *three = [threeTypes objectAtIndex:threeRow];

    NSString *message = [[NSString alloc] initWithFormat:
     @"First = %@         Second = %@          Third = %@",one, two, three];

    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Please Confirm Numbers" message:message delegate:nil cancelButtonTitle:@"Confirm" otherButtonTitles:nil];
    [alert show];
    [alert release];
    [message release];
}


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    NSArray *oneArray = [[NSArray alloc] initWithObjects:
    @"91",@"92",@"93",@"94",@"95",@"96",nil];
    self.oneTypes = oneArray;
    [oneArray release];

    NSArray *twoArray = [[NSArray alloc] initWithObjects:
    @"97",@"98",@"99",@"100",@"101",@"102",@"103", nil];
    self.twoTypes = twoArray;
    [twoArray release];

    NSArray *threeArray = [[NSArray alloc] initWithObjects:
    @"102",@"103",@"104",@"105",@"106",@"107", nil];
    self.threeTypes = threeArray;
    [threeArray release];
    [super viewDidLoad];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [doublePicker release];
    [oneTypes release];
    [twoTypes release];
    [threeTypes release];
    [super dealloc];
}


 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  {
    return 3;
  }

 -(NSInteger)pickerView:(UIPickerView *)pickerView
   numberOfRowsInComponent:(NSInteger)component
  {
      if (component == kOneComponent)
        return[self.oneTypes count];
        return[self.twoTypes count];
        return[self.threeTypes count];

      if (component == kTwoComponent)
        return[self.oneTypes count];
        return[self.twoTypes count];
        return[self.threeTypes count];

      if (component == kThreeComponent)
        return[self.oneTypes count];
        return[self.twoTypes count];
        return[self.threeTypes count];
  }

 -(NSString *)pickerView:(UIPickerView *)pickerView
  titleForRow:(NSInteger)row
    forComponent:(NSInteger)component 
  {
    if (component == kThreeComponent)
    return [self. threeTypes objectAtIndex:row];
    return [self. twoTypes objectAtIndex:row];
    return [self. oneTypes objectAtIndex:row];

  }



@end

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

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

发布评论

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

评论(1

儭儭莪哋寶赑 2024-11-25 11:47:07

您可以实现数据源方法

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

,然后可以实现委托方法

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

You can implement datasource methods

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

and then you can implement delegate method

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