界面:
[img]http://dl.iteye.com/upload/attachment/0070/3385/8c2d14c5-9c0f-32c3-b02a-f09db66d5db4.png[/img]
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
运行结果:
[img]http://dl.iteye.com/upload/attachment/0070/3387/cc324c75-7cd0-3e86-b0f8-d64ca5fa4591.png[/img]
[img]http://dl.iteye.com/upload/attachment/0070/3389/1ff22572-4607-3128-8611-30785afaf574.png[/img]