即使 UIResponder 对象放弃其第一响应者状态,键盘也不会关闭
我设法自定义 UIAlertView,将两个 UITextFields 作为子视图放入其中,但这样做时键盘不知何故损坏了。
当我试图解雇它并辞去第一响应者时,它什么也没做,只是停留在那里。
这是我的代码,好吧,只有相关部分(这是我放入 UIAlertView 子类中的内容):
#pragma mark -
#pragma mark UIView
- (void)layoutSubviews
{
[super layoutSubviews];
NSArray *subviews = [self subviews];
UIView *backgroundView = [subviews objectAtIndex:0];
CGRect firstFrame = [[subviews objectAtIndex:2] frame];
CGRect backgroundFrame = [backgroundView frame];
CGFloat displacement = kTextHeight * 2 + kPadding * 3;
CGFloat pivot = firstFrame.origin.y + firstFrame.size.height;
CGRect nameFrame = CGRectMake(firstFrame.origin.x,
pivot + kPadding,
kTextWidth, kTextHeight);
CGRect descriptionFrame = CGRectMake(firstFrame.origin.x,
pivot + kTextHeight + kPadding * 2,
kTextWidth, kTextHeight);
if (_nameField == nil && _descriptionField == nil) {
// first UITextField
_nameField = [[UITextField alloc] initWithFrame:CGRectZero];
[_nameField setPlaceholder:kNamePlaceholder];
[_nameField setBorderStyle:UITextBorderStyleRoundedRect];
[_nameField setDelegate:self];
// second UITextField
_descriptionField =
[[UITextField alloc] initWithFrame:CGRectZero];
[_descriptionField setPlaceholder:kDescriptionPlaceholder];
[_descriptionField setBorderStyle:UITextBorderStyleRoundedRect];
[_descriptionField setDelegate:self];
[self addSubview:_nameField];
[self addSubview:_descriptionField];
}
// set the new frames to each text field
[_nameField setFrame:nameFrame];
[_descriptionField setFrame:descriptionFrame];
// increment background image-view height by "displacement"
backgroundFrame.size.height += displacement;
[backgroundView setFrame:backgroundFrame];
// displace by "diplacement" every subview positioned after "pivot"
for (UIView *view in subviews) {
CGRect viewRect = [view frame];
if (viewRect.origin.y > pivot)
[view setFrame:CGRectOffset(viewRect, 0., displacement)];
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return YES;
}
#pragma mark -
#pragma mark <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}
这是我的一大块代码:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
static CGFloat kTextWidth = 260.;
static CGFloat kTextHeight = 25.;
static CGFloat kPadding = 10.;
static NSString *kNamePlaceHolder = @"Name";
static NSString *kDescriptionPlaceHolder = @"Description";
// interfaces
@interface AppDelegate: NSObject <UIApplicationDelegate>
{
UIWindow *_window;
}
- (void)showAlertView;
@end
@interface AlertView: UIAlertView <UITextFieldDelegate>
{
UITextField *_nameField;
UITextField *_descriptionField;
}
@end
// implementations
@implementation AppDelegate
#pragma mark -
#pragma mark NSObject
- (void)dealloc
{
[_window release];
[super dealloc];
}
#pragma mark -
#pragma mark AppDelegate
- (void)showAlertView
{
AlertView *alertView = [[[AlertView alloc] initWithTitle:@"Title"
message:@"Body" delegate:nil
cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
[alertView show];
}
#pragma mark -
#pragma mark <UIApplicationDelegate>
- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)options
{
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[_window setBackgroundColor:[UIColor whiteColor]];
[_window makeKeyAndVisible];
[NSTimer scheduledTimerWithTimeInterval:3. target:self
selector:@selector(showAlertView) userInfo:nil repeats:NO];
return YES;
}
@end
@implementation AlertView
#pragma mark -
#pragma mark NSObject
- (void)dealloc
{
[_nameField setDelegate:nil];
[_descriptionField setDelegate:nil];
[_nameField release];
[_descriptionField release];
[super dealloc];
}
#pragma mark -
#pragma mark UIView
- (void)layoutSubviews
{
[super layoutSubviews];
NSArray *subviews = [self subviews];
UIView *backgroundView = [subviews objectAtIndex:0];
CGRect firstFrame = [[subviews objectAtIndex:2] frame];
CGRect backgroundFrame = [backgroundView frame];
CGFloat displacement = kTextHeight * 2 + kPadding * 3;
CGFloat pivot = firstFrame.origin.y + firstFrame.size.height;
CGRect nameFrame = CGRectMake(firstFrame.origin.x,
pivot + kPadding,
kTextWidth, kTextHeight);
CGRect descriptionFrame = CGRectMake(firstFrame.origin.x,
pivot + kTextHeight + kPadding * 2,
kTextWidth, kTextHeight);
if (_nameField == nil && _descriptionField == nil) {
// first UITextField
_nameField = [[UITextField alloc] initWithFrame:CGRectZero];
[_nameField setPlaceholder:kNamePlaceholder];
[_nameField setBorderStyle:UITextBorderStyleRoundedRect];
[_nameField setDelegate:self];
// second UITextField
_descriptionField =
[[UITextField alloc] initWithFrame:CGRectZero];
[_descriptionField setPlaceholder:kDescriptionPlaceholder];
[_descriptionField setBorderStyle:UITextBorderStyleRoundedRect];
[_descriptionField setDelegate:self];
[self addSubview:_nameField];
[self addSubview:_descriptionField];
}
// set the new frames to each text field
[_nameField setFrame:nameFrame];
[_descriptionField setFrame:descriptionFrame];
// increment background image-view height by "displacement"
backgroundFrame.size.height += displacement;
[backgroundView setFrame:backgroundFrame];
// displace by "diplacement" every subview positioned after "pivot"
for (UIView *view in subviews) {
CGRect viewRect = [view frame];
if (viewRect.origin.y > pivot)
[view setFrame:CGRectOffset(viewRect, 0., displacement)];
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return YES;
}
#pragma mark -
#pragma mark <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}
@end
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil,
NSStringFromClass([AppDelegate class]));
[pool drain];
return retVal;
}
I managed to customize the UIAlertView putting two UITextFields as subviews inside of it but in doing so somehow the keyboard broke.
When I try to dismiss it resigning the first responder it doesn't do anything just stays there.
Here's my code, well, only relevant parts (this is what I put in a UIAlertView subclass):
#pragma mark -
#pragma mark UIView
- (void)layoutSubviews
{
[super layoutSubviews];
NSArray *subviews = [self subviews];
UIView *backgroundView = [subviews objectAtIndex:0];
CGRect firstFrame = [[subviews objectAtIndex:2] frame];
CGRect backgroundFrame = [backgroundView frame];
CGFloat displacement = kTextHeight * 2 + kPadding * 3;
CGFloat pivot = firstFrame.origin.y + firstFrame.size.height;
CGRect nameFrame = CGRectMake(firstFrame.origin.x,
pivot + kPadding,
kTextWidth, kTextHeight);
CGRect descriptionFrame = CGRectMake(firstFrame.origin.x,
pivot + kTextHeight + kPadding * 2,
kTextWidth, kTextHeight);
if (_nameField == nil && _descriptionField == nil) {
// first UITextField
_nameField = [[UITextField alloc] initWithFrame:CGRectZero];
[_nameField setPlaceholder:kNamePlaceholder];
[_nameField setBorderStyle:UITextBorderStyleRoundedRect];
[_nameField setDelegate:self];
// second UITextField
_descriptionField =
[[UITextField alloc] initWithFrame:CGRectZero];
[_descriptionField setPlaceholder:kDescriptionPlaceholder];
[_descriptionField setBorderStyle:UITextBorderStyleRoundedRect];
[_descriptionField setDelegate:self];
[self addSubview:_nameField];
[self addSubview:_descriptionField];
}
// set the new frames to each text field
[_nameField setFrame:nameFrame];
[_descriptionField setFrame:descriptionFrame];
// increment background image-view height by "displacement"
backgroundFrame.size.height += displacement;
[backgroundView setFrame:backgroundFrame];
// displace by "diplacement" every subview positioned after "pivot"
for (UIView *view in subviews) {
CGRect viewRect = [view frame];
if (viewRect.origin.y > pivot)
[view setFrame:CGRectOffset(viewRect, 0., displacement)];
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return YES;
}
#pragma mark -
#pragma mark <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}
Here's my code in one big chunk:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
static CGFloat kTextWidth = 260.;
static CGFloat kTextHeight = 25.;
static CGFloat kPadding = 10.;
static NSString *kNamePlaceHolder = @"Name";
static NSString *kDescriptionPlaceHolder = @"Description";
// interfaces
@interface AppDelegate: NSObject <UIApplicationDelegate>
{
UIWindow *_window;
}
- (void)showAlertView;
@end
@interface AlertView: UIAlertView <UITextFieldDelegate>
{
UITextField *_nameField;
UITextField *_descriptionField;
}
@end
// implementations
@implementation AppDelegate
#pragma mark -
#pragma mark NSObject
- (void)dealloc
{
[_window release];
[super dealloc];
}
#pragma mark -
#pragma mark AppDelegate
- (void)showAlertView
{
AlertView *alertView = [[[AlertView alloc] initWithTitle:@"Title"
message:@"Body" delegate:nil
cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
[alertView show];
}
#pragma mark -
#pragma mark <UIApplicationDelegate>
- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)options
{
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[_window setBackgroundColor:[UIColor whiteColor]];
[_window makeKeyAndVisible];
[NSTimer scheduledTimerWithTimeInterval:3. target:self
selector:@selector(showAlertView) userInfo:nil repeats:NO];
return YES;
}
@end
@implementation AlertView
#pragma mark -
#pragma mark NSObject
- (void)dealloc
{
[_nameField setDelegate:nil];
[_descriptionField setDelegate:nil];
[_nameField release];
[_descriptionField release];
[super dealloc];
}
#pragma mark -
#pragma mark UIView
- (void)layoutSubviews
{
[super layoutSubviews];
NSArray *subviews = [self subviews];
UIView *backgroundView = [subviews objectAtIndex:0];
CGRect firstFrame = [[subviews objectAtIndex:2] frame];
CGRect backgroundFrame = [backgroundView frame];
CGFloat displacement = kTextHeight * 2 + kPadding * 3;
CGFloat pivot = firstFrame.origin.y + firstFrame.size.height;
CGRect nameFrame = CGRectMake(firstFrame.origin.x,
pivot + kPadding,
kTextWidth, kTextHeight);
CGRect descriptionFrame = CGRectMake(firstFrame.origin.x,
pivot + kTextHeight + kPadding * 2,
kTextWidth, kTextHeight);
if (_nameField == nil && _descriptionField == nil) {
// first UITextField
_nameField = [[UITextField alloc] initWithFrame:CGRectZero];
[_nameField setPlaceholder:kNamePlaceholder];
[_nameField setBorderStyle:UITextBorderStyleRoundedRect];
[_nameField setDelegate:self];
// second UITextField
_descriptionField =
[[UITextField alloc] initWithFrame:CGRectZero];
[_descriptionField setPlaceholder:kDescriptionPlaceholder];
[_descriptionField setBorderStyle:UITextBorderStyleRoundedRect];
[_descriptionField setDelegate:self];
[self addSubview:_nameField];
[self addSubview:_descriptionField];
}
// set the new frames to each text field
[_nameField setFrame:nameFrame];
[_descriptionField setFrame:descriptionFrame];
// increment background image-view height by "displacement"
backgroundFrame.size.height += displacement;
[backgroundView setFrame:backgroundFrame];
// displace by "diplacement" every subview positioned after "pivot"
for (UIView *view in subviews) {
CGRect viewRect = [view frame];
if (viewRect.origin.y > pivot)
[view setFrame:CGRectOffset(viewRect, 0., displacement)];
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return YES;
}
#pragma mark -
#pragma mark <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}
@end
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil,
NSStringFromClass([AppDelegate class]));
[pool drain];
return retVal;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的解决方案是重写
UIAlertView
子类中的becomeFirstResponder
:我将发布我的所有代码,以防有人需要。正确处理 UIAlertView 自定义。
CustomizedAlertView.h
CustomizedAlertView.m
My solution was to override
becomeFirstResponder
in theUIAlertView
subclass:I'll post all my code in case someone needs it. Properly handles UIAlertView customization.
CustomizableAlertView.h
CustomizableAlertView.m
这是一个用法示例:
InputView.h
InputView.m
Here's a usage example:
InputView.h
InputView.m