我设法自定义 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;
}