Iphone uitableview中辞职的FirstResponder崩溃

Iphone uitableview中辞职的FirstResponder崩溃,iphone,objective-c,uitableview,uitextfield,Iphone,Objective C,Uitableview,Uitextfield,在我的UITableView中,我有一组自定义单元格,其中包含UITextField。我发现(困难的是),当键盘仍然可见时,显然离开当前视图(通过按下新的视图控制器或关闭活动视图(“返回”)会使我的应用程序崩溃 要在用户仍在编辑UITextField,但视图已更改时隐藏键盘,我添加了[self.view endEditing:YES]在我按下新的视图控制器之前,以及在viewwilldiscover方法中 现在,我的应用程序在5次尝试隐藏键盘时只有1次崩溃。我可能知道了原因: 当单元格移到屏幕外

在我的
UITableView
中,我有一组自定义单元格,其中包含
UITextField
。我发现(困难的是),当键盘仍然可见时,显然离开当前视图(通过按下新的视图控制器或关闭活动视图(“返回”)会使我的应用程序崩溃

要在用户仍在编辑
UITextField
,但视图已更改时隐藏键盘,我添加了
[self.view endEditing:YES]在我按下新的视图控制器之前,以及在
viewwilldiscover
方法中

现在,我的应用程序在5次尝试隐藏键盘时只有1次崩溃。我可能知道了原因: 当单元格移到屏幕外时,它会被销毁/回收,因此可以在需要时再次退出队列。这意味着,一旦我的手机和包含的文本字段移出屏幕,向其发送
辞职FirstResponder
消息(手动或通过
[self.view endEditing:YES];
,应用程序将崩溃。这是回溯:

#0  0x012e309b in objc_msgSend ()
#1  0x05956888 in dyld_stub_usleep ()
#2  0x003ff056 in -[UIResponder resignFirstResponder] ()
#3  0x003c697f in -[UITextField resignFirstResponder] ()
#4  0x003c6ab1 in -[UIView(UITextField) endEditing:] ()
#5  0x00012c0a in -[MyController viewWillDisappear:] (self=0x7419e90, _cmd=0x52aa27e, animated=1 '\001') at MyController:121
#6  0x003ee9a2 in -[UINavigationController _startTransition:fromViewController:toViewController:] ()
#7  0x003e932a in -[UINavigationController _startDeferredTransitionIfNeeded] ()
#8  0x003e8fb6 in -[UINavigationController _popViewControllerWithTransition:allowPoppingLast:] ()
#9  0x003e9142 in -[UINavigationController popViewControllerAnimated:] ()
#10 0x003e857a in -[UINavigationController navigationBar:shouldPopItem:] ()
#11 0x00389260 in -[UINavigationBar _popNavigationItemWithTransition:] ()
#12 0x0039261b in -[UINavigationBar _handleMouseUpAtPoint:] ()
#13 0x00354ded in -[UIWindow _sendTouchesForEvent:] ()
#14 0x00335c37 in -[UIApplication sendEvent:] ()
#15 0x0033af2e in _UIApplicationHandleEvent ()
#16 0x01ad5992 in PurpleEventCallback ()
#17 0x0115e944 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#18 0x010becf7 in __CFRunLoopDoSource1 ()
#19 0x010bbf83 in __CFRunLoopRun ()
#20 0x010bb840 in CFRunLoopRunSpecific ()
#21 0x010bb761 in CFRunLoopRunInMode ()
#22 0x01ad41c4 in GSEventRunModal ()
#23 0x01ad4289 in GSEventRun ()
#24 0x0033ec93 in UIApplicationMain ()
#25 0x00001e18 in main (argc=1, argv=0xbffff0a0) at main.m:24
我现在的问题是,在所有情况下,如何正确地将
UITextField
的键盘隐藏在我的
UITableViewCell
中?(表视图消失,新视图控制器按下,单元格/textfield移出屏幕,等等)

非常感谢您的帮助,我只是无法摆脱崩溃

我将包含更多代码:

1) 自定义单元类:

#import <UIKit/UIKit.h>

enum Type
{
   tpText = 0,
   tpInteger,
   tpDecimal,
   tpNumber
};

@protocol TextInputCellDelegate <NSObject>
@required
- (void)setNewText:(NSString*)newText forIndex:(NSIndexPath*)index;
@end

@interface TextInputCell : UITableViewCell <UITextFieldDelegate> {
   IBOutlet UILabel* mainText;
   IBOutlet UITextField* textField;
   NSNumberFormatter* numberFormatter;
   int type;
   id <TextInputCellDelegate> delegate;
}

- (void) initDelegateWithType:(int)aType;
- (void) save:(NSString*)text;
- (void) startEditing;

@property (nonatomic, retain) UILabel* mainText;
@property (nonatomic, retain) UITextField* textField;
@property (nonatomic, retain) NSNumberFormatter* numberFormatter;
@property (nonatomic, assign) int type;
@property (nonatomic, assign) id <TextInputCellDelegate> delegate;

@end
包含其中4个单元格的视图控制器:

@implementation MailPrefController

@synthesize menuItems;
@synthesize mailTo;
@synthesize mailCc;
@synthesize mailBcc;
@synthesize mailSubject;
@synthesize mailBody;


#pragma mark -
#pragma mark Initialization


- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
    }
    return self;
}


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

   // save button

   UIBarButtonItem* saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveMailPrefs)];
   self.navigationItem.rightBarButtonItem = saveButton;
   [saveButton release];

   self.navigationItem.title = NSLocalizedString(@"Mail template", nil);

   // menu items

   self.menuItems = [[NSArray alloc] initWithObjects:NSLocalizedString(@"To:", nil), NSLocalizedString(@"Cc:", nil), NSLocalizedString(@"Bcc:", nil), NSLocalizedString(@"Subject:", nil), NSLocalizedString(@"Body:", nil), nil];
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [menuItems count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   if (indexPath.row < 4)
   {
      // title

      TextInputCell* textCell = (TextInputCell*)[tableView dequeueReusableCellWithIdentifier:@"TextInputCell"];

      if (textCell == nil)
      {
         NSArray* nibContents = [[NSBundle mainBundle] loadNibNamed:@"TextInputCell" owner:self options:nil];
         textCell = [nibContents lastObject]; 
         textCell.selectionStyle = UITableViewCellSelectionStyleNone;
         textCell.textField.textColor = [Service uiColor];
         textCell.delegate = self;
         [textCell initDelegateWithType:tpText];
      }

      textCell.mainText.text = [menuItems objectAtIndex:indexPath.row];

      switch (indexPath.row)
      {
         case 0: textCell.textField.text = self.mailTo; break;
         case 1: textCell.textField.text = self.mailCc; break;
         case 2: textCell.textField.text = self.mailBcc; break;
         case 3: textCell.textField.text = self.mailSubject; break;

         default: break;
      }

      return textCell;
   }

   // body text multiline

   TextInputMultilineCell* textMultilineCell = (TextInputMultilineCell*)[tableView dequeueReusableCellWithIdentifier:@"TextInputMultilineCell"];

   if (textMultilineCell == nil)
   {
      NSArray* nibContents = [[NSBundle mainBundle] loadNibNamed:@"TextInputMultilineCell" owner:self options:nil];
      textMultilineCell = [nibContents lastObject]; 
      textMultilineCell.selectionStyle = UITableViewCellSelectionStyleNone;
      textMultilineCell.textView.font = [UIFont systemFontOfSize:12];
      textMultilineCell.textView.textColor = [Service uiColor];
      textMultilineCell.delegate = self;
      [textMultilineCell initDelegate];

      CGRect rect = textMultilineCell.textView.frame;
      rect.size.height *= 2;
      textMultilineCell.textView.frame = rect;
   }

   textMultilineCell.mainText.text = [menuItems objectAtIndex:indexPath.row];
   textMultilineCell.textView.text = self.mailBody;

   return textMultilineCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   switch (indexPath.section)
   {
      case 0:
      {
         // general section

         if (indexPath.row < 4)
         {
            TextInputCell* cell = (TextInputCell*)[[self tableView] cellForRowAtIndexPath:indexPath];            
            [cell startEditing];
         }
      }

      default: break;
   }
}
@implementation-MailPrefController
@合成单体;
@合成mailTo;
@合成mailCc;
@合成bcc;
@综合学科;
@合成邮件体;
#布拉格标记-
#杂注标记初始化
-(id)initWithStyle:(UITableViewStyle)样式{
//Override initWithStyle:如果以编程方式创建控制器并希望执行不适合viewDidLoad的自定义。
if((self=[super initWithStyle:UITableViewStyleGrouped])){
}
回归自我;
}
#布拉格标记-
#pragma标记视图生命周期
-(无效)viewDidLoad{
[超级视图下载];
//保存按钮
UIBarButtonItem*saveButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave目标:自我操作:@selector(saveMailPrefs)];
self.navigationItem.rightBarButtonItem=saveButton;
[保存按钮释放];
self.navigationItem.title=NSLocalizedString(@“邮件模板”,无);
//菜单项
self.menuItems=[[NSArray alloc]initWithObjects:NSLocalizedString(@“To:”,nil),NSLocalizedString(@“Cc:”,nil),NSLocalizedString(@“Bcc:”,nil),NSLocalizedString(@“Subject:”,nil),NSLocalizedString(@“Body:”,nil),nil];
}
-(无效)视图将消失:(BOOL)已设置动画{
[超级视图将消失:动画];
}
#布拉格标记-
#pragma标记表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//返回节中的行数。
返回[菜单项计数];
}
//自定义表格视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.row<4)
{
//头衔
TextInputCell*textCell=(TextInputCell*)[tableView dequeueReusableCellWithIdentifier:@“TextInputCell”];
if(textCell==nil)
{
NSArray*nibContents=[[NSBundle mainBundle]loadNibNamed:@“TextInputCell”所有者:自选项:nil];
textCell=[nibContents lastObject];
textCell.selectionStyle=UITableViewCellSelectionStyleNone;
textCell.textField.textColor=[Service uiColor];
textCell.delegate=self;
[textCell initDelegateWithType:tpText];
}
textCell.mainText.text=[menuItems objectAtIndex:indexath.row];
开关(indexPath.row)
{
案例0:textCell.textField.text=self.mailTo;中断;
案例1:textCell.textField.text=self.mailCc;中断;
案例2:textCell.textField.text=self.mailBcc;中断;
案例3:textCell.textField.text=self.mailSubject;中断;
默认:中断;
}
返回文本单元格;
}
//正文文本多行
textInputMultileCell*textMultileCell=(textInputMultileCell*)[tableView dequeueReusableCellWithIdentifier:@“textInputMultileCell”];
如果(textMultileCell==nil)
{
NSArray*nibContents=[[NSBundle mainBundle]loadNibNamed:@“textinputmultilecell”所有者:自选项:nil];
textMultileCell=[nibContents lastObject];
textMultileCell.selectionStyle=UITableViewCellSelectionStyleNone;
textMultileCell.textView.font=[UIFont systemFontOfSize:12];
textMultilecell.textView.textColor=[Service uiColor];
textMultileCell.delegate=self;
[textMultilecell initDelegate];
CGRect rect=textMultileCell.textView.frame;
矩形尺寸高度*=2;
textMultileCell.textView.frame=rect;
}
textMultileCell.mainText.text=[menuItems objectAtIndex:indexath.row];
textMultileCell.textView.text=self.mailBody;
返回文本多个ecell;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
开关(indexPath.section)
{
案例0:
{
//总务科
if(indexPath.row<4)
{
TextInputCell*单元格=(TextInputCell*)[[self tableView]cellForRowAtIndexPath:indexPath];
[细胞启动试验];
}
}
默认:中断;
}
}
通过使用
UITextField
单击其中一个单元格,将单元格滑出屏幕(不隐藏键盘),然后关闭tableview(例如,通过导航控制器返回),可以再现崩溃

这可能是由于在单击单元格时手动打开键盘造成的吗?(@implementation MailPrefController @synthesize menuItems; @synthesize mailTo; @synthesize mailCc; @synthesize mailBcc; @synthesize mailSubject; @synthesize mailBody; #pragma mark - #pragma mark Initialization - (id)initWithStyle:(UITableViewStyle)style { // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. if ((self = [super initWithStyle:UITableViewStyleGrouped])) { } return self; } #pragma mark - #pragma mark View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // save button UIBarButtonItem* saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveMailPrefs)]; self.navigationItem.rightBarButtonItem = saveButton; [saveButton release]; self.navigationItem.title = NSLocalizedString(@"Mail template", nil); // menu items self.menuItems = [[NSArray alloc] initWithObjects:NSLocalizedString(@"To:", nil), NSLocalizedString(@"Cc:", nil), NSLocalizedString(@"Bcc:", nil), NSLocalizedString(@"Subject:", nil), NSLocalizedString(@"Body:", nil), nil]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } #pragma mark - #pragma mark Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [menuItems count]; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row < 4) { // title TextInputCell* textCell = (TextInputCell*)[tableView dequeueReusableCellWithIdentifier:@"TextInputCell"]; if (textCell == nil) { NSArray* nibContents = [[NSBundle mainBundle] loadNibNamed:@"TextInputCell" owner:self options:nil]; textCell = [nibContents lastObject]; textCell.selectionStyle = UITableViewCellSelectionStyleNone; textCell.textField.textColor = [Service uiColor]; textCell.delegate = self; [textCell initDelegateWithType:tpText]; } textCell.mainText.text = [menuItems objectAtIndex:indexPath.row]; switch (indexPath.row) { case 0: textCell.textField.text = self.mailTo; break; case 1: textCell.textField.text = self.mailCc; break; case 2: textCell.textField.text = self.mailBcc; break; case 3: textCell.textField.text = self.mailSubject; break; default: break; } return textCell; } // body text multiline TextInputMultilineCell* textMultilineCell = (TextInputMultilineCell*)[tableView dequeueReusableCellWithIdentifier:@"TextInputMultilineCell"]; if (textMultilineCell == nil) { NSArray* nibContents = [[NSBundle mainBundle] loadNibNamed:@"TextInputMultilineCell" owner:self options:nil]; textMultilineCell = [nibContents lastObject]; textMultilineCell.selectionStyle = UITableViewCellSelectionStyleNone; textMultilineCell.textView.font = [UIFont systemFontOfSize:12]; textMultilineCell.textView.textColor = [Service uiColor]; textMultilineCell.delegate = self; [textMultilineCell initDelegate]; CGRect rect = textMultilineCell.textView.frame; rect.size.height *= 2; textMultilineCell.textView.frame = rect; } textMultilineCell.mainText.text = [menuItems objectAtIndex:indexPath.row]; textMultilineCell.textView.text = self.mailBody; return textMultilineCell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 0: { // general section if (indexPath.row < 4) { TextInputCell* cell = (TextInputCell*)[[self tableView] cellForRowAtIndexPath:indexPath]; [cell startEditing]; } } default: break; } }
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    currentTextField = textField;
}
-(BOOL) textFieldShouldReturn:(UITextField *) textField {  
    if (textfield && [textField isKindOfClass:[UITextField class]] &&  [textField retainCount] > 0 && [textField isFirstResponder])
    {   
        [textField resignFirstResponder];
    }  
    else
    {
        // how to hide the keyboard?
        // since uitextfield is bad the containing cell (self) may be bad, so cant  
        [self.view endEditing:YES];
        // any suggestions?
    }

    return YES; 
 }