同一视图中的两个 uipicker

发布于 2025-01-03 04:11:45 字数 6254 浏览 1 评论 0原文

我想做的是将两个 uipicker 放在同一个视图上,我在某处读到其中一个应该有自己单独的委托,我尝试这样做,但无法使其正常工作。

有时,当我运行应用程序时,第二个应用程序会停止工作,控制台上没有错误。

这是视图的 .h 文件:

@interface FirstViewController : UIViewController {

IBOutlet UIPickerView *cities;
NSMutableArray *array;
NSString *picked;
}

@property(nonatomic,retain) IBOutlet UIPickerView *cities;
@property(nonatomic,retain) IBOutlet NSMutableArray *array;

这是 .m:

@implementation FirstViewController
@synthesize cities,array;

-(void) getCities:(NSString *)link{
    url=link;
    NSString *str=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
    if([str length]==0){
        [str release];
        return;
    }

    SBJsonParser *parser=[[SBJsonParser alloc] init];
    array=[[parser objectWithString:str] copy];
    [receivedData release];

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { // This method needs to be used. It asks how many columns will be used in the UIPickerView 
    return 1; // We only need one column so we will return 1. 
}


- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { // This method also needs to be used. This asks how many rows the UIPickerView will have. 
    return [array count]; // We will need the amount of rows that we used in the pickerViewArray, so we will return the count of the array. 
} 


- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { // what happens when selecting rows
    picked=[array objectAtIndex:row];
} 


- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { //method asks for what the title or label of each row will be
    return [array objectAtIndex:row]; // We will set a new row for every string used in the array. 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [sv setScrollEnabled:TRUE];
    [sv setContentSize:CGSizeMake(320, 800)];
    [self getCities:@"any url"];

    [super viewDidLoad];
}


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

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc
{
    [super dealloc];
}

@end

对于第二个 uipicker,我向视图添加了一个 nsobject,并将其类更改为我之前创建的“SecondPickerDelegate”,这是它的代码:
.h

#import <UIKit/UIKit.h>


@interface FirstViewSecondPickerDelegate :  UIViewController<UIPickerViewDelegate>{

    IBOutlet UIPickerView *specialities;
    NSMutableArray *array;
    NSString *picked;
}

@property(nonatomic,retain) IBOutlet UIPickerView *specialities;
@property(nonatomic,retain) NSMutableArray *array;
@property(nonatomic,retain) NSString *picker;

@end

.m 文件:

#import "FirstViewSecondPickerDelegate.h"
#import "JSON.h"


@implementation FirstViewSecondPickerDelegate
@synthesize specialities,array,picker;

-(void) getSpecialities:(NSString *)link{
    url=link;
    NSString *str=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
    if([str length]==0){
        [str release];
        return;
    }

    SBJsonParser *parser=[[SBJsonParser alloc] init];
    array=[[parser objectWithString:str] copy];
    for(int i=0;i<[array count];i++){
        NSLog(@"index %i",i);
        NSLog(@"value %@",[array objectAtIndex:i]);
    }


}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { // This method needs to be used. It asks how many columns will be used in the UIPickerView 
    return 1; // We only need one column so we will return 1. 
}


- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { // This method also needs to be used. This asks how many rows the UIPickerView will have. 
    return [array count]; // We will need the amount of rows that we used in the pickerViewArray, so we will return the count of the array. 
} 


- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { // what happens when selecting rows
    picked=[array objectAtIndex:row];
} 


- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { //method asks for what the title or label of each row will be
    return [array objectAtIndex:row]; // We will set a new row for every string used in the array. 
} 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        picked=@"1";
        [self getSpecialities:@"http://localhost:8080/Test/gs"];
    }


    return self;
}
/*
-(void) viewDidLoad{
    [super viewDidLoad];

}

*/

- (void)dealloc
{

    [super dealloc];
}

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

#pragma mark - View lifecycle

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

    [super viewDidLoad];
}


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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

如果有人有此类情况的工作示例,请提供帮助。

what i am trying to do is to put two uipickers on the same view, i read somewhere that one of them should have its own separate delegate i tried to do so but i couldn't make it work properly.

Some times when i run the application the second application just stops working with no errors on the console.

this is the .h file for the view:

@interface FirstViewController : UIViewController {

IBOutlet UIPickerView *cities;
NSMutableArray *array;
NSString *picked;
}

@property(nonatomic,retain) IBOutlet UIPickerView *cities;
@property(nonatomic,retain) IBOutlet NSMutableArray *array;

and here is the .m:

@implementation FirstViewController
@synthesize cities,array;

-(void) getCities:(NSString *)link{
    url=link;
    NSString *str=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
    if([str length]==0){
        [str release];
        return;
    }

    SBJsonParser *parser=[[SBJsonParser alloc] init];
    array=[[parser objectWithString:str] copy];
    [receivedData release];

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { // This method needs to be used. It asks how many columns will be used in the UIPickerView 
    return 1; // We only need one column so we will return 1. 
}


- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { // This method also needs to be used. This asks how many rows the UIPickerView will have. 
    return [array count]; // We will need the amount of rows that we used in the pickerViewArray, so we will return the count of the array. 
} 


- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { // what happens when selecting rows
    picked=[array objectAtIndex:row];
} 


- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { //method asks for what the title or label of each row will be
    return [array objectAtIndex:row]; // We will set a new row for every string used in the array. 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [sv setScrollEnabled:TRUE];
    [sv setContentSize:CGSizeMake(320, 800)];
    [self getCities:@"any url"];

    [super viewDidLoad];
}


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

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc
{
    [super dealloc];
}

@end

and for the second uipicker i added an nsobject to the view and changed its class to "SecondPickerDelegate" which i created before and this is its code:
.h

#import <UIKit/UIKit.h>


@interface FirstViewSecondPickerDelegate :  UIViewController<UIPickerViewDelegate>{

    IBOutlet UIPickerView *specialities;
    NSMutableArray *array;
    NSString *picked;
}

@property(nonatomic,retain) IBOutlet UIPickerView *specialities;
@property(nonatomic,retain) NSMutableArray *array;
@property(nonatomic,retain) NSString *picker;

@end

the .m file:

#import "FirstViewSecondPickerDelegate.h"
#import "JSON.h"


@implementation FirstViewSecondPickerDelegate
@synthesize specialities,array,picker;

-(void) getSpecialities:(NSString *)link{
    url=link;
    NSString *str=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
    if([str length]==0){
        [str release];
        return;
    }

    SBJsonParser *parser=[[SBJsonParser alloc] init];
    array=[[parser objectWithString:str] copy];
    for(int i=0;i<[array count];i++){
        NSLog(@"index %i",i);
        NSLog(@"value %@",[array objectAtIndex:i]);
    }


}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { // This method needs to be used. It asks how many columns will be used in the UIPickerView 
    return 1; // We only need one column so we will return 1. 
}


- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { // This method also needs to be used. This asks how many rows the UIPickerView will have. 
    return [array count]; // We will need the amount of rows that we used in the pickerViewArray, so we will return the count of the array. 
} 


- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { // what happens when selecting rows
    picked=[array objectAtIndex:row];
} 


- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { //method asks for what the title or label of each row will be
    return [array objectAtIndex:row]; // We will set a new row for every string used in the array. 
} 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        picked=@"1";
        [self getSpecialities:@"http://localhost:8080/Test/gs"];
    }


    return self;
}
/*
-(void) viewDidLoad{
    [super viewDidLoad];

}

*/

- (void)dealloc
{

    [super dealloc];
}

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

#pragma mark - View lifecycle

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

    [super viewDidLoad];
}


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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

If any one has a working example for such case please help.

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

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

发布评论

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

评论(1

﹂绝世的画 2025-01-10 04:11:45

您不需要为同一视图中的两个 UIPickerView 提供单独的委托。

使用 UIPickerView tag 属性,然后您可以在委托方法中区分它们。

You don't need separate delegates for two UIPickerView's in the same view.

Use the UIPickerView tag property, and then you can differentiate between them in delegate methods.

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