2013-04-18 39 views
1

在一個ViewController調用,我嘗試以編程方式顯示一個組合框。該組合框實現UIPickerView委託協議並添加一個.xib文件。UIPickerView不出現

當我運行應用程序時,我可以在屏幕上看到我的組合框,但是當我點擊它時,沒有任何附加內容。通常會顯示pickerview。

什麼我不明白,是在另一個視圖 - 控制通話模式它工作正常

// 
// 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 

我的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的viewDidLoad中對你的答案

+0

picker委託方法正在調用或不與斷點一起檢查。 – Balu

+0

連接到UITextField的方法showPicker不會調用 – Nagasaki

+0

@Nagasaki:你能改善你的問題嗎? – Meera

回答

0

我嘗試在我的ViewController使用這個組合類,我嘗試你給我所有的解決方案,但沒有工作,所以解決方案是直接在我的viewcontroller中實現所有組合類代碼,現在它可以工作,但它有點不好...

0

我忘記具體細節,但我記得有同樣的問題,但對我來說,我需要將它鏈接到委託或數據源或什麼?我不是100%確定的,因爲它已經有相當長的一段時間了,但要確保當你在視圖上有它時,將它鏈接到你的選擇器參考+你需要的其他東西。

0

您的ComboBox類未設置爲UITextField的代理,因此不會調用textFieldShouldBeginEditing:

+0

@interface組合框:UIViewController 是的。 – Nagasaki

2

我假設你的目標是iOS 5.0及以上版本。由於您將viewController的視圖添加到另一個viewController,因此您可以使用iOS 5.0中引入的childViewController。

修改您的viewDidLoad方法

- (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]; 

    } 

幾個步驟來檢查

  1. 充分利用ComboBox的viewController自由與maskType UIViewAutoresizingNone的視圖。
  2. 檢查文本框和文本框的委託連接

Demo project

+0

我會嘗試,但你是什麼意思檢查代表textField連接 – Nagasaki

+0

@Nagasaki如果你正在實現UITextFieldDelegate,你可以連接它通過IB或代碼。我正是這個意思。 – Anupdas

+0

它不工作:( – Nagasaki