Ios UIPickerView没有';不出现

Ios UIPickerView没有';不出现,ios,cocoa-touch,uipickerview,Ios,Cocoa Touch,Uipickerview,在通过push调用ViewController时,我尝试以编程方式显示一个组合框。此组合框实现UIPickerView委托协议并添加.xib文件 当我运行应用程序时,我可以在屏幕上看到我的组合框,但当我单击它时,没有任何附加。通常会显示pickerview 我不明白的是,在另一个viewcontroller调用model中,它工作正常 // // ComboBox.h // #import <UIKit/UIKit.h> @interface ComboBox : UIView

在通过push调用ViewController时,我尝试以编程方式显示一个组合框。此组合框实现UIPickerView委托协议并添加.xib文件

当我运行应用程序时,我可以在屏幕上看到我的组合框,但当我单击它时,没有任何附加。通常会显示pickerview

我不明白的是,在另一个viewcontroller调用model中,它工作正常

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

谢谢你的回答

我忘记了细节,但我记得有同样的问题,但对我来说,我需要将其链接到委托、数据源或其他什么?我不是100%确定,因为这已经有一段时间了,但请确保当您的视图中有它时,您将它链接到您的选择器引用+您需要的其他内容。

您的
组合框
类未设置为
UITextField
的委托,因此
textfield应该开始编辑:
永远不会被调用。

我假设您的目标是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];

    }
需要检查的几个步骤

  • 使用maskType
    UIViewAutoresizingNone
    使组合框的视图成为自由形式
  • 检查textField和textField的委托是否已连接

  • 我尝试在我的viewcontroller中使用这个combo类,我尝试了你给我的所有解决方案,但没有任何效果,所以解决方案是直接在我的viewcontroller中实现所有combo类代码,现在它可以工作了,但这有点难看…

    picker委托方法正在调用或未调用,请检查断点。连接到UITextField的方法showPicker在i clic时不调用on@Nagasaki:你能改进你的问题吗?我的问题是为什么combox在模式VC中工作,为什么在推送VC中不工作?你是如何添加pickerView的?通过编程方式还是在xib?@界面组合框中:UIViewController是的。我会尝试一下,但您所说的选中textField的委托是什么意思connected@Nagasaki如果要实现UITextFieldDelegate,可以通过IB或代码连接它。这就是我的意思。@Nagasaki您是否测试了我提供的演示项目,如果它不工作,请确保ComboBox的视图不会自动调整为小于textField。我没有测试它,但正如我所说,ComboBox在调用modal的ViewController中工作正常,在我调用push segue的另一个控件中不工作。。。我将尝试取消选中视图的自动调整大小,我将尽快告诉您。@Nagasaki您从未提到Push Segue。如果像视图一样使用viewController,则需要执行更多步骤才能使其正常工作。您使用的是类似UIView的组合框。
    - (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];
    
        }