gpt4 book ai didi

ios - UIPickerView 没有出现

转载 作者:行者123 更新时间:2023-11-29 11:00:28 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com