[iphone] 提示框 UIAlertViewDelegate和UIActionSheetDelegate

界面:
[iphone] 提示框 UIAlertViewDelegate和UIActionSheetDelegate

ViewController.h
//
//  ViewController.h
//  HellowWorld
//
//  Created by iaiai on 12-7-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

//如果用alert则用UIAlertViewDelegate,如果用action则用UIActionSheetDelegate
//@interface ViewController : UIViewController<UIActionSheetDelegate>{
@interface ViewController : UIViewController<UIAlertViewDelegate>{
    IBOutlet UITextField *txtName;
}

@property (nonatomic, retain) UITextField *txtName;

-(IBAction)btnClicked:(id)sender;

-(IBAction)btnClicked1:(id)sender;

@end


ViewController.m
//
//  ViewController.m
//  HellowWorld
//
//  Created by iaiai on 12-7-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@implementation ViewController

@synthesize txtName;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

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

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(IBAction)btnClicked:(id)sender{
    @autoreleasepool {
        NSString *str = [[NSString alloc] initWithFormat:@"Hello, %@", txtName.text];
        UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:str delegate:self cancelButtonTitle:@"OK" destructiveButtonTitle:@"Delete Message" otherButtonTitles:@"按钮一",@"按钮二", nil];
        [action showInView:self.view];
    }
}

-(IBAction)btnClicked1:(id)sender{
    @autoreleasepool {
        NSString *str = [[NSString alloc] initWithFormat:@"Hello, %@", txtName.text];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:str delegate:self cancelButtonTitle:@"Done" otherButtonTitles:@"Cancel",@"按钮一",@"按钮二", nil];
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%i",buttonIndex);
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%d",buttonIndex);
}

@end


运行结果:
[iphone] 提示框 UIAlertViewDelegate和UIActionSheetDelegate
[iphone] 提示框 UIAlertViewDelegate和UIActionSheetDelegate

你可能感兴趣的:(uialertview)