Objective-C:以编程方式创建的UIPickerView没有显示任何内容,但IBOutlet有效

时间:2014-08-25 10:12:15

标签: ios objective-c xcode uipickerview

我正在使用纯代码创建UIPickerView,我将UIPickerView作为ContentView CXAlertView datasourcehttps://github.com/ChrisXu1221/CXAlertView)中的子视图。
我已经配置了delegateAlertView's但是当我运行它时,ContentView NSLog中没有显示任何内容,其中pickerview将我的组件的数字返回为1 。 这里奇怪的是,当我将IBOutlet链接为HJCouponViewController.m时,一切正常。 请帮忙。

(PS:请忽略我的代码中的实例,它们都在工作)

#import "HJCouponViewController.h" #import "CXAlertView.h" @interface HJCouponViewController()<UIPickerViewDelegate, UIPickerViewDataSource> { NSArray *largeArray; NSArray *smallArray; } @property (strong,nonatomic) UIPickerView *prizePicker; @end @implementation -(void)viewDidLoad{ [self.prizePicker setDelegate:self]; [self.prizePicker setDataSource:self]; } - (IBAction)addPicker:(id)sender { CXAlertView *pickerAlert = [[CXAlertView alloc]initWithTitle:@"Select one" message:@"Choose From Below" cancelButtonTitle:@"Cancel"]; [pickerAlert addButtonWithTitle:@"Select" type:CXAlertViewButtonTypeDefault handler:^(CXAlertView *alertview, CXAlertButtonItem *button){ }]; [pickerAlert setContentView:[self prizePickerContent]]; [self.prizePicker reloadAllComponents]; [pickerAlert show]; NSLog(@"%d",prizePicker.numberOfComponents); } #pragma mark - Pickerview Datasource -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2; } -(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (component == 0) { return 80; } return 160; } -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component{ if (component == 0) { return 3; } if (component == 1) { NSInteger index = [prizePicker selectedRowInComponent:0]; switch (index) { case 0: return 1; break; case 1: return smallArray.count; break; case 2: return largeArray.count; break; default: break; } } return 0; } -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component == 0) { NSArray *leftComponentNameArray = @[@"Favourite",@"Small Prizes",@"Large Prizes"]; return [leftComponentNameArray objectAtIndex:row]; } if (component == 1) { NSInteger index = [prizePicker selectedRowInComponent:0]; switch (index) { case 0: return @"Not Set yet"; break; case 1:{ HJInstance *smallInstance = [smallArray objectAtIndex:row]; return smallInstance.Name; break; } case 2:{ HJInstance *largeInstance = [largeArray objectAtIndex:row]; return largeInstance.Name; } break; default: break; } } return nil; } @end

{{1}}

2 个答案:

答案 0 :(得分:0)

我错过了一个将选择器添加到视图层次结构的行:

[self.view addSubview:pickerAlert];

答案 1 :(得分:0)

-(void)ViewDidLoad

需要

-(void)viewDidLoad

(小写&#39; v&#39;)