您的位置:首页 > 产品设计 > UI/UE

UIAlertController详解和UINavigaton使用解析

2017-01-12 17:26 127 查看

一。UIAlertController

        从事Android开发的人都知道Android的提示框是有AlertDialog来实现,完成各种展示效果,您亦可以实现各种布局定制,IOS同样有自己的提示框在6以前是有UIAlertView负责,

UIAlertView *alertView=

[[UIAlertView alloc] initWithTile:@"提示" message:@"投资有风险,请多多留意!"  delegate:self cancelButtonTitle:@"取消"  otherButtonTitle:@" 确定",nil];

[alertView show];

8开始使用UIAlertController全面替代UIAlertView,只是官方建议,UIAlertView也可以使用。

UIAlertController相对于UIAlertView的有点就是,它把Alert和ActionSheet整合成UIAlertController的不同的Style,你只要在初始化的时候指定好Style就可以是Alert或者ActionSheet然后可以添加自己的各种UIAlertAction。

UIAlertController *controller=[UIAlertController alertWithTitle:@"tile" message:@"message" preferedStyle:UIAlertControllerStyleAlert ];
//创建了一个Alert如果是UIAlertControllerStyleActionSheet就是创建了一个ActionSheet其类似于Andriod的PopupWindow
UIAlertAction *cancelAction=[UIAlertAction
actionWithTitle:@"登陆"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){

                                 //style:UIAlertActionStyleDefault指定Action的类型一般有确定 取消和中性很类似android的AlertDialog
                                 //handler:的参数是一个block就是android对应的OnClickListner处理点击事件的
               NSLog(@"点击了%@",[action title]);
    UITextField *name=alertContrller.textFields.firstObject;
        UITextField *pwd=alertContrller.textFields.lastObject;

}];//初始化一个action
[controller addAction:cancelAction];//把自己想要的添加的UIAlertAction添加到UIAlertController中,可以初始化好多添加好多,根据实际使用情况
[self presentViewController:controller animated:YES completion:nil ];//显示UIAlertController,点击任何一个Action后都会消失

有时候我们需要在Alert中需要输入框,比如登录或者注册的Alert等
[controller addTextFieldWithConfiguration:^(UITextField *nametField){
        name.placeHolder=@"用户名";

}];
[controller addTextFieldWithConfiguration:^(UITextFiled *pwd){
       pwd.placeHolder=@"密码";
       pwd.secrueTextEntry=YES;

}];

这样就把两个输入框UITextField加入到了UIAlertController里面,但是还是有点美中不足之处,就是我们无法鉴定用户的输入状态,就像Android的EditView的
TextWacher一样监听着用户的输入开始过程和结束等动作,还好IOS为我们提供了NSNotificationCenter

        [[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(abserverTextFieldChange:)
name:UITextFieldTextDidChangeNotification
object:pwdTextFiled];

-(void)abserverTextFieldChange:(NSNotification*) notification{
    UIAlertController *alertContrller=(UIAlertController*)self.presentedViewController;
    if(alertContrller){
        UITextField *name=alertContrller.textFields.firstObject;
        UITextField *pwd=alertContrller.textFields.lastObject;
        UIAlertAction *sureAction=alertContrller.actions.firstObject;
        if(name.text.length>2&&pwd.text.length>5){
            sureAction.enabled=YES;
        }
    }
}

二,UINavigationController

UINavigationController其实就是每一个UIViewController的布局控制器,每一个UIViewController都有一个属于自己的UINavigationController。
UINavigationController分为三部分很像Android RootView,R.id.contentView就是代表后来我们自己定义的Layout的id。
UINavigationController的三部分分别是UINavigationBar其实就是TitleBar(UINavigationItem UIBarButtonItem titleView),UINavigation content View(屏幕中间的主要内容布局的地方),UINavigationController ToolBar(屏幕下面的工具栏
UIBarButtonItem, toolbarItems)

//设置NavigationBar的title也就是android的titleBar

    self.title=@"MainViewController";

    self.navigationController.navigationBar.tintColor=[UIColor redColor];

    //在NavigationBar添加背景图片

    UIImage *img=[UIImage imageNamed:@"ic_bank_card.png"];

    UIImageView *imgView=[[UIImageView alloc] initWithImage:img];

    [self.navigationController.navigationBar addSubview:imgView];

//    

//    //NavigationBar左边的系统按钮

    UIBarButtonItem *leftBtnItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(leftButtonAction)];

    self.navigationItem.leftBarButtonItem=leftBtnItem;

    

    //使用自定义的UIBarButtonItem

 

//    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];

//    [btn1 setFrame:CGRectMake(500, 0, 60, 30)];

//    [btn1 setBackgroundColor:[UIColor redColor]];

//    [btn1 setBackgroundImage:[UIImage imageNamed:@"ic_bank_card_verify"] forState:UIControlStateNormal];

//    [btn1 setBackgroundImage:[UIImage imageNamed:@"ic_eye_open"] forState:UIControlStateHighlighted];

//    

//    UIBarButtonItem *custonItem=[[UIBarButtonItem alloc]init];

//    custonItem.customView=btn1;

//    custonItem.action=@selector(leftButtonAction);

//    self.navigationItem.leftBarButtonItem=custonItem;

    

    //自定义中间的titleView

    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn2.frame=CGRectMake(0, 0, 60, 30);

    [btn2 setBackgroundColor:[UIColor whiteColor]];

    [btn2 setTitle:@"Hello IOS" forState:UIControlStateNormal];

    [btn2 setTitle:@"嗯呢" forState:UIControlStateHighlighted];

    self.navigationItem.titleView=btn2;

    

    UIBarButtonItem *rightBtnItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(rightBarButtonAction)];

    self.navigationItem.rightBarButtonItem=rightBtnItem;

#import "MainViewController.h"
#import "ViewControllerTwo.h"

@interface MainViewController ()
@property (weak,
nonatomic) IBOutlet
UILabel *lable;

@end

@implementation MainViewController
- (IBAction)onClickBtn:(id)sender {
    [_lable
setText:@"点击了btn"];
    [_lable
setTextColor:[UIColor
blueColor]];
//    [self.navigationController pushViewController:[[ViewControllerTwo alloc]init] animated:YES];
    [self
presentViewController:[[ViewControllerTwo
alloc]init]
animated:YES
completion:nil];
}
-(void)rightBarButtonAction{
    UIAlertController *alertController=[UIAlertController
alertControllerWithTitle:@"登陆"
message:@"请您先登陆"
preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *loginTextField){
        loginTextField.placeholder=@"用户名";
        [[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(abserverTextFieldChange:)
name:UITextFieldTextDidChangeNotification
object:loginTextField];
    
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *pwdTextFiled){
        pwdTextFiled.placeholder=@"密码";
        pwdTextFiled.secureTextEntry=YES;
        [[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(abserverTextFieldChange:)
name:UITextFieldTextDidChangeNotification
object:pwdTextFiled];
    
    }];
    UIAlertAction *sureAction=[UIAlertAction
actionWithTitle:@"登陆"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
        NSLog(@"点击了%@",[action
title]);
        UITextField *name=alertController.textFields.firstObject;
        UITextField *pwd=alertController.textFields.lastObject;
        NSLog(@"用户名:%@ 
密码:%@",name,pwd);
        
    }];
    sureAction.enabled=NO;
    
    [alertController addAction:sureAction];
    
    UIAlertAction *cancelAction=[UIAlertAction
actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action){
        NSLog(@"点击了%@",[action
title]);
    
    }];
    [alertController addAction:cancelAction];
    [self presentViewController:alertController animated:YES completion:nil];

}
-(void)abserverTextFieldChange:(NSNotification*) notification{
    UIAlertController *alertContrller=(UIAlertController*)self.presentedViewController;
    if(alertContrller){
        UITextField *name=alertContrller.textFields.firstObject;
        UITextField *pwd=alertContrller.textFields.lastObject;
        UIAlertAction *sureAction=alertContrller.actions.firstObject;
        if(name.text.length>2&&pwd.text.length>5){
            sureAction.enabled=YES;
        }
    
    }

}
-(void)leftButtonAction{
    UIAlertController *alerController=[UIAlertController alertControllerWithTitle:@"点击了返回" message:@"真可以返回的哦,你确定吗"
preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:
                                 ^(UIAlertAction *action){
                                     NSLog(@"点击了取消按钮");
                                 }];
    UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:
                               ^(UIAlertAction *action){
                                   NSLog(@"点击了%@按钮",[action title]);
                               }];
    [alerController addAction:cancelAction];
    [alerController addAction:sureAction];
    [self presentViewController:alerController animated:YES completion:nil];
}

- (void)viewDidLoad {
    
    
    [super viewDidLoad];
    //设置NavigationBar的title也就是android的titleBar
    self.title=@"MainViewController";
    self.navigationController.navigationBar.tintColor=[UIColor redColor];
    //设置NavigationBar的背景也就是android的titleBar的背景
    UIImage *img=[UIImage imageNamed:@"ic_bank_card.png"];
    UIImageView *imgView=[[UIImageView alloc] initWithImage:img];
    [self.navigationController.navigationBar addSubview:imgView];
//    
//    //NavigationBar左边的系统按钮
    UIBarButtonItem *leftBtnItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(leftButtonAction)];
    self.navigationItem.leftBarButtonItem=leftBtnItem;
    
    //使用自定义的UIBarButtonItem
  
//    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
//    [btn1 setFrame:CGRectMake(500, 0, 60, 30)];
//    [btn1 setBackgroundColor:[UIColor redColor]];
//    [btn1 setBackgroundImage:[UIImage imageNamed:@"ic_bank_card_verify"] forState:UIControlStateNormal];
//    [btn1 setBackgroundImage:[UIImage imageNamed:@"ic_eye_open"] forState:UIControlStateHighlighted];
//    
//    UIBarButtonItem *custonItem=[[UIBarButtonItem alloc]init];
//    custonItem.customView=btn1;
//    custonItem.action=@selector(leftButtonAction);
//    self.navigationItem.leftBarButtonItem=custonItem;
    
    //自定义中间的titleView
    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame=CGRectMake(0,
0, 60,
30);
    [btn2 setBackgroundColor:[UIColor whiteColor]];
    [btn2 setTitle:@"Hello IOS" forState:UIControlStateNormal];
    [btn2 setTitle:@"嗯呢" forState:UIControlStateHighlighted];
    self.navigationItem.titleView=btn2;
    
    UIBarButtonItem *rightBtnItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(rightBarButtonAction)];
    self.navigationItem.rightBarButtonItem=rightBtnItem;
    
    
    // Do any additional setup after loading the view from its nib.
}

//-(void) creatNavigation{
//    MainViewController *mainViewController=[[MainViewController alloc]initWithNibName:nil bundle:nil];
//    mainViewController.title=@"MainController";
//    UINavigationController *uiNavigationController=[[UINavigationController alloc]init];
//    self.navigationController=uiNavigationController;
//
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

//1.显示底部工具栏

    self.navigationController.toolbarHidden = NO;

//2.设置底部工具栏的背景图片

    UIImage *navBarImage11 = [UIImage imageNamed:@"bottomBK.png"];

    UIImageView *imageView11 = [[UIImageView alloc] initWithImage:navBarImage11];

    [self.navigationController.toolbar addSubview:imageView11];

//3.设置底部工具栏的文本颜色

    self.navigationController.toolbar.tintColor = [UIColor redColor];

//4.两个自定义tool下的按钮和系统save按钮

    UIBarButtonItem *custom = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil];

    UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];

    [button3 setBackgroundImage:[UIImage imageNamed:@"102.png"] forState:UIControlStateNormal];

    custom.customView = button3;

    UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];

    UIButton *button4 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];

    [button4 setBackgroundImage:[UIImage imageNamed:@"102.png"] forState:UIControlStateNormal];

    add.customView = button4;

    UIBarButtonItem *save = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];

    UIBarButtonItem *save3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];

    UIBarButtonItem *save1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];

    UIBarButtonItem *save2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];

    self.toolbarItems = [NSArray arrayWithObjects:custom,add,save,save1,save2,save3,nil];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: