.h
@interface Control_FunViewController : UIViewController <UIActionSheetDelegate> {
UITextField *nameField;
.....
}
- (IBAction)buttonPressed;
......
@end
.m
- (IBAction)buttonPressed {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Are you sure?"
delegate:self
cancelButtonTitle:@"No Way!"
destructiveButtonTitle:@"Yes, I'm Sure!"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet //实现委托的方法
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [actionSheet cancelButtonIndex])
{
NSString *msg = nil;
if (nameField.text.length > 0)
msg = [[NSString alloc] initWithFormat:
@"You can breathe easy, %@, everything went OK."
, nameField.text];
else
msg = @"You can breathe easy, everything went OK.";
UIAlertView *alert = [[UIAlertView alloc] //报警
initWithTitle:@"Something was done"
message:msg
delegate:nil
cancelButtonTitle:@"Phew!"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
}
//注:当表上有按钮按下时,委托的
- (void)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex
方法将被调用。