1

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

当我运行该应用程序时,我可以在屏幕上看到我的组合框,但是当我单击它时,什么都没有。通常会显示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

我的视图控制器的 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];
}

谢谢你的回答

4

4 回答 4

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使用 maskType使viewController 的视图自由形式UIViewAutoresizingNone
  2. 检查 textField 和 textField 的委托是否已连接

演示项目

于 2013-04-27T10:39:10.360 回答
0

我忘记了细节,但我记得有同样的问题,但对我来说,我需要将它链接到委托或数据源或其他东西?我不是 100% 确定,因为它已经有一段时间了,但请确保当你看到它时,你将它链接到你的选择器参考 + 你需要的其他东西。

于 2013-04-18T16:14:32.657 回答
0

我尝试在我的视图控制器中使用这个组合类,我尝试了你给我的所有解决方案,但没有任何效果,所以解决方案是直接在我的视图控制器中实现所有组合类代码,现在它可以工作,但它有点有点丑……

于 2013-05-03T09:38:10.657 回答
0

您的ComboBox课程未设置为 的委托UITextField,因此textFieldShouldBeginEditing:永远不会被调用。

于 2013-04-26T21:11:20.863 回答