Nagasaki
Nagasaki

Reputation: 58

UIPickerView doesn't appear

In a ViewController call by push, I try to programmatically display a ComboBox. This combobox implement UIPickerView delegate protocol and add a .xib file.

When i run the app, i can see my combobox on the screen, but when i click on it, nothing append. Normally the pickerview will be displayed.

What i don't understand, is in another viewcontroller call modal it works fine

//
//  ComboBox.h
//

#import <UIKit/UIKit.h>

@interface ComboBox : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>
{
    UIPickerView* pickerView;
    IBOutlet UITextField* textField;
    NSMutableArray *dataArray;
}

-(void) setComboData:(NSMutableArray*) data; //set the picker view items
-(void) setPlaceholder:(NSString*) label;
@property (retain, nonatomic) NSString* selectedText; //the UITextField text
@property (retain, nonatomic) IBOutlet UITextField* textField; //the UITextField

@end

//
//  ComboBox.m
//


#import "ComboBox.h"

@implementation ComboBox
@synthesize selectedText;
@synthesize textField;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


//-- UIPickerViewDelegate, UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    textField.text = [dataArray objectAtIndex:row];
    selectedText = textField.text;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    return [dataArray count];
}

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

//-- ComboBox


-(void) setComboData:(NSMutableArray*) data
{
    dataArray = data;    
}

-(void) setPlaceholder:(NSString *)label
{
    textField.placeholder = label;
}

-(void)doneClicked:(id) sender
{
    [textField resignFirstResponder]; //hides the pickerView
}


- (IBAction)showPicker:(id)sender {

    pickerView = [[UIPickerView alloc] init];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;

    UIToolbar* toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    [toolbar sizeToFit];

    //to make the done button aligned to the right
    UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];


    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                   style:UIBarButtonItemStyleDone target:self
                                                                  action:@selector(doneClicked:)];


    [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];

    //custom input view
    textField.inputView = pickerView;
    textField.inputAccessoryView = toolbar;  
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)aTextField
{
    [self showPicker:aTextField];
    return YES;
}

@end

the viewdidload of my viewcontroller

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray* ServeurArray = [[NSMutableArray alloc] init];
    [ServeurArray addObject:@"1"];
    [ServeurArray addObject:@"2"];
    [ServeurArray addObject:@"3"];

    comboServeur = [[ComboBox alloc] init];
    [comboServeur setComboData:ServeurArray];  //Assign the array to ComboBox
    comboServeur.view.frame = CGRectMake(95, 220, 130, 31);  //ComboBox location and size (x,y,width,height)

    [self.view addSubview:comboServeur.view];
}

thx for your answers

Upvotes: 1

Views: 1049

Answers (4)

Nagasaki
Nagasaki

Reputation: 58

i try to use this combo class in my viewcontroller, i try all the solution you give to me, but nothing work, so the solution is to implement all the combo class code directly in my viewcontroller, and now it works, but it's a little bit uggly...

Upvotes: 0

Anupdas
Anupdas

Reputation: 10201

I assume that you are targeting iOS 5.0 and above. Since you are adding a view of a viewController to another viewController you can use the childViewController introduced in iOS 5.0.

Modify your viewDidLoad method

- (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

        ComboBox *comboServeur = [[ComboBox alloc]initWithNibName:@"ComboBoxController" bundle:nil];
        [comboServeur setComboData:@[@"1",@"2",@"3"]];

        comboServeur.view.frame = CGRectMake(50.0f, 200.0f, 220.0f, 31.0f);

        [self.view addSubview:comboServeur.view];
        [self addChildViewController:comboServeur];
        [comboServeur didMoveToParentViewController:self];

    }

Few steps to check

  1. Make the view of the ComboBox viewController freeform with maskType UIViewAutoresizingNone.
  2. Check the textField and delegate of textField is connected

Demo project

Upvotes: 2

NeoNacho
NeoNacho

Reputation: 680

Your ComboBox class isn't set as a delegate for the UITextField, so textFieldShouldBeginEditing: will never be called.

Upvotes: 0

Michael Choo
Michael Choo

Reputation: 1

I forget the specifics but I remember having the same problem but the thing for me was that I needed to link it to delegate or datasource or something? I'm not 100% sure since it's been quite a while but make sure when you have it on your view you link it to your picker reference + the other thing that you need.

Upvotes: 0

Related Questions