在我的
为了在用户仍在编辑
现在,我的应用程序只有在尝试隐藏键盘1次中出现5次崩溃。在这里,我可能了解到了原因: 当单元格移出屏幕时,它被销毁/回收,以便在需要时可以重新排队。这意味着,一旦我的单元格和其中包含的文本字段移出屏幕,将
我的问题是,如何在所有情况下(表视图消失、推送新的视图控制器、单元格/文本字段移出屏幕等)正确地隐藏
点击带有
这是否是由于在单元格被点击时手动打开键盘引起的?(使用
UITableView
中,我有一组包含UITextField
的自定义单元格。我发现(通过痛苦的方式),显然离开当前视图(无论是通过推送新的视图控制器还是关闭活动视图('返回'))会使我的应用程序崩溃,当键盘仍然可见时。为了在用户仍在编辑
UITextField
,但视图已更改时隐藏键盘,我在推送新视图控制器之前添加了 [self.view endEditing:YES];
,并在viewWillDisappear
方法中也添加了该行代码。现在,我的应用程序只有在尝试隐藏键盘1次中出现5次崩溃。在这里,我可能了解到了原因: 当单元格移出屏幕时,它被销毁/回收,以便在需要时可以重新排队。这意味着,一旦我的单元格和其中包含的文本字段移出屏幕,将
resignFirstResponder
消息发送给它(手动或通过[self.view endEditing:YES];
),应用程序将崩溃。这是backtrace:#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
中的键盘?任何帮助都将不胜感激,我只是无法摆脱崩溃!以下是一些额外的代码: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
自定义单元格的实现:
#import "TextInputCell.h"
@implementation TextInputCell
@synthesize mainText;
@synthesize textField;
@synthesize numberFormatter;
@synthesize type;
@synthesize delegate;
- (void) initDelegateWithType:(int)aType {
self.type = aType;
textField.delegate = self;
}
- (BOOL)textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; {
if (![string length] || type == tpText || (isnumber([string characterAtIndex:0]) && type == tpNumber))
{
[self save:[textField.text stringByReplacingCharactersInRange:range withString:string]];
return YES;
}
char c = [string characterAtIndex:0];
BOOL isSep = [string isEqualToString:[numberFormatter decimalSeparator]];
BOOL isMinus = [string isEqualToString:@"-"];
NSRange sep;
sep.location = NSNotFound;
sep.length = 0;
if ([aTextField.text length])
sep = [aTextField.text rangeOfString:[numberFormatter decimalSeparator]];
if (isMinus)
{
// allow '-' only if type is tpNumber and field is empty
if (type != tpNumber)
return NO;
if ([aTextField.text length])
return NO;
[self save:[textField.text stringByReplacingCharactersInRange:range withString:string]];
return YES;
}
if (isnumber(c) || ((type == tpDecimal || type == tpNumber) && isSep && sep.location == NSNotFound))
{
// allow separator for decimal and number, but only if not in text already
if (!isSep && sep.location != NSNotFound && type == tpDecimal)
{
// round after , (only for decimal type)
NSString* text = [NSString stringWithFormat:@"%@%@", aTextField.text, string];
double num = [[numberFormatter numberFromString:text] doubleValue];
double res = ((int)(num / 0.5)) * 0.5;
aTextField.text = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:res]];
[self save:aTextField.text];
return NO;
}
[self save:[NSString stringWithFormat:@"%@%@", aTextField.text, string]];
return YES;
}
[self save:[NSString stringWithFormat:@"%@%@", aTextField.text, string]];
return NO;
}
- (void) save:(NSString*)text {
UITableView* view = (UITableView*)[self superview];
NSIndexPath* index = [view indexPathForCell:self];
if (delegate)
[delegate setNewText:text forIndex:index];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)field
{
NSLog(@"should end");
// if ([field becomeFirstResponder])
// [field resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)field {
if (type != tpText && [field.text length] == 0)
field.text = @"0";
NSLog(@"should return");
// if ([field becomeFirstResponder])
// [field resignFirstResponder];
return YES;
}
- (void) startEditing {
NSLog(@"should return");
[self.textField becomeFirstResponder];
}
@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;
}
}
点击带有
UITextField
的单元格,将单元格滑动到屏幕外(不隐藏键盘),然后仅仅关闭tableview(例如通过导航控制器返回),即可重现崩溃。这是否是由于在单元格被点击时手动打开键盘引起的?(使用
startEditing
方法)我这样做是为了让用户不必点击文本字段,但编辑也会在他点击单元格文本标签时开始。