Friday, June 25, 2010

Wired thing after adding subview to UIAlertView

There are many discussions about adding UITextField or other UIView to UIAlertView, here are some links:

  1. New info on adding text fields to alerts
  2. Custom UIAlertView (Color chooser)
  3. Add TextField or other control to UIActionSheet or UIAlertView
At first, I used the undocumented API ‘addTextFieldWithValue’, then I found that your APP may be rejected if undocumented API used. so I changed to the second way, as mentioned in Custom UIAlertView (Color chooser), it works, except one thing, look the following pictures:

This is the first time UIAlertView shows, after I click any button, it dismisses. then when it shows again, looks like this:


I don’t know why, seems cocoa touch re-layout the views again or just forget two buttons positions, as the buttons positions are almost the same as when no subviews in UIAlertView. to avoid this, I have to release the UIAlertView each time after it dismiss.

The following is the code, almost the same as Custom UIAlertView (Color chooser) :
@interface FolderAlertView : UIAlertView
{
        UITextView *errorMsg;
        UITextField *folderName;
}

@property (nonatomic, readonly) UITextView *errorMsg;
@property (nonatomic, readonly) UITextField *folderName;

- (void)clear;
- (NSString *)inputText;
- (void)warn;

@end

@implementation
FolderAlertView

@synthesize errorMsg;
@synthesize folderName;

- (id)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
              
                errorMsg = [[UITextView alloc] initWithFrame:CGRectZero];
                errorMsg.backgroundColor = [UIColor clearColor];
                errorMsg.textColor = [UIColor whiteColor];
                errorMsg.text = @“illegal name“;
                errorMsg.editable = NO;
                errorMsg.font = [UIFont systemFontOfSize:14];
              
                folderName = [[UITextField alloc] initWithFrame:CGRectZero];
                folderName.backgroundColor = [UIColor whiteColor];
                folderName.clearsOnBeginEditing = YES;
                folderName.font = [UIFont systemFontOfSize:17];
                folderName.placeholder = @"Folder Name";
                folderName.adjustsFontSizeToFitWidth = YES;
                folderName.autocorrectionType = UITextAutocorrectionTypeNo;
              
                [super insertSubview:folderName atIndex:0];
                [super insertSubview:errorMsg atIndex:1];
        }
        return self;
}

- (void)setFrame:(CGRect)rect {
        [super setFrame:CGRectMake(0, 0, rect.size.width, 200)];
        self.center = CGPointMake(320/2, 460/2);
}

- (void)layoutSubviews {
        CGFloat buttonTop;
        for (UIView *view in self.subviews) {
                if ([[[view class] description] isEqualToString:@"UIThreePartButton"]) {
                        view.frame = CGRectMake(view.frame.origin.x, self.bounds.size.height - view.frame.size.height - 15,
                                                                        view.frame.size.width, view.frame.size.height);
                        buttonTop = view.frame.origin.y;
                }
        }
      
        buttonTop -= 7;
        buttonTop -= 60;
        errorMsg.frame = CGRectMake(12, buttonTop, self.bounds.size.width - 24, 60);
      
        buttonTop -= 7;
        buttonTop -= 21;
        folderName.frame = CGRectMake(12, buttonTop, self.bounds.size.width - 24, 21);
}

- (void)clear {
        folderName.text = @"";
        errorMsg.textColor = [UIColor whiteColor];
}

- (NSString *)inputText {
        return folderName.text;
}

- (void)warn {
        errorMsg.textColor = [UIColor redColor];
}

- (void)dealloc {
        [errorMsg release];
        [folderName release];
        [super dealloc];
}

@end

No comments: