【问题标题】:Uipicker to replace Keyboarduipicker替换键盘
【发布时间】:2013-01-22 06:08:29
【问题描述】:

好的,我正在尝试用 UIpicker 替换键盘。我输入了代码,Uipicker 总是显示,当你点击文本字段时,键盘当然会出现......我什至在里面设置了断点,看看它是否进入了告诉它隐藏键盘,它永远不会......有人会看看它,看看我哪里出错了

 #import "collectionViewViewController.h"
#import "activityCell.h"

@interface collectionViewViewController ()
{
    NSArray *imageArray;
    NSArray *labelArray;
}

@end

@implementation collectionViewViewController
@synthesize pickerViewContainer;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];

    //picker exists in the view, but is outside visible range
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(200, 780, 400, 270)];
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;
    [self.view addSubview:pickerView];



    [[self collectionView]setDataSource:self];
    [[self collectionView]setDelegate:self];

    imageArray = [[NSArray alloc]initWithObjects:
        @"act_blank.png",
        @"act_blank.png",
       @"act_blank.png",
       @"act_blank.png",
       @"act_blank.png",
        @"act_blank.png",
        @"act_1.png",
        @"act_2.png",
        @"act_3.png",
        @"act_4.png",
        @"act_5.png",
        @"act_6.png",
        @"act_7.png",
        @"act_8.png",
        @"act_9.png",
        @"act_10.png",
        @"act_11.png",
        @"act_12.png",
        @"act_13.png",
        @"act_14.png",
        @"act_15.png",
        @"act_16.png",
        @"act_17.png",
        @"act_18.png",nil];

    [[self pickerView]setDataSource:self];
    [[self pickerView]setDelegate:self];


    arrayHour = [[NSMutableArray alloc] init];
    [arrayHour addObject:@"0 h"];
    [arrayHour addObject:@"1 h"];
    [arrayHour addObject:@"2 h"];
    [arrayHour addObject:@"3 h"];
    [arrayHour addObject:@"4 h"];
    [arrayHour addObject:@"5 h"];
    [arrayHour addObject:@"6 h"];
    [arrayHour addObject:@"7 h"];
    [arrayHour addObject:@"8 h"];

    arrayMinute = [[NSMutableArray alloc] init];
    [arrayMinute addObject:@"0 min"];
    [arrayMinute addObject:@"15 min"];
    [arrayMinute addObject:@"30 min"];
    [arrayMinute addObject:@"45 min"];
    pickerViewContainer.hidden = YES;


    //labelArray = [[NSArray alloc] initWithObjects:@"1st",@"2nd", nil];

    // Do any additional setup after loading the view.
}

//animate the picker into view
- (void)textFieldDidEndEditing:(UITextField *)textField {

    [UIView beginAnimations:@"picker" context:nil];
    [UIView setAnimationDuration:0.5];

    _pickerView.transform = CGAffineTransformMakeTranslation(0,-236);
    [UIView commitAnimations];

}

//animate the picker out of view
- (void)textFieldDidBeginEditing:(UITextField *)textField {

    [UIView beginAnimations:@"picker" context:nil];
    [UIView setAnimationDuration:0.5];

    _pickerView.transform = CGAffineTransformMakeTranslation(0,236);
    [UIView commitAnimations];
}

//just hide the keyboard in this example
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectioinView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [imageArray count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier=@"cell";
    activityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [[cell imageView]setImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.item]]];

      UITextField *nameLabel = (UITextField *)[cell viewWithTag:1];

    nameLabel.text = @"HERRO";
    //[[cell textField]setText:[labelArray objectAtIndex:indexPath.item]];

    return cell;




}



- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{

    return 2;

}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == HOUR)
        return [arrayHour count];
    if (component == Minutes)
        return [arrayMinute count];


    return 0;
}

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


    if (component == HOUR)
        return [arrayHour objectAtIndex:row];
    if (component == Minutes)
        return [arrayMinute objectAtIndex:row];


    return 0;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    static NSString *CellIdentifier=@"cell";
//    
//    activityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
//
//    
//    
//    UITextField *nameLabel = (UITextField *)[cell viewWithTag:1];


    //nameLabel.text = [NSString stringWithFormat:@"%@  %@",[arrayHour objectAtIndex:[pickerView selectedRowInComponent:0]], [arrayMinute objectAtIndex:[pickerView    selectedRowInComponent:1]]];


    //textLabel.text = [NSString stringWithFormat:@"%@  %@",[arrayHour objectAtIndex:[pickerView selectedRowInComponent:0]], [arrayMinute objectAtIndex:[pickerView    selectedRowInComponent:1]]];


    //[[cell textField]setText:[labelArray objectAtIndex:indexPath.item]];
    //textField.text = [NSString stringWithFormat:@"%@  %@",[arrayHour objectAtIndex:[pickerView selectedRowInComponent:0]], [arrayMinute objectAtIndex:[pickerView    selectedRowInComponent:1]]];


    return 0;
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setCollectionView:nil];
    [self setPickerView:nil];
    [self setPickerViewContainer:nil];
    [self setTimeText:nil];
    collectionView = nil;
    [super viewDidUnload];
}
- (IBAction)closePicker:(id)sender {
    pickerViewContainer.hidden = YES;
}

- (IBAction)showPicker:(id)sender {
    pickerViewContainer.hidden = NO;
}
@end

谢谢

【问题讨论】:

  • 让用户先点击文本字段然后从UIPickerView 中选择不是一个好的设计模式。如果不用于接收键盘输入,则文本字段是冗余。为什么不使用类似按钮的文本字段来触发选择器视图并将按钮的标题标签设置为用户选择的内容?

标签: xcode replace keyboard uitextfield uipicker


【解决方案1】:

看看这个问题here 和我在下面的回答,看看这是否有帮助。基本上处理这个问题的最好方法是将键盘类型实际设置为 UIPicker:

UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.dataSource = self;
pickerView.delegate = self;
// ... ...
self.pickerTextField.inputView = pickerView;

把它放在你的 viewDidLoad 或某个地方,这样当用户选择 UITextField 时,它实际上会拉出一个 UIPicker。

【讨论】:

  • 一个未被发现的答案宝石!
【解决方案2】:

您没有覆盖正确的委托方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    <code that presentes the picker view>
    return NO;
}

这应该会阻止文本字段显示键盘,而是做任何你想做的事情。

【讨论】:

  • 好吧,我需要做的是'- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(200, 780, 400, 270) ]; pickerView.delegate = self; pickerView.showsSelectionIndicator = YES; [self.view addSubview:pickerView];返回否; }'
  • 当我这样做时,它甚至不会调用它...我在它上面放置了一个断点...它从不作用于代码。不管是bool还是void
  • 您确定将文本字段的委托设置为您的视图控制器吗? textField.delegate = self我在您的代码中的任何地方都看不到它。您可以在 Interface Builder 中设置它,所以我只是仔细检查。
  • 我现在做了,但还是什么都没有,让我添加一个小东西,这可能会产生小的差异,我正在使用一个包含文本字段的集合视图......如果不是什么大问题。 ...但是我确实找到了一种小方法来使 Uipicker 在触摸 TextField 时显示出来,当你右键单击它时,在发送的事件下,它说 Editing Begins,Ctrl 拖动到 UIPicker,它将使选择器显示现在,但键盘也出现了,你只需要手动关闭它..所以如果有一种小方法可以告诉键盘不要出现,我认为这会很好......谢谢
  • 您是否收到有关“从不兼容类型 分配给 'id' 的警告?这意味着您需要将其添加到 .h 文件中的接口与另一个你符合的委托。如果你也这样做,你确定委托已设置,并且你没有收到回调,我不知道发生了什么。
猜你喜欢
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多