在 push 的ViewController调用中,我尝试以编程方式显示ComboBox。这个组合框实现UIPickerView委托协议并添加.xib文件。
当我运行应用程序时,我可以在屏幕上看到我的组合框,但是当我点击它时,没有任何附加内容。通常会显示选择器视图。
我不明白,是在另一个viewcontroller调用模态它工作正常
//
// 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];
}
你的答案
答案 0 :(得分: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];
}
检查的步骤很少
ComboBox
制作UIViewAutoresizingNone
viewController自由形式的视图。答案 1 :(得分:0)
我忘记了细节,但我记得有同样的问题,但对我而言,我需要将它链接到委托或数据源或其他东西?我不是百分之百确定,因为它已经有一段时间了,但请确保当你在视图中将它链接到你的选择器参考+你需要的其他东西时。
答案 2 :(得分:0)
您的ComboBox
课程未设置为UITextField
的代理人,因此永远不会调用textFieldShouldBeginEditing:
。
答案 3 :(得分:0)
我尝试在我的viewcontroller中使用这个组合类,我尝试了你给我的所有解决方案,但没有任何工作,所以解决方案是直接在我的viewcontroller中实现所有的组合类代码,现在它可以工作,但是这有点过时了......