2013-02-06 84 views
0

好吧我正在嘗試用UIpicker替換鍵盤。我得到了代碼,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 *[email protected]"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 *[email protected]"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 

感謝

+0

這不是一個好的設計模式,讓用戶先點擊文本字段,然後從'UIPickerView'中選擇。如果不用於接收鍵盤輸入,則文本字段爲_redundant_。爲什麼不使用像按鈕這樣的文本字段來觸發選擇器視圖,並將按鈕的標題標籤設置爲用戶選擇的內容? –

回答

1

你不重寫正確的委託方法:

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

應從顯示鍵盤停止文本字段,而是做你想做的。

+0

好的,我需要做的是' - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField UIPickerView * pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(200,780,400,270)]; pickerView.delegate = self; pickerView.showsSelectionIndicator = YES; [self.view addSubview:pickerView]; return NO; }' – smokingmango

+0

當我這樣做時,它甚至不會調用它......我在它上面放置了一個斷點...並且它從不作用於代碼。無論是bool還是void – smokingmango

+0

您確定文本字段的委託設置爲您的視圖控制器嗎? 'textField.delegate = self' 我在代碼中沒有看到它。你可以在Interface Builder中設置它,所以我只是雙重檢查。 –

6

看這個問題here和我的回答如下,看看是否有所幫助。基本上,來處理這個最好的方法是實際的鍵盤類型設置爲UIPicker:

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

在您的viewDidLoad將這個類的地方,這樣,當用戶選擇的UITextField它實際上拉了UIPicker代替。

+0

一個未被發現的答案寶石! – PassKit

+0

@PassKit哦,謝謝!你也是一顆寶石! :) – Firo