2011-03-03 48 views
0

我在我的視圖中有2個UIPickerViews和兩個UILabels,並且UIPickerViews是用NSMutableArray中的數字填充的。2 UIPickerViews每個都有自己的UILabel來顯示NSMutableArray的值

挑選者需要將選擇的值發送給那裏指定的標籤。例如:

_pickerView1(選擇了 「18」) _pickerOutputLabel1(顯示 「18」)

_pickerView2(選擇了 「7」) _pickerOutputLabel2(顯示 「7」)

我不能得到這個工作時,_pickerView2也會將其值發送到_pickerOutputLabel1而不是_pickerOutputLabel2。

我已經嘗試了幾件事,但我無法弄清楚如何得到它的工作。

這是代碼(我打消了我的努力來解決這個問題,使之ATLEAST編譯:)

//頭文件

#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

在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

+0

謝謝!我在委託中使用了.tag,它立即工作。我知道我犯了一些愚蠢的錯誤。優秀的解釋,真正解決了問題。我希望其他初學者也能從中學到一些東西。也刪除了IBOutlets :) – ivoos 2011-03-04 08:51:50

0

,你也可以使用這個: - (空)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger的)行inComponent:(NSInteger的)組件 {

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



if([pickerView isEqual: pickerAnother ]){ 

    secondstr = [secondArray objectAtIndex:row]; 


} 

}

相關問題