在导航控制器中使用UIPickerView

问题描述:

更新! 错误是在委托和选择器视图:)的DataSource在导航控制器中使用UIPickerView

好吧,这里的问题是: 我想,我已经创建了一个类“infoGeneral”的视图来实现一个UIPickerView。

到目前为止,应用程序的层次结构如下所示:当您单击“Crear proyecto nuevo”按钮时,将转到UiPickerView所在的其他视图。

App hierarchy(Dropbox的,我不能发表图片)

我看到如何使用UiPicker视图的教程,但是在默认视图。 所以我也跟着教程,但不是使用一些inits在viewDidLoad中,我做这件事是awakeFromNib,(因为viewDidLoad中是一个视图控制器的方法,而不是一个视图一个)

我设置好的了PickerView的数据源和委托,对视图在哪里。

但我得到这个错误:

2014-12-07 19:11:57.174 calcMuroLosas[6272:182705] -[UIViewController numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7fb620c88300 

而且我知道错误是因为我使用的是导航控制器。

因此,有人可以告诉我我可以使用该层次结构中的pickerview吗?

这里是我的文件:

infoGeneral.h

#import <UIKit/UIKit.h> 

@interface infoGeneral : UIView <UIPickerViewDelegate, UIPickerViewDataSource> 

@property (retain, nonatomic) IBOutlet UIPickerView *picker; 
- (IBAction)buttonPressed:(id)sender; 

@end 

infoGeneral.m

#import "infoGeneral.h" 
@interface infoGeneral() 
@property (strong, nonatomic) NSArray *arrayTipoCodigoEsfuerzosAdmisibles; 
@end 
@implementation infoGeneral 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect { 
    // Drawing code 
} 
*/ 

- (void)awakeFromNib 
{ 

    NSArray *dataTipoCodigoEsfuerzosAdmisibles = [[NSArray alloc] initWithObjects:@"MSJC", @"Guatemala", @"El Salvador", @"Honduras", @"Nicaragua", @"Costa Rica", @"Panamá (MSJC)", @"México", @"Chile", @"Colombia", @"Canada", @"Perú", @"Dominicada (similar a MSJC)", @"Ecuador", @"Otro", nil]; 
    self.arrayTipoCodigoEsfuerzosAdmisibles = dataTipoCodigoEsfuerzosAdmisibles; 
} 

- (IBAction)buttonPressed:(id)sender { 
    NSString *select = [_arrayTipoCodigoEsfuerzosAdmisibles objectAtIndex:[_picker selectedRowInComponent:0]]; 
    NSString *title = [[NSString alloc]initWithFormat:@"You selected %@!", select]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:@"YAY!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [alert show]; 

} 

#pragma mark Picker Data Sources Methods 
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return [_arrayTipoCodigoEsfuerzosAdmisibles count]; 
} 
#pragma mark Picker Delegate Methods 
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    return [_arrayTipoCodigoEsfuerzosAdmisibles objectAtIndex:row]; 
} 
@end 
+0

你是否勾起了选择器视图让文件所有者成为委托? – kpsharp 2014-12-08 02:32:39

+0

是的,我已经做到了 – 2014-12-08 05:39:15

+0

我发现错误! 2014-12-08 05:47:48

最简单的可能就是添加以下到您的infoGeneral viewDidLoad中:方法

[_picker setDelegate:self]; 

最有可能通过您的选取器视图属性检查器窗格在您的故事板中设置委托。

+0

我已经发现了错误,谢谢你:) – 2014-12-08 05:48:39

+0

所以它是一个问题,你的代表。好吧 – believesInSanta 2014-12-08 06:35:20

+0

是的,它是:) 再次感谢你 – 2014-12-08 07:12:31