Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

objective c - How to create an alert box in iphone?

I would like to make an alert type box so that when the user tries to delete something, it says, "are you sure" and then has a yes or no for if they are sure. What would be the best way to do this in iphone?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A UIAlertView is the best way to do that. It will animate into the middle of the screen, dim the background, and force the user to address it, before returning to the normal functions of your app.

You can create a UIAlertView like this:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this.  This action cannot be undone" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];

That will display the message.

Then to check whether they tapped delete or cancel, use this:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0){
        //delete it
    }
}

Make sure in your header file (.h), you include the UIAlertViewDelegate by putting <UIAlertViewDelegate>, next to whatever your class inherits from (ie. UIViewController or UITableViewController, etc.)

For more infomation on all the specifics of UIAlertViews check out Apple's Docs Here

Hope that helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...