开发者

2 UIPickerViews each having its own UILabel to display value from NSMutableArray

开发者 https://www.devze.com 2023-02-14 13:18 出处:网络
I\'m having 2 UIPickerViews and two UILabels in my view and the UIPickerViews are populated with numbers from an NSMutableArray.

I'm having 2 UIPickerViews and two UILabels in my view and the UIPickerViews are populated with numbers from an NSMutableArray.

The pickers need to send there chosen value to there assigned label. Example:

_pickerView1 (selected "18") _pickerOutputLabel1 (shows "18")

_pickerVie开发者_StackOverfloww2 (selected "7") _pickerOutputLabel2 (shows "7")

I can't get this working, _pickerView2 also sends its value to _pickerOutputLabel1 instead of _pickerOutputLabel2.

I've tried a couple of things but i can't figure out how to get it to work.

This is the code (i removed my attempts to fix the issue so it atleast compiles :)

//header file

#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

//implementation file

#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

I'm trying for 3 days and i'm stuck.

Thanks in advance.


In the UIPickerView delegate methods, you've named the pickerView parameter "_pickerView1". Naming that parameter the same as the instance variable does not mean the delegate method will be called only for that picker. It just becomes the local name for whatever picker calls the delegate method.

Since you've set the delegate for both the pickers to be self, both the pickers call the same methods.

To tell which picker is making the call, a couple of ways are:

  • Set a different tag value for each one when creating them and check the tag in the delegate method (eg. _pickerView1.tag = 1; and in the delegate method: if (pickerView.tag == 1)... )

  • Or, compare directly against the instance variable. For example:

    - (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];
    }
    


Also, you have IBOutlet in front of the control declarations but then you create them programmatically. If you are using Interface Builder to create the controls, don't re-create them in code. If you're not using IB, remove the IBOutlet.


you can also use this: - (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];


}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消