在所有堆栈溢出中对此问题毫无疑问是奇怪的,但我找不到它:(
让我解释一下。我有一个简单的UITextField
应该提供自定义inputAccessoryView
。
预期的行为是:
UITextField
中
inputField
)如果我按下取消,键盘应该消失并让主文本域取消替换
非常类似Apple的消息应用程序
所有作品都很好地解决了两件事:
让我们展示一些代码 .h
#import <UIKit/UIKit.h>
@interface ACTextField : UITextField
@end
.m
#import "ACTextField.h"
@interface ACTextField () <UITextFieldDelegate>
@property (nonatomic, retain) UITextField *inputField;
@property (nonatomic, assign) BOOL keyboardOpen; // to check if keyboard is open
@property (nonatomic, retain) NSTimer *timer; //added after an answer
@end
@implementation ACTextField
// method called when timer fire
- (void) fireTimer:(NSTimer *) timer
{
[self.timer invalidate];
self.timer = nil; // this line thoesen't change anyting so can be deleted
[self.inputField becomeFirstResponder];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self awakeFromNib];
}
return self;
}
-(void)awakeFromNib
{
self.delegate = self;
self.keyboardOpen = NO;
}
- (void)cancelPressed:(id)sender
{
[self.inputField resignFirstResponder];
[self resignFirstResponder];
self.keyboardOpen = NO;
}
- (void)dealloc
{
NSLog(@"dealloc ACTextField %@", self);
self.inputField = nil;
self.timer = nil;
}
- (void) designAccessoryView
{
if (self.inputAccessoryView == nil) {
CGRect frame = [[UIScreen mainScreen] bounds];
self.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 44)];
self.inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
int buttonWidth = 65;
int padding = 5;
UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
cancel.frame = CGRectMake(padding, 0, buttonWidth, 44);
[cancel setTitle:@"cancel" forState:UIControlStateNormal];
[cancel setTitle:@"cancel" forState:UIControlStateHighlighted];
[cancel addTarget:self action:@selector(cancelPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.inputAccessoryView addSubview:cancel];
self.inputField = [[UITextField alloc] initWithFrame:CGRectMake(buttonWidth + 2*padding, 2, frame.size.width - buttonWidth - 3*padding, 40)];
self.inputField.borderStyle = UITextBorderStyleRoundedRect;
self.inputField.delegate = self;
[self.inputAccessoryView addSubview:self.inputField];
self.inputAccessoryView.backgroundColor = [UIColor lightGrayColor];
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (!self.keyboardOpen && textField == self)
{
self.inputField.text = self.text;
[self designAccessoryView];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self // modified to use the instance method fireTimer
selector:@selector(fireTimer:)
userInfo:nil
repeats:NO];
self.keyboardOpen = YES;
}
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
self.text = self.inputField.text;
[self cancelPressed:self];
return YES;
}
@end
非常感谢任何帮助
编辑:我忘了指定我在IOS 7.0.4的iPhone 4s上进行测试 EDIT2:我尝试使用UITextField
Wrapper和几乎相同的代码,一切顺利。行为是预期的,键盘不再冻结,内存保持低位。好的,但我需要一个UITextField
子类,而不是一个包装器:(