Dynamically show/hide a UIPickerView that was created using the interface builder

I’m trying to create an iPad/iPhone app that dynamically creates menus depending on a JSON object that is returned from a RESTful API. Specifically I’m trying to show/hide a UIPickerView that I created using the interface builder. In the properties menu in the interface builder I checked the box “hidden” for that UIPickerView. I used this tutorial to create to UIPickerView. I’ve set the delegate and data source to the View Controller using the interface builder. I’d like to unhide/show the UIPickerView when a certain condition is met. So far I’ve tried the following code:

 [self.choicePicker setHidden:NO];
 self.choicePicker.hidden = NO;

I usually build such object programmatically but I thought I’d try it this way. I’ve looking through various stackoverflow posts and doing research but I can’t seem to find something that works. I’m new to programming in Objective C. Thanks in advance any help is greatly appreciated.

.h file code

@interface slrpViewController : UIViewController<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>

IBOutlet UIPickerView *picker_choice;

NSMutableArray *dataArray;
NSMutableData *receivedData;

 @property(nonatomic, strong) UILabel *nameLabel;
 @property(nonatomic, retain) UIPickerView *choicePicker;

.m file code

-(void)buildChoicesMenu:(NSDictionary *)choiceDict{
//in this method we build the choices menu

[self.choicePicker setHidden:NO];
self.choicePicker.hidden = NO;
if (self.choicePicker) self.choicePicker.hidden = !self.choicePicker.hidden;

1 Solution

You have two different picker views defined. One a property choicePicker (and an implicit _choicePicker instance variable), and another instance variable picker_choice. It seems you have connected your picker_choice in interface builder, but are trying to modify the property. In fact, if you try to print po self.choicePicker in the debugger, you would get nil, because there is nothing filling it.

Either remove the instance variable, and connect your property in interface builder, or synthesize your property with your instance variable by doing so:

@syntesize choicePicker=picker_choice