2012-02-05 15 views
1

私は何をしようとしているのは、同じビューに2つのuipickersを置くことです。私はどこかでそれらの1つが別のデリゲートを持っているべきだと読んでいますが、正しく機能します。

アプリケーションを実行すると、2番目のアプリケーションがコンソールでエラーなしで動作しなくなることがあります。

これは、ビューの.hファイルである:
同じビューの2つのuipicker

@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のを追加し、その変更を

#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 
.H
:クラスiの前に作成され、これがそのコードである "SecondPickerDelegate" へ

.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 
     [email protected]"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 

場合は助けてください。

答えて

3

あなたは、同じビューに2 UIPickerView年代に別々の代表者を必要としません。

UIPickerViewtagプロパティを使用して、あなたは、デリゲートメソッドでそれらを区別することができます。

+1

または、コンセントまたはivarをそれぞれ指し、デリゲート/データソースメソッドでチェックします。 – jrturton

+0

ありがとう、私はそれぞれのピッカーにタグを与え、thePickerViewパラメータを比較して、現在は完全に動作しています。もう一度感謝します。 – 4kr4m

関連する問題