2013-03-25 26 views
1

我有一個應用程序,它具有使用自定義單元格的UITableView,我爲CustomCell創建了一個新類。每次按某個按鈕時都會創建一個新的單元格,並且每個單元格都有一個UITextField。我想知道在點擊它之後,我應該如何讓單元格中的UITextField辭職第一響應者。請記住,它是在另一個類中初始化的,稱爲CustomCell使UITextField從另一個類別中退出第一響應者

UIViewController代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(dismissKeyboard)]; 

    [self.view addGestureRecognizer:tap]; 
} 
-(void)dismissKeyboard{ 

    } 
(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 


    static NSString *CellIdentifier= @"Cell"; 

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 
    if (indexPath.row%2==0){ 

     n=1; 

     cell.inOut.text = [NSString stringWithFormat:@"Entrada %d:", indexPath.row/2 +1]; 

     cell.entryTime.text = [NSString stringWithFormat:@"%@", [_entryArray objectAtIndex:(indexPath.row+2)]]; 


     if (indexPath.row==0){ 
      [email protected]""; 
     } 
     else { 
      cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row]; 
     } 
    } 
    else{ 
     cell.inOut.text = [NSString stringWithFormat:@"Saída %d:", (indexPath.row+1)/2]; 
     cell.entryTime.text = [NSString stringWithFormat:@"%@",[_entryArray objectAtIndex:(indexPath.row+2)]]; 

     cell.delta.text = [_timeSinceLastEntryArray objectAtIndex:indexPath.row]; 
     if (whichButton==YES){} 
     else{ 
      } 
     n=2; 

    } 
    if([indexPath row] % 2) 
     [cell setBackgroundColor:[UIColor whiteColor]]; 
    else 
     [cell setBackgroundColor:[UIColor lightGrayColor]]; 



    return cell; 
} 

CustomCell.h代碼:

@interface CustomCell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UITextField *inOut; 

@property (strong, nonatomic) IBOutlet UILabel *entryTime; 
@property (strong, nonatomic) IBOutlet UILabel *delta; 

- (void) dismissInOutKeyboard; 

@end 

CustomCell.m代碼:

@implementation CustomCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     _inOut = [[UITextField alloc]init]; 




     _entryTime = [[UILabel alloc]init]; 

     _delta = [[UILabel alloc]init]; 

     [self.contentView addSubview:_entryTime]; 

     [self.contentView addSubview:_inOut]; 

     [self.contentView addSubview:_delta]; 

    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@synthesize inOut=_inOut, entryTime=_entryTime, delta=_delta; 
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { 

    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) { 

     // Initialization code 

     _inOut = [[UITextField alloc]init]; 

     _inOut.textAlignment = UITextAlignmentLeft; 


     _entryTime = [[UILabel alloc]init]; 

     _entryTime.textAlignment = UITextAlignmentLeft; 


     _delta = [[UILabel alloc]init]; 

     _delta.textAlignment = UITextAlignmentLeft; 

     [self.contentView addSubview:_entryTime]; 

     [self.contentView addSubview:_inOut]; 

     [self.contentView addSubview:_delta]; 

    } 

    return self; 

} 



@end 

回答

4

直接讓findAndResignFirstResponder的最頂端查看您擁有,即UIViewController.view

聲明:

@interface UIView (findAndResignFirstResponder) 
- (void)findAndResignFirstResponder; 
- (UIView *)findFirstResponder; 
@end 

定義:

@implementation UIView (findAndResignFirstResponder) 
- (void)findAndResignFirstResponder { 
    [self.findFirstResponder resignFirstResponder]; 
} 

- (UIView *)findFirstResponder { 
    for (UIView *subView in self.subviews) { 
     if (subView.isFirstResponder) 
      return subView; 
     UIView* firstResponder = subView.findFirstResponder; 
     if(firstResponder) 
      return firstResponder; 
    } 
    return nil; 
} 
+0

您好,感謝的解。我有這個在另一個應用程序的工作,但我似乎無法在這一個工作。我應該在哪裏編寫這段代碼?謝謝! – dietbacon 2013-04-09 01:56:57

+0

btw,我在故事板中添加了我的'UITextField' – dietbacon 2013-04-09 02:03:47

3

一種原油,但工作實現,我用的是創建一個新的UITextField,將其設置爲第一響應者,然後立即刪除它。這樣做(在視圖中)的基本方法是做這樣的事情:

UITextField *cell = [UITextField new]; 
[self addSubview:cell]; 

[cell becomeFirstResponder]; 
[cell resignFirstResponder]; 

[cell removeFromSuperview]; 
cell = nil; 

這將創建一個新的細胞,將其添加到視圖,以便可以選擇,有沒有采取的焦點鍵盤遠離當前選定的單元格,隱藏鍵盤,然後刪除新的單元格。

編輯:如果你將要設置文本框另一個是第一個響應後,這個代碼是完全不必要的。然後,你只需要調用becomeFirstResponder在新的文本框,以及鍵盤焦點會自動切換。

+0

TY非常:) – dietbacon 2013-03-25 03:37:07

相關問題