gpt4 book ai didi

iphone - UITextField inputview 属性拉出自定义 UIPickerView 时出现问题

转载 作者:行者123 更新时间:2023-12-03 20:52:15 27 4
gpt4 key购买 nike

我在从文本字段的 inputview 属性中提取自定义 UIPickerView 时遇到问题。我已经为此工作了一段时间,做了一些研究,并尝试根据 Apple 的 UICatalog 示例构建一个单独的项目。

但是,每当我点击文本字段时,我得到的只是弹出的黑屏,而不是选择器 View 。我确信我忽略了一些东西,但我已经找了好几天了,如果有任何帮助,我们将不胜感激。

请记住,这是我的测试项目,看看我是否可以将此功能添加到我的其他应用程序中,并且我尝试从 UICatalog 应用程序复制很多代码。抱歉,如果有任何不明白的地方,请随时提出您可能有的任何问题。谢谢。

//  ViewController.h
// TestPicker

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>{

UIPickerView *picker;
//The picker view the textfield responds to.

IBOutlet UITextField *myText;
//The textfield which has the input view of a picker.

NSArray *testArray;
//The array which values are loaded into the picker.
}

@end


// ViewController.m
// TestPicker


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
picker.delegate=self;
picker.dataSource=self;
myText.delegate=self;

[self createPicker];

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
myText = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
else {
return YES;
}
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
myText.inputView=picker;
return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{

}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *returnStr = @"";

if (pickerView == picker)
{
if (component == 0)
{
returnStr = [testArray objectAtIndex:row];
}
else
{
returnStr = [[NSNumber numberWithInt:row] stringValue];
}
}

return returnStr;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [testArray count];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}


- (void)createPicker
{
testArray = [NSArray arrayWithObjects:
@"John Appleseed", @"Chris Armstrong", @"Serena Auroux",
@"Susan Bean", @"Luis Becerra", @"Kate Bell", @"Alain Briere",
nil];


picker = [[UIPickerView alloc] initWithFrame:CGRectZero];


picker.showsSelectionIndicator = YES; // note this is default to NO

picker.delegate = self;
picker.dataSource = self;

}

@end

最佳答案

问题出在这个方法上:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
myText.inputView=picker;
return YES;
}

您已经在 createPicker 方法中创建了选取器(从 viewDidLoad 调用),因此这会创建另一个实例(此实例不会将委托(delegate)或数据源设置为 self,因为这些是在您创建的第一个实例上设置的)。如果你只删除这个方法的第一行,它应该可以工作。

关于您的代码的另外两条注释。 viewDidLoad 方法中的前两行没有执行任何操作,因为您还没有创建选择器 - 它们应该被删除。您还需要实现 pickerView:didSelectRow:inComponent: 方法,以便将选择器的选定值获取到文本字段中。

关于iphone - UITextField inputview 属性拉出自定义 UIPickerView 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10966366/

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