UIActionSheet和UIAlertView的使用
先来看看效果
1.点击登录按钮 弹出UIActionSheet

2.点击确定 弹出UIAlertView带文本框

3.填入数据点击确定

4.提示收到信息

代码如下:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 设置屏幕背景图片,自定义图片
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];
imgView.frame = self.view.bounds;
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view insertSubview:imgView atIndex:0];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"1.jpg"]]];
// 初始化一个登录按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:@"登录" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.frame = CGRectMake(self.view.frame.size.width/2-50, 50, 100, 44);
[btn addTarget:self action:@selector(logIn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)logIn
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"警告" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles: nil];
[actionSheet showInView:actionSheet];
}
//代理方法
-(void)showAlert:(NSString *)msg {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:nil
message:@"请输入账号和密码"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
}
// 实现点击事件
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
[self showAlert:@"确定"];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
UIAlertView *alert1 = [[UIAlertView alloc]
initWithTitle:nil
message:@"已收到信息"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles: nil];
[alert1 show];
}
}
@end
谢谢!!!