[IOS] 自定義AlertView實現模態對話框


 在Windows應用程序中,經常使用模態(Model)對話框來和用戶進行簡單的交互,比如登錄框。
IOS應用程序中,有時我們也希望做同樣的事情。但IOS的UI庫中,沒有模態對話框,最接近那個樣子的應該算是AlertView。
但僅用AlertView,我們只能做文字提示,而不能和用戶做交互。
本文將介紹如何基於AlertView做定製,實現模態對話框的功能。以密碼修改框爲例:

1. 首先,我們要繼承AlertView類,在類的頭文件PwdModifyView.h中,加入控件的聲明
    這裏我們把控件都聲明爲property,目的是讓外部的類可以訪問用戶輸入的數據。
 

  

#import <UIKit/UIKit.h>

@interface PwdModifyView : UIAlertView

@property(nonatomic, retain) UITextField* _oldPwd;    // 舊密碼輸入框
@property(nonatomic, retain) UITextField* _newPwd;    // 新密碼輸入框
@property(nonatomic, retain) UITextField* _cfmPwd;    // 新密碼確認框

@end
 
2. 在PwdModifyView.m文件中,需要實現兩個函數
 
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
    self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
    if (self != nil) {
        // 初始化自定義控件,注意擺放的位置,可以多試幾次位置參數直到滿意爲止
        // createTextField函數用來初始化UITextField控件,在文件末尾附上
        self._oldPwd = [self createTextField:@"舊密碼"
                                   withFrame:CGRectMake(22, 45, 240, 36)];
        [self addSubview:self._oldPwd];
       
        self._newPwd = [self createTextField:@"新密碼"
                                   withFrame:CGRectMake(22, 90, 240, 36)];
        [self addSubview:self._newPwd];
       
        self._cfmPwd = [self createTextField:@"確認新密碼"
                                   withFrame:CGRectMake(22, 135, 240, 36)];
        [self addSubview:self._cfmPwd];
    }
   
    return self;
}
 
 
// Override父類的layoutSubviews方法
- (void)layoutSubviews {
    [super layoutSubviews];     // 當override父類的方法時,要注意一下是否需要調用父類的該方法
   
    for (UIView* view in self.subviews) {
        // 搜索AlertView底部的按鈕,然後將其位置下移
        // IOS5以前按鈕類是UIButton, IOS5裏該按鈕類是UIThreePartButton
        if ([view isKindOfClass:[UIButton class]] ||
            [view isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {
            CGRect btnBounds = view.frame;
            btnBounds.origin.y = self._cfmPwd.frame.origin.y + self._cfmPwd.frame.size.height + 7;
            view.frame = btnBounds;
        }
    }
   
    // 定義AlertView的大小
    CGRect bounds = self.frame;
    bounds.size.height = 260;
    self.frame = bounds;
}
 
3. 當需要彈出該對話框時,只需創建並初始化一個PwdModifyView對象,然後調用對象的show()方法即可。
 
PwdModifyDlg* pwdModifyDlg = [[PwdModifyView alloc]
                     initWithTitle:@"密碼修改"
                     message:nil
                     delegate:self
                     cancelButtonTitle:@"確定"
                     otherButtonTitles:@"取消", nil];
[pwdModifyDlg show];
 
 
最後,附上UITextField的創建函數
 
- (UITextField*)createTextField:(NSString*)placeholder withFrame:(CGRect)frame {
    UITextField* field = [[UITextField alloc] initWithFrame:frame];
    field.placeholder = placeholder;
    field.secureTextEntry = YES;
    field.backgroundColor = [UIColor whiteColor];
    field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    return field;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章