iOS_16_控制器切换_modal_代码方式

来源:互联网 发布:淘宝美版三星s7能买吗 编辑:程序博客网 时间:2024/05/29 08:33

最终效果图:



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界面上的登录按钮,切换到BeyondLoginViewController.h进行输入密码帐号登录- (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管理的栈实现    从右往左边展开,弹出新的控制器(处于栈顶),    涉及内容主要有:                参数的传递                导航栏的标题定制                跳转前的验证,典型如登录跳转前的客户端校验  3,UITabbarController    以平行的方式是管理子视图控制器  4,自定义容器,类似抽屉效果(右滑,弹出左侧边栏)  */#import "BeyondViewController.h"#import "BeyondLoginViewController.h"@interface BeyondViewController ()<BeyondLoginViewControllerDelegate,UIActionSheetDelegate>@end@implementation BeyondViewController- (void)viewDidLoad{    [super viewDidLoad];}// 点击BeyondViewController界面上的登录按钮,切换到BeyondLoginViewController.h进行输入密码帐号登录- (IBAction)wantLogin:(UIButton *)sender {    // 想通过代码以Modal的方式,切换到BeyondLoginViewController控制器,就必须创建实例对象,耦合性太强~    BeyondLoginViewController *loginViewCtrl = [[BeyondLoginViewController alloc]init];    // 设置loginViewCtrl的代理 为当前控制器,因为,在下一个控制器(loginViewCtrl)中,用户输入完用户名和密码之后,会调用代理 的doSomethingWithUsername方法,给它的代理对象(即当前控制器)发消息,参数 就是要传递过来的用户名~    loginViewCtrl.delegate = self;    // 关键,代码,所有控制器都有该方法,展现    [self presentViewController:loginViewCtrl animated:YES completion:^{        NSLog(@"BeyondLogin控制器--出现了");    }];}// 实现下一个控制器中代理方法,因为在下一个控制器(loginViewCtrl)中,用户输入完用户名和密码之后,会调用代理 的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;// 向服务器提交用户名和密码- (IBAction)submit:(UIButton *)sender;// 点击返回按钮,返回到前一个控制器- (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.secureTextEntry = YES;}// 点击返回按钮,返回到前一个控制器- (IBAction)backToHome:(UIBarButtonItem *)sender{    // 关键代码,关闭自身这个modal模态对话框    [self dismissViewControllerAnimated:YES completion:^{        NSLog(@"BeyondLogin控制器,消失了");    }];}// 向服务器提交用户名和密码- (IBAction)submit:(UIButton *)sender {    // 如同 JS 表单验证    if (_username.text.length == 0 || _password.text.length == 0) {        // <#(id<UIActionSheetDelegate>)#>        UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"请输入用户名和密码" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"红色警告" otherButtonTitles:@"其他按钮", 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方式实现




1 0
原创粉丝点击