ios - UIPickerView 没有出现

标签 ios cocoa-touch uipickerview

push 的 ViewController 调用中,我尝试以编程方式显示 ComboBox。此组合框实现 UIPickerView 委托(delegate)协议(protocol)并添加一个 .xib 文件。

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

我不明白的是,在另一个 View Controller 调用 modal 它工作正常

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

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

谢谢你的回答

最佳答案

我假设您的目标是 iOS 5.0 及更高版本。由于您要将一个 viewController 的 View 添加到另一个 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. 使用 maskType UIViewAutoresizingNone 使 ComboBox viewController 的 View 自由。
  2. 检查 textField 和 textField 的委托(delegate)是否已连接

Demo project

关于ios - UIPickerView 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16085402/

相关文章:

swift - 运行我最近的 Realm 迁移挂起

ios - 如何在 IOS 的 uiwebview 中将 textalignment 属性设置为正确?

iphone - 在模拟器上运行 ios4.2 应用程序正常,在设备上,NIB View 为空白

ios - xcodebuild:错误:名为 "myWorkspace"的工作空间不包含名为

ios - 如何在 IOS 中获取不基于扩展名的 Mime 类型

objective-c - 计算 Cocoa-Touch 中调用方法的次数?

ios - PickerView 数据在使用多个 pickerview 时不正确

ios - 如何创建一个只有两列(小时和分钟)的简单(自定义)时间选择器?

ios - 点击它时尝试更改包含pickerview的单元格的高度

iphone - 是否可以判断一个应用程序被打开了多少次?