我的视图中有两个UIPickerViews和两个UILabel,UIPickerViews中填充了NSMutableArray中的数字。
拣货员需要将选定的值发送到指定的标签。例如:
_pickerView1(已选中“18”) _pickerOutputLabel1(显示“18”)
_pickerView2(选中“7”) _pickerOutputLabel2(显示“7”)
我无法正常工作,_pickerView2也将其值发送到_pickerOutputLabel1而不是_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
//实施文件
#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
我正在尝试3天而且我被困住了。
提前致谢。
答案 0 :(得分:0)
在UIPickerView委托方法中,您已将pickerView参数命名为“_pickerView1”。命名该参数与实例变量 相同 意味着仅为该选择器调用委托方法。它只是调用委托方法的任何选择器的本地名称。
由于您已将两个选择器的委托都设置为自己,因此两个选择器都调用相同的方法。
要告诉哪个选择器正在拨打电话,有两种方法:
在创建每个标记时为每个标记值设置不同的标记值,并检查委托方法中的标记(例如_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];
}
此外,您在控件声明前面有IBOutlet
,但之后以编程方式创建它们。如果您使用Interface Builder来创建控件,请不要在代码中重新创建它们。如果您不使用IB,请删除IBOutlet
。
答案 1 :(得分:0)
你也可以用这个: - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if( [pickerView isEqual: picker ]){
firststr = [firstArray objectAtIndex:row];
}
if( [pickerView isEqual: pickerAnother ]){
secondstr = [secondArray objectAtIndex:row];
}
}