在导航控制器中使用UIPickerView

时间:2014-12-08 01:57:12

标签: ios uipickerview

UPDATE! 错误发生在委托人和选择器视图的dataSource中:)

好的,这是问题所在: 我想在类" infoGeneral"的视图中实现UIPickerView。我已经创造了。

到目前为止,应用程序的层次结构如下:当您单击" Crear proyecto nuevo"按钮,您转到UiPickerView所在的另一个视图。

App hierarchy(Dropbox,我无法发布图片)

我看到了如何使用UiPicker视图的教程,但是在默认视图中。 所以我按照教程,但不是在viewDidLoad中使用一些内容,而是在awakeFromNib上做了,(因为viewDidLoad是一个ViewController方法,而不是View方法)

我将PickerView的dataSource和delegate设置为View所在的位置。

但是我收到了这个错误:

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

1 个答案:

答案 0 :(得分:0)

最简单的方法是在infoGeneral viewDidLoad:method

中添加以下内容
[_picker setDelegate:self];

最好通过选择器视图属性检查器窗格在故事板中设置委托。