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

发布于 2024-10-20 08:39:47 字数 4444 浏览 6 评论 0原文

我的视图中有 2 个 UIPickerViews 和两个 UILabels,并且 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 天,但我被卡住了。

提前致谢。

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")

_pickerView2 (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.

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

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

发布评论

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

评论(2

时光与爱终年不遇 2024-10-27 08:39:47

在 UIPickerView 委托方法中,您将 pickerView 参数命名为“_pickerView1”。将该参数命名为与实例变量相同并不意味着只会为该选择器调用委托方法。它只是成为任何选择器调用委托方法的本地名称。

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

要判断哪个选择器正在进行调用,有几种方法:

  • 在创建它们时为每个选择器设置不同的标记值,并在委托方法中检查标记(例如。_pickerView1.tag = 1;< /code> 并在委托方法中: if (pickerView.tag == 1)... )

  • 或者,比较直接针对实例变量。例如:

    - (void)pickerView:(UIPickerView *)pickerView //<-- 文档中的 std 名称
                didSelectRow:(NSInteger)行 inComponent:(NSInteger)组件
    {
        if (pickerView == _pickerView1)
              // 多于:
              // “pickerView”是选择行的选择器
              // “_pickerView1”是实际的实例变量
            _pickerOutputLabel1.text = [nrArray objectAtIndex:row];
        别的
            _pickerOutputLabel2.text = [nrArray objectAtIndex:row];
    }
    

另外,您可以在控件声明前面添加 IBOutlet,然后以编程方式创建它们。如果您使用 Interface Builder 创建控件,请勿在代码中重新创建它们。如果您不使用 IB,请删除 IBOutlet

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.

我为君王 2024-10-27 08:39:47

你也可以使用这个:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)行 inComponent:(NSInteger)组件
{

if( [pickerView isEqual: picker ]){
    firststr = [firstArray objectAtIndex:row];
}



if( [pickerView isEqual: pickerAnother ]){

    secondstr = [secondArray objectAtIndex:row];


}

}

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


}

}

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