iOS_16_開關控制器_modal_代碼方法

最後效果圖:atom



main.storyboardspa



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.hcode

//
//  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.xiborm


BeyondLoginViewController.hserver

//
//  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方式實現blog




版權聲明:本文博客原創文章,博客,未經贊成,不得轉載。索引

相關文章
相關標籤/搜索