zoukankan      html  css  js  c++  java
  • iOS_16_开关控制器_modal_代码方法

    最后效果图:



    main.storyboard



    BeyondViewController.h

    //
    //  BeyondViewController.h
    //  16_控制器切换方式1_Modal_通过代码方式
    //
    //  Created by beyond on 14-7-30.
    //  Copyright (c) 2014年 com.beyond. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface BeyondViewController : UIViewController
    // 提示欢迎谁谁谁
    @property (weak, nonatomic) IBOutlet UILabel *welcomeLabel;
    @property (weak, nonatomic) IBOutlet UIButton *wantLoginBtn;
    @property (weak, nonatomic) IBOutlet UIButton *wantLogoutBtn;
    
    // 点击BeyondViewController界面上的登录button,切换到BeyondLoginViewController.h进行输入password帐号登录
    - (IBAction)wantLogin:(UIButton *)sender;
    - (IBAction)wantLogout:(UIButton *)sender;
    
    @end
    


    BeyondViewController.m

    //
    //  BeyondViewController.m
    //  16_控制器切换方式1_Modal_通过代码方式
    //
    //  Created by beyond on 14-7-30.
    //  Copyright (c) 2014年 com.beyond. All rights reserved.
    
    /*
     控制器切换的3种方式:
     1,modal (模态对话框,新的控制器从底部往上展开,遮住后面的控制器)(很多其它能够參看 罗云彬的<琢石成器—Windows环境下32位汇编语言程序设计>第5.4章 对话框)
        通过代码实现切换
        通过storyboard实现切换
     
     2,push 通过UINavigationController管理的栈实现
        从右往左边展开,弹出新的控制器(处于栈顶),
        涉及内容主要有:
                    參数的传递
                    导航栏的标题定制
                    跳转前的验证,典型如登录跳转前的client校验
     
     3,UITabbarController
        以平行的方式是管理子视图控制器
    
     
     4,自己定义容器,相似抽屉效果(右滑,弹出左側边栏)
     
     */
    
    #import "BeyondViewController.h"
    #import "BeyondLoginViewController.h"
    @interface BeyondViewController ()<BeyondLoginViewControllerDelegate,UIActionSheetDelegate>
    
    @end
    
    @implementation BeyondViewController
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	
    }
    
    // 点击BeyondViewController界面上的登录按钮,切换到BeyondLoginViewController.h进行输入password帐号登录
    - (IBAction)wantLogin:(UIButton *)sender {
        // 想通过代码以Modal的方式,切换到BeyondLoginViewController控制器,就必须创建实例对象,耦合性太强~
        BeyondLoginViewController *loginViewCtrl = [[BeyondLoginViewController alloc]init];
        // 设置loginViewCtrl的代理 为当前控制器,由于,在下一个控制器(loginViewCtrl)中,用户输入完用户名和password之后,会调用代理 的doSomethingWithUsername方法,给它的代理对象(即当前控制器)发消息,參数 就是要传递过来的用户名~
        loginViewCtrl.delegate = self;
        // 关键,代码,全部控制器都有该方法,展现
        [self presentViewController:loginViewCtrl animated:YES completion:^{
            NSLog(@"BeyondLogin控制器--出现了");
        }];
    }
    
    
    
    
    // 实现下一个控制器中代理方法,由于在下一个控制器(loginViewCtrl)中,用户输入完用户名和password之后,会调用代理 的doSomethingWithUsername方法,给它的代理对象(即当前控制器)发消息,參数 就是要传递过来的用户名~
    - (void)doSomethingWithLoginName:(NSString *)username
    {
        username = [NSString stringWithFormat:@"欢迎回来:%@",username];
        _welcomeLabel.text = username;
        
        // 禁用登录按钮
        _wantLoginBtn.enabled = NO;
        _wantLogoutBtn.enabled = YES;
    }
    
    
    - (IBAction)wantLogout:(UIButton *)sender
    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"确定注销吗?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [actionSheet showInView:self.view];
    }
    
    #pragma mark - actionSheet的代理 方法
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        NSLog(@"按钮索引:%d",buttonIndex);
        // 确定注销   索引是 0
        if (buttonIndex == 0) {
            // 注销
            _wantLoginBtn.enabled = YES;
            _wantLogoutBtn.enabled = NO;
            _welcomeLabel.text = @"";
        }
        // 取消   索引是 1  doNothing
        
    }
    @end
    

    协议 

    BeyondLoginViewControllerDelegate.h

    //
    //  BeyondLoginViewControllerDelegate.h
    //  16_控制器切换方式1_Modal
    //
    //  Created by beyond on 14-7-30.
    //  Copyright (c) 2014年 com.beyond. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @protocol BeyondLoginViewControllerDelegate <NSObject>
    
    - (void) doSomethingWithLoginName:(NSString *)username;
    @end
    


    BeyondLoginViewController.xib


    BeyondLoginViewController.h

    //
    //  BeyondLoginViewController.h
    //  16_控制器切换方式1_Modal
    //
    //  Created by beyond on 14-7-30.
    //  Copyright (c) 2014年 com.beyond. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import "BeyondLoginViewControllerDelegate.h"
    
    @interface BeyondLoginViewController : UIViewController
    
    // id类型的代理 (weak弱引用),调用代理 的方法,将本控制器中用户输入的姓名,通过參数传递给 代理 ,供其使用
    @property (nonatomic,weak) id <BeyondLoginViewControllerDelegate> delegate;
    
    @property (weak, nonatomic) IBOutlet UITextField *username;
    @property (weak, nonatomic) IBOutlet UITextField *password;
    
    // 向server提交username与password
    - (IBAction)submit:(UIButton *)sender;
    
    // 点击返回button,返回到前一个控制器
    - (IBAction)backToHome:(UIBarButtonItem *)sender;
    
    @end
    


    BeyondLoginViewController.m

    //
    //  BeyondLoginViewController.m
    //  16_控制器切换方式1_Modal
    //
    //  Created by beyond on 14-7-30.
    //  Copyright (c) 2014年 com.beyond. All rights reserved.
    //
    
    #import "BeyondLoginViewController.h"
    #import "BeyondViewController.h"
    #import "BeyondLoginViewControllerDelegate.h"
    @interface BeyondLoginViewController ()
    
    @end
    
    @implementation BeyondLoginViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // 设置password框 为星号
        _password.secureTextEntry = YES;
    }
    
    
    // 点击返回button,返回到前一个控制器
    - (IBAction)backToHome:(UIBarButtonItem *)sender
    {
        // 关键代码,关闭自身这个modal模态对话框
        [self dismissViewControllerAnimated:YES completion:^{
            NSLog(@"BeyondLogin控制器,消失了");
        }];
    }
    
    // 向server提交用户名和password
    - (IBAction)submit:(UIButton *)sender {
        // 如同 JS 表单验证
        if (_username.text.length == 0 || _password.text.length == 0) {
            // <#(id<UIActionSheetDelegate>)#>
            UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"请输入用户名和password" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"红色警告" otherButtonTitles:@"其它button", nil];
            // 如同toast,手动显示
            [actionSheet showInView:self.view];
            
            // 直接返回
            return;
        }
        
        // 传递数据 给上一个控制器
        /*
        // 方式1,耦合性太强,直接得到弹出自己的那一个(上一个)控制器
        BeyondViewController *preVC = (BeyondViewController *)self.presentingViewController;
                // 这样就能够设置上一个控制 器的Label了
        preVC.welcomeLabel.text = _username.text;
        [self dismissViewControllerAnimated:YES completion:nil];
        */
        
        // 方式2,使用代理 ,调用代理 的方法,并将当前控制器的输入的用户名,作为參数,传递给代理的这种方法里,供代理去使用
        [self.delegate doSomethingWithLoginName:_username.text];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    @end
    



    Modal模态对话框--------通过storyboard方式实现




    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    转载:SqlServer数据库性能优化详解
    复杂事件处理技术概览(一)
    Netty : writeAndFlush的线程安全及并发问题
    如何在RCP程序中添加一个banner栏
    AChecker + Selenium2对需要登录的页面进行自动化可访问性测试
    5分钟开启Esper之旅
    使SWT/JFace支持跨平台
    Ubuntu上Docker安装Trouble Shooting
    使Docker Container支持运行SWT程序
    Xcode5中如何切换Storyboards为xib
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4640875.html
Copyright © 2011-2022 走看看