2011-09-09 22 views
3

我認爲這可能是一個非常基本的問題,但經過幾個小時的搜索後,我可以找到一些與我的問題的人,但沒有找到工作的解決方案。UIPickerView ios問題 - 無法識別的選擇器

雖然我已經編寫了Android和Blackberry應用程序,但我剛剛開始使用iPhone開發,所以Objective-C與Java有點相似。

首先,當我試圖與它一個UIPickerView打開屏幕,我收到此錯誤信息:

[UIViewController中numberOfComponentsInPickerView:]:無法識別的選擇發送到實例0x4b4e0d0 2011-09- 09 15:57:19.619的TabBar [4945:207] *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [UIViewController中numberOfComponentsInPickerView:]:無法識別的選擇發送到實例0x4b4e0d0'

ħ ERE是我的頭文件:

#import <UIKit/UIKit.h> 

@interface ThirdViewController : UIViewController <UIPickerViewDelegate,  UIPickerViewDataSource>{ 
UILabel *setUnits; 
UIPickerView *pickerView; 
NSMutableArray *pickItems; 
} 
@property (retain, nonatomic) UIPickerView *pickerView; 
@property (retain, nonatomic) NSMutableArray *pickItems; 

@end 

這裏是我的執行文件:

#import "ThirdViewController.h" 

@implementation ThirdViewController 

@synthesize pickerView; 
@synthesize pickItems; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    // Custom initialization 
    } 
    return self; 
} 

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

    pickItems = [[NSMutableArray alloc] init]; 
    [pickItems addObject:@"Pounds"]; 
    [pickItems addObject:@"Kilograms"]; 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 

    return 1; //give components here 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component { 

    return [pickItems count]; //give rows here 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    return [pickItems objectAtIndex:row]; // give titles 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    //Do the step here 
} 

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

我已經查看過,無論是委託和數據源連接到文件的所有者。我錯過了什麼嗎?

謝謝。

回答

2

我懷疑在Interface Builder/xib文件中,您需要將您的UIViewController類更改爲ThirdViewController類。在任何情況下,錯誤消息都會告訴您,您正在將消息發送給UIViewController實例,而不是您創建的子類的實例。常見的(但不是唯一的)原因是IB/xib設置 - 但是您也是通過編程來編寫錯誤。 (使用IB/xib很容易,即使您已經使用iOS一段時間。)

+0

我將IB中的類更改爲ThirdViewController類,並修復它。謝謝! – coder

1

當您撥打numberOfComponentsInPickerView:時,看起來您正在調用UIViewController的實例,而不是ThirdViewController的實例。如果有必要,可以使用viewController來確保編譯器知道它是後者的一個實例。

+1

正確的診斷,但這裏的錯誤是運行時不編譯時間。或者我是愚蠢的? – Obliquely

2

您在初始化pickerView的位置?如果你正在做xib,那麼它應該連接,我可以在你的代碼中找到pickerView作爲IBOutlet。

相關問題