In Windows applications, you often use the Model dialog box to interact with users, such as the logon box.
In IOS apps, we sometimes want to do the same thing. However, in the ios ui library, there is no modal dialog box, and what is closest to that is AlertView.
But with AlertView only, we can only make text prompts, rather than interacting with users.
This article describes how to customize the mode Dialog Box Based on AlertView. Take the password modification box as an example:
1. First, we need to inherit the AlertView class and add the control declaration to the header file PwdModifyView. h of the class.
Here we declare all controls as property to allow external classes to access user input data.
# Import <UIKit/UIKit. h>
@ Interface PwdModifyView: UIAlertView
@ Property (nonatomic, retain) UITextField * _ oldPwd; // old password input box
@ Property (nonatomic, retain) UITextField * _ newPwd; // new password input box
@ Property (nonatomic, retain) UITextField * _ cfmPwd; // New Password confirmation box
@ End
2. In the PwdModifyView. m file, two functions must be implemented.
-(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 ){
// Initialize the custom control. Pay attention to the placement location. You can try the location parameter several times until you are satisfied.
// The createTextField function is used to initialize the UITextField control.
Self. _ oldPwd = [self createTextField: @ "old password"
WithFrame: CGRectMake (22, 45,240, 36)];
[Self addSubview: self. _ oldPwd];
Self. _ newPwd = [self createTextField: @ "new password"
WithFrame: CGRectMake (22, 90,240, 36)];
[Self addSubview: self. _ newPwd];
Self. _ cfmPwd = [self createTextField: @ "confirm new password"
WithFrame: CGRectMake (22,135,240, 36)];
[Self addSubview: self. _ cfmPwd];
}
Return self;
}
// LayoutSubviews of the Override parent class
-(Void) layoutSubviews {
[Super layoutSubviews]; // when using the override parent class method, pay attention to whether to call this method of the parent class.
For (UIView * view in self. subviews ){
// Search for the button at the bottom of AlertView and move it down
// Before IOS5, the button class is UIButton. In IOS5, the button class is 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;
}
}
// Define the size of AlertView
CGRect bounds = self. frame;
Bounds. size. height = 260;
Self. frame = bounds;
}
3. To bring up this dialog box, you only need to create and initialize a PwdModifyView object, and then call the show () method of the object.
PwdModifyDlg * pwdModifyDlg = [[PwdModifyView alloc]
InitWithTitle: @ "password change"
Message: nil
Delegate: self
CancelButtonTitle: @ "OK"
OtherButtonTitles: @ "cancel", nil];
[PwdModifyDlg show];
Finally, the UITextField creation function is attached.
-(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 = uicontrolcontentverticalignmentcenter;
Return field;
}
From linear