[IOS] custom alertview implementation mode dialog box

Source: Internet
Author: User

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 and add self at the end of the file. _ 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 method of override parent class-(void) layoutsubviews {[Super layoutsubviews]; // when override parent class method, note whether you need 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; }}// defines 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" cancel: @ "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 = UIControlContentVerticalAlignmentCenter;    return field;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.