Objective c 2个UIPickerView,每个都有自己的UILabel来显示NSMutableArray中的值

Objective c 2个UIPickerView,每个都有自己的UILabel来显示NSMutableArray中的值,objective-c,cocoa-touch,ios,uipickerview,Objective C,Cocoa Touch,Ios,Uipickerview,我的视图中有两个UIPickerViews和两个UILabel,UIPickerViews由NSMutableArray中的数字填充 选择器需要将选择的值发送到指定的标签。例如: _pickerView1(选择“18”) _pickerOutputLabel1(显示“18”) _pickerView2(选择“7”) _pickerOutputLabel2(显示“7”) 我无法使其工作,\u pickerView2还将其值发送到\u pickerOutputLabel1,而不是\u pickerO

我的视图中有两个UIPickerViews和两个UILabel,UIPickerViews由NSMutableArray中的数字填充

选择器需要将选择的值发送到指定的标签。例如:

_pickerView1(选择“18”) _pickerOutputLabel1(显示“18”)

_pickerView2(选择“7”) _pickerOutputLabel2(显示“7”)

我无法使其工作,\u pickerView2还将其值发送到\u pickerOutputLabel1,而不是\u pickerOutputLabel2

我试过几种方法,但我不知道如何让它发挥作用

这是代码(我删除了修复该问题的尝试,因此它至少可以编译:)

//头文件

#import <UIKit/UIKit.h>



@interface UIPickerViewAndLabelsViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {


NSMutableArray *nrArray;
IBOutlet UIPickerView *_pickerView1;
IBOutlet UIPickerView *_pickerView2;

UILabel *_pickerOutputLabel1;
    UILabel *_pickerOutputLabel2;

}
@property (nonatomic, retain) IBOutlet UIPickerView *pickerView1;
@property (nonatomic, retain) IBOutlet UIPickerView *pickerView2;
@property (nonatomic, retain) IBOutlet UILabel *pickerOutputLabel1;
@property (nonatomic, retain) IBOutlet UILabel *pickerOutputLabel2;
@end
#导入
@接口UIPickServiceAndLabelsViewController:UIViewController{
NSMUTABLEARRY*nrArray;
IBUIPickerView*PickerView;
IBUIPickerView*\U PickerView 2;
UILabel*_pickerOutputLabel1;
UILabel*_pickerOutputLabel2;
}
@属性(非原子,保留)IBUIPickerView*PickerView;
@属性(非原子,保留)IBUIPickerView*pickerView2;
@属性(非原子,保留)IBUILabel*pickerOutputLabel1;
@属性(非原子,保留)IBUILabel*pickerOutputLabel2;
@结束
//实现文件

#import "UIPickerViewAndLabelsViewController.h"

@implementation UIPickerViewAndLabelsViewController

@synthesize pickerView1 = _pickerView1;
@synthesize pickerView2 = _pickerView2;
@synthesize pickerOutputLabel1 = _pickerOutputLabel1;
@synthesize pickerOutputLabel2 = _pickerOutputLabel2;

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


// 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 {

_pickerOutputLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(400, 120, 50, 50)];
[self.view addSubview:_pickerOutputLabel1];

_pickerOutputLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(400, 320, 50, 50)];
[self.view addSubview:_pickerOutputLabel2];

nrArray = [[NSMutableArray alloc] init];

for (int i=0;i<20+1;i++) {

    [nrArray addObject:[NSString stringWithFormat:@"%d", i]];
}



_pickerView1 = [[UIPickerView alloc] initWithFrame:CGRectMake(500, 120, 100, 162)];


_pickerView1.delegate = self;
_pickerView1.dataSource = self;
_pickerView1.showsSelectionIndicator = YES;
_pickerView1.transform = CGAffineTransformMakeScale(0.8, 0.8);
[self.view addSubview:_pickerView1];
[_pickerView1 release];
[_pickerView1 selectRow:0 inComponent:0 animated:NO];

_pickerOutputLabel1.text = [nrArray objectAtIndex:[_pickerView1 selectedRowInComponent:0]];


_pickerView2 = [[UIPickerView alloc] initWithFrame:CGRectMake(500, 320, 100, 162)];


_pickerView2.delegate = self;
_pickerView2.dataSource = self;
_pickerView2.showsSelectionIndicator = YES;
_pickerView2.transform = CGAffineTransformMakeScale(0.8, 0.8);
[self.view addSubview:_pickerView2];
[_pickerView2 release];
[_pickerView2 selectRow:0 inComponent:0 animated:NO];

_pickerOutputLabel2.text = [nrArray objectAtIndex:[_pickerView2 selectedRowInComponent:0]];


    [super viewDidLoad];
}







- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)_pickerView1;
{
    return 1;
}


- (void)pickerView:(UIPickerView *)_pickerView1 didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _pickerOutputLabel1.text=    [nrArray objectAtIndex:row];
}

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

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

}








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

- (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 {
    [super dealloc];
}

@end
#导入“uiPickServiceAndLabelsViewController.h”
@UIPickServiceAndLabelsViewController的实现
@合成pickerView1=_pickerView1;
@合成pickerView2=_pickerView2;
@合成pickerOutputLabel1=\u pickerOutputLabel1;
@合成pickerOutputLabel2=_pickerOutputLabel2;
/*
//指定的初始值设定项。覆盖以执行加载视图之前所需的设置。
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
*/
//实现loadView以编程方式创建视图层次结构,而不使用nib。
/*
-(void)负荷视图{
}
*/
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad{
_pickerOutputLabel1=[[UILabel alloc]initWithFrame:CGRectMake(400,120,50,50)];
[self.view addSubview:_pickerOutputLabel1];
_pickerOutputLabel2=[[UILabel alloc]initWithFrame:CGRectMake(400,320,50,50)];
[self.view addSubview:_pickerOutputLabel2];
nrArray=[[NSMutableArray alloc]init];

对于UIPickerView委托方法中的(int i=0;i,您已将pickerView参数命名为“\u pickerView w1”。将该参数命名为与实例变量相同的变量并不意味着将仅为该选取器调用委托方法。它只是成为任何选取器调用委托方法的本地名称

由于已将两个选择器的委托都设置为self,因此两个选择器调用相同的方法

要判断是哪个选择器在打电话,有以下几种方法:

  • 在创建标记时,为每个标记设置不同的标记值,并在委托方法中检查标记(例如,
    \u pickerView1.tag=1;
    和委托方法中:
    if(pickerView.tag==1).

  • 或者,直接与实例变量进行比较。例如:

    - (void)pickerView:(UIPickerView *)pickerView //<-- std name as in doc
                didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        if (pickerView == _pickerView1)
              // Above:
              //   "pickerView" is the picker in which a row was selected
              //   "_pickerView1" is the actual instance variable
            _pickerOutputLabel1.text = [nrArray objectAtIndex:row];
        else
            _pickerOutputLabel2.text = [nrArray objectAtIndex:row];
    }
    
    -(void)pickerView:(UIPickerView*)pickerView/您也可以使用此选项:
    -(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row不完整项:(NSInteger)组件
    {


    }

    谢谢!我在代理中使用了.tag,它马上就起作用了。我知道我犯了一些愚蠢的错误。很好的解释,真的把事情弄清楚了。我希望其他初学者也能从中学习一些东西。还删除了IBOutlets:)
    if( [pickerView isEqual: picker ]){
        firststr = [firstArray objectAtIndex:row];
    }
    
    
    
    if( [pickerView isEqual: pickerAnother ]){
    
        secondstr = [secondArray objectAtIndex:row];
    
    
    }