IOS6.0 Learning Article 2nd: AlertView

Source: Internet
Author: User

Ns_class_available_ios (2_0) @ interface uialertview: uiview {

From the definition of uialertview, we can see that uialertview is also a subclass of uiview and is defined in Versions later than ios2.0.

The following is an example of its definition:

-(Id) initWithTitle :( NSString *) title

Message :( NSString *) message

Delegate :( id/* <UIAlertViewDelegate> */) delegate

CancelButtonTitle :( NSString *) cancelButtonTitle

OtherButtonTitles :( NSString *) otherButtonTitles,..., nil;

Initialize a uialertview, and specify the title, displayed message, and delegate object (the uialertviewdelegate protocol must be implemented. This is generally self, so the viewcontroller object needs to implement this Protocol), the text of the cancel button (cancelbuttontitle), and the text of other buttons (separated by commas (,), ending with nil)

 

@ Property (nonatomic, assign) ID/* <uialertviewdelegate> */delegate; // delegate object

@ Property (nonatomic, copy) nsstring * Title; // Title text

@ Property (nonatomic, copy) nsstring * message; // message text displayed

-(NSInteger) addButtonWithTitle :( NSString *) title;

Add a button to alertview and specify the text displayed by the button, and return its index (from 0, the cancelbutton index is 0)

-(NSString *) buttonTitleAtIndex :( NSInteger) buttonIndex;

Returns the text of the button for the specified index value.

@ Property (nonatomic, readonly) NSInteger numberOfButtons;

Returns the number of all buttons in the nsalertview.

@ Property (nonatomic) nsinteger cancelbuttonindex;

@ Property (nonatomic, readonly) nsinteger firstotherbuttonindex;

@ Property (nonatomic, readonly, getter = isVisible) BOOL visible;

-(Void) show;

Show AlertView

-(Void) dismissWithClickedButtonIndex :( NSInteger) buttonIndex animated :( BOOL) animated;

Hide the button that presses the specified index value, hide the AlertView, and determine whether to enable the animation effect.

@ Property (nonatomic, assign) UIAlertViewStyle alertViewStyle NS_AVAILABLE_IOS (5_0 );

-(UITextField *) textFieldAtIndex :( NSInteger) textFieldIndex NS_AVAILABLE_IOS (5_0 );

Returns the TextField of the specified index value. This API only exists in IOS5.0 or above.

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------

The following describes the UIAlertViewDelegate protocol, which implements the NSObject informal protocol and does not have to be implemented. All methods are optional.

@ Protocol UIAlertViewDelegate <NSObject>

@ Optional

-(Void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex;

When a button for a specified index is clicked, this method is called back. buttonIndex is the index value of the button, starting from 0.

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.

// If not defined in the delegate, we simulate a click in the cancel button

-(Void) alertViewCancel :( UIAlertView *) alertView;

When you press the HOME key, this method is called back. When you click the Cancel button, this method is not called back.

-(Void) willPresentAlertView :( UIAlertView *) alertView;

Callback before displaying the View animation

-(Void) didPresentAlertView :( UIAlertView *) alertView;

Callback after the display animation is complete

-(Void) alertView :( UIAlertView *) alertView willDismissWithButtonIndex :( NSInteger) buttonIndex;

Call back when you want to start the View hiding animation.

-(Void) alertView :( UIAlertView *) alertView didDismissWithButtonIndex :( NSInteger) buttonIndex;

Callback when the hidden animation of the View ends

-(BOOL) alertViewShouldEnableFirstOtherButton :( UIAlertView *) alertView;

Edit any default field adding style and call

In this example, When you click the button, modify the text of UILabel and a dialog box is displayed.

#import <UIKit/UIKit.h>@interface XinYeViewController : UIViewController <UITextFieldDelegate,UIAlertViewDelegate>@property (weak, nonatomic) IBOutlet UIButton *myButton;@property (weak, nonatomic) IBOutlet UITextField *myTextField;@property (weak, nonatomic) IBOutlet UILabel *myLabel;- (IBAction)clickMyButton:(id)sender;@end
# Import "XinYeViewController. h "@ interface XinYeViewController () @ end // Number of buttons clicked static int CLICK_TIMES = 1; @ implementation XinYeViewController @ synthesize myButton; @ synthesize myLabel; @ synthesize myTextField;-(void) viewDidLoad {[super viewDidLoad]; [myTextField becomeFirstResponder]; // the keyboard is displayed as soon as you enter the meeting (myTextField gets the focus)}-(void) ready {[super didReceiveMemoryWarning];} -(IBAction) clickMy Button :( id) sender {CLICK_TIMES + = 1; if (CLICK_TIMES % 2 = 0) {[myLabel setText: @ "modified text, modify by xinye"];} else {[myLabel setText: @ "don't try again. Don't bother !!! "];} [[[UIAlertView alloc] initWithTitle: @" here is the title "message: @" here is the Message message "delegate: self cancelButtonTitle: @ "Cancel button" otherButtonTitles: @ "OK", @ "Hello", @ "World", nil] show];} // handle the event (void) alertView (UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {NSLog (@ "buttonIndex is: % I ", buttonIndex); switch (buttonIndex) {case 0 :{} break; case 1 :{} break; case 2 :{} break; case 3 :{} break; default: break ;}}// call back when the Enter key is clicked-(BOOL) textFieldShouldReturn :( UITextField *) textField {[myTextField resignFirstResponder]; // make myTextField lose focus return YES;} @ end

The running effect is as follows:

 

 

 

 

 

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.