UIAlertView for IOS development (1)

Source: Internet
Author: User

1. Preface
Previously, I learned the basic syntax of Objective-C. From today on, I started to learn simple IOS view development.

2. UIAlertView
2.1 normal bullet box
The best way to use the prompt view is to use a specific initialization method:

 

[Plain]
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// Title: This string is displayed on the top of the prompt view.
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "Title"
// Message: the actual message to be viewed.
Message: @ "Message"
// Delegate: You can pass the delegate object (optional) to the prompt view. When the view status changes, the delegate object will be notified. The passed parameter object must use the UIAlertViewDelegate protocol.
Delegate: nil
// CancelButtonTitle: an optional parameter. This string is displayed on the cancel button in the prompt view. The prompt view with the cancel button usually requires the user to confirm. If the user is unwilling to perform this action, the user will press Cancel. The icon of this button can be set by yourself, but it may not be displayed as canceled.
CancelButtonTitle: @ "Cancel"
// OtherButtonTitles: Optional. If you want to display other buttons in the view, you only need to pass the title parameter. This parameter must be separated by commas (,) and end with nil.
OtherButtonTitles: @ "OK", nil];
[AlertView show];
}

-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// Title: This string is displayed on the top of the prompt view.
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "Title"
// Message: the actual message to be viewed.
Message: @ "Message"
// Delegate: You can pass the delegate object (optional) to the prompt view. When the view status changes, the delegate object will be notified. The passed parameter object must use the UIAlertViewDelegate protocol.
Delegate: nil
// CancelButtonTitle: an optional parameter. This string is displayed on the cancel button in the prompt view. The prompt view with the cancel button usually requires the user to confirm. If the user is unwilling to perform this action, the user will press Cancel. The icon of this button can be set by yourself, but it may not be displayed as canceled.
CancelButtonTitle: @ "Cancel"
// OtherButtonTitles: Optional. If you want to display other buttons in the view, you only need to pass the title parameter. This parameter must be separated by commas (,) and end with nil.
OtherButtonTitles: @ "OK", nil];
[AlertView show];
}


Running result:

 

 
 

 

2.2 proxy bullet box
. H file:


[Plain]
@ Interface ZYAlertYesOrNoViewController: UIViewController <UIAlertViewDelegate> // added the UIAlertViewDelegate proxy.
-(Void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex;
@ End

@ Interface ZYAlertYesOrNoViewController: UIViewController <UIAlertViewDelegate> // added the UIAlertViewDelegate proxy.
-(Void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex;
@ End. m file:


[Plain]
-(Void) viewDidAppear :( BOOL) animated {
[Super viewDidAppear: animated];
// Initialize UIAlertView
Self. view. backgroundColor = [UIColor whiteColor];
UIAlertView * alertView = [[UIAlertView alloc]
InitWithTitle: @ "Rating"
Message: @ "Can you please rate our app? "
// Add a proxy for itself
Delegate: self
CancelButtonTitle: [self noButtonTitle]
OtherButtonTitles: [self yesButtonTitle], nil];
[AlertView show];
}

-(Void) viewDidAppear :( BOOL) animated {
[Super viewDidAppear: animated];
// Initialize UIAlertView
Self. view. backgroundColor = [UIColor whiteColor];
UIAlertView * alertView = [[UIAlertView alloc]
InitWithTitle: @ "Rating"
Message: @ "Can you please rate our app? "
// Add a proxy for itself
Delegate: self
CancelButtonTitle: [self noButtonTitle]
OtherButtonTitles: [self yesButtonTitle], nil];
[AlertView show];
}
[Plain]

-(NSString *) yesButtonTitle {return @ "Yes ";
}
-(NSString *) noButtonTitle {return @ "No ";
}
// Determine whether the user presses Yes or No
-(Void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {
NSString * buttonTitle = [alertView buttonTitleAtIndex: buttonIndex];
If ([buttonTitle isw.tostring: [self yesButtonTitle]) {
NSLog (@ "User pressed the Yes button .");
} Else if ([buttonTitle isw.tostring: [self noButtonTitle]) {
NSLog (@ "User pressed the No button .");
}
}

-(NSString *) yesButtonTitle {return @ "Yes ";
}
-(NSString *) noButtonTitle {return @ "No ";
}
// Determine whether the user presses Yes or No
-(Void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {
NSString * buttonTitle = [alertView buttonTitleAtIndex: buttonIndex];
If ([buttonTitle isw.tostring: [self yesButtonTitle]) {
NSLog (@ "User pressed the Yes button .");
} Else if ([buttonTitle isw.tostring: [self noButtonTitle]) {
NSLog (@ "User pressed the No button .");
}
}
When you click Yes
Running result (displayed on the console ):


11:21:33. 675 UIAlertViewTestDemo [1147: c07] User pressed the Yes button.

 


2.3 Alert with input box


[Plain]
// Login pop-up box: a text input box and a password box
UIAlertView * alertView = [[UIAlertView alloc]
InitWithTitle: @ "Password" message: @ "Please enter your credentials" delegate: self
CancelButtonTitle: @ "Cancel" otherButtonTitles: @ "OK", nil];
// Set the AlertView Style
[AlertView setAlertViewStyle: UIAlertViewStyleLoginAndPasswordInput];
[AlertView show];

// Login pop-up box: a text input box and a password box
UIAlertView * alertView = [[UIAlertView alloc]
InitWithTitle: @ "Password" message: @ "Please enter your credentials" delegate: self
CancelButtonTitle: @ "Cancel" otherButtonTitles: @ "OK", nil];
// Set the AlertView Style
[AlertView setAlertViewStyle: UIAlertViewStyleLoginAndPasswordInput];
[AlertView show];
Running result:

 

 


UIAlertView style:


[Plain]
Type enum {
UIAlertViewStyleDefalut = 0; // default Style
UIAlertViewStyleSecureTextInput, // Password box
UIAlertViewStylePlainTextInput, // text input box
UIAlertViewStyleLoginAndPasswordInput // prompt box with logon Effect
} UIAlertViewStyle

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.