UITextField inputview 弹出 UIPickerView 在pickerview中显示问号

Posted

技术标签:

【中文标题】UITextField inputview 弹出 UIPickerView 在pickerview中显示问号【英文标题】:UITextField inputview to pop up UIPickerView displaying question mark in pickerview 【发布时间】:2012-03-09 23:24:52 【问题描述】:

这是我的 .h 文件

当我运行此代码并选择一个文本字段时,会弹出一个带有问号的pickerview 但是当我选择问号时,我会在文本字段中得到正确的值

#import <UIKit/UIKit.h>

#import "ScrollableViewController.h"
#import "MIBackgroundTapDelegate.h"


//@interface Activapc1 : UIViewController 
@interface Activapc1 : ScrollableViewController <MIBackgroundTapDelegate, UIPickerViewDelegate, UIPickerViewDataSource>   

UITextField *amplitude1;
UITextField *rate1;
UITextField *pulse_width1;
UITextField *impedance1;

IBOutlet UITextField *configuration;
NSArray *mode;

@property (nonatomic, retain) IBOutlet UITextField *amplitude1;
@property (nonatomic, retain) IBOutlet UITextField *rate1;
@property (nonatomic, retain) IBOutlet UITextField *pulse_width1;
@property (nonatomic, retain) IBOutlet UITextField *impedance1;

-(IBAction)next;
-(IBAction)skip;
-(IBAction)home;
-(IBAction)select;

-(IBAction)textFieldDoneEditing:(id) sender;

@end

这是.m文件

我不明白为什么我得到问号请帮助我

#import "Activapc1.h"
#import "dbsViewController.h"
#import "Activapc2.h"
#import "APCoption.h"



@implementation Activapc1

@synthesize amplitude1;
@synthesize rate1;
@synthesize pulse_width1;
@synthesize impedance1;

const int MyModePicker = 3002;

-(void)viewDidLoad 
self.svScrollViewM.contentSize = CGSizeMake(320, 416);
[self registerForEditingEvents:amplitude1];
[self registerForEditingEvents:rate1];
[self registerForEditingEvents:pulse_width1];
[self registerForEditingEvents:impedance1];

UIPickerView *modePicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
modePicker.tag = MyModePicker;
modePicker.delegate = self;
modePicker.dataSource = self;
[modePicker setShowsSelectionIndicator:YES];
configuration.inputView = modePicker;
[modePicker release];

[super viewDidLoad];

    //Prepare data for pickers
    mode = [NSArray arrayWithObjects:@"Voltage",@"Current",nil];



[mode retain];

//mode = [[NSMutableArray alloc] init];
//[mode addObject]


//********picker view code**************

#pragma mark -

#pragma mark UIPickerViewDelegate

-(NSString *)pickerView:(UIPickerView *)pickerView titileForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

    if(pickerView.tag == MyModePicker)
    
        return [mode objectAtIndex:row];
    
    return @"Unknown title";



-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

        if(pickerView.tag == MyModePicker)
        
            configuration.text = (NSString *)[mode objectAtIndex:row];
        


#pragma mark -

#pragma mark UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

    return 1;


- (NSInteger)pickerView:(UIPickerView *)pickerView    numberOfRowsInComponent:(NSInteger)component

    if(pickerView.tag == MyModePicker)
    
        return mode.count;

return 1;


#pragma mark -

//**********picker view code end************

-(IBAction) backgroundTap:(id) sender
[self.amplitude1 resignFirstResponder];
[self.rate1 resignFirstResponder];
[self.pulse_width1 resignFirstResponder];
[self.impedance1 resignFirstResponder];


-(IBAction) textFieldDoneEditing:(id)sender
[sender resignFirstResponder];


-(IBAction)home 
dbsViewController *dbs = [[dbsViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:dbs animated:YES];


-(IBAction)skip 
Activapc2 *activapc2 = [[Activapc2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:activapc2 animated:YES];


-(IBAction)next 
Activapc2 *activapc2 = [[Activapc2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:activapc2 animated:YES];


-(IBAction)select 
APCoption *apcoption = [[APCoption alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:apcoption animated:YES];





- (void)didReceiveMemoryWarning 
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use.


- (void)viewDidUnload 
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;



- (void)dealloc 
[mode release];
[super dealloc];




@end

【问题讨论】:

【参考方案1】:

pickerview 需要标题而不是标题。此外,返回字符串的委托方法没有reusingView: 参数。由于您没有正确实现委托方法,因此选择器只会显示问号。

-(NSString *)pickerView:(UIPickerView *)pickerView titileForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

我认为应该是:

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

【讨论】:

感谢它现在可以工作了,我还在 .m 文件中删除了这个 reusingView:(UIView *)view 语句,之后它就可以工作了 @bigbang 很高兴为您提供帮助。很高兴收到评论说解决方案有效。但你也应该accept an answer for each of your questions that solved the issue for you。

以上是关于UITextField inputview 弹出 UIPickerView 在pickerview中显示问号的主要内容,如果未能解决你的问题,请参考以下文章

Swift:使用 UIDatePicker 作为 UITextField inputView 不更新

iOS:自定义 UITextField 的 inputView 大小

UIPickerView 作为多个 UITextField 的 inputView

UITableView 作为 UITextField didSelectRowAtIndexPath 的 inputView 随机未触发

UITextField 自定义 InputView - 如何让 textFieldShouldReturn 触发?

UITableView 中的 UITextField 与 DatePicker 的 InputView