iOS頁面間傳值的方式(Delegate/NSNotification/Block/NSUserDefault/單例)


 

iOS頁面間傳值的方式(NSUserDefault/Delegate/NSNotification/Block/單例)

實現了以下iOS頁面間傳值:1.委托delegate方式;2.通知notification方式;3.block方式;4.UserDefault或者文件方式;5.單例模式方式;6.通過設置屬性,實現頁面間傳值

在iOS開發中,我們經常會遇到頁面間跳轉傳值的問題,現歸納總結一下:

情況1:A頁面跳轉到B頁面

方法:

在B頁面的控制器中,編寫對應的屬性,在A頁面跳轉到B頁面的地方,給B的屬性賦值即可

//SecondViewController.h

@property(nonatomic) NSInteger flag;//當前系統標示(0:其他傳值方式;1:block傳值方式)

在A頁面的試圖控制器中

 

//RootViewController.m

- (IBAction)showSecondView:(id)sender { SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; second.delegate = self; second.flag = 0; [self presentViewController:second animated:YES completion:nil]; }

 

情況2:A頁面跳轉到B頁面,B頁面再跳轉回A頁面

主流方案:

(1)通過委托delegate的方式實現

 

設置協議及方法
 
 
 
 
 
 
               
//SecondViewController.h

@protocol
secondViewDelegate -(void)showName:(NSString *)nameString; @end

 

設置代理(為防止循環引用,此處采用了weak)

 

 
          
//SecondViewController.h

@interface
SecondViewController : UIViewController @property (nonatomic, weak)id<secondViewDelegate> delegate; @property (nonatomic, copy) ablock block; @end
 
 調用
//SecondViewController.m
- (IBAction)delegateMethod:(id)sender { if ([self notEmpty]) { [self.delegate showName:self.nameTextField.text]; [self dismissViewControllerAnimated:YES completion:nil]; }else{ [self showAlert]; } }

顯示
 
//RootViewController.m
-(void)showName:(NSString *)nameString{ self.nameLabel.text = nameString; }

 

最重要也是最容易忽略的,就是一定要設置delegate的指向。
 
 

(2)通過通知notification的方式實現

在B頁面的控制器中,發送通知:
//SecondViewController.m
- (IBAction)notificationMethod:(id)sender {
    if ([self notEmpty]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeNameNotification" object:self userInfo:@{@"name":self.nameTextField.text}];
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self showAlert];
    }
}

 

在A頁面的控制器中,注冊通知:

//RootViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeNameNotification:) name:@"ChangeNameNotification" object:nil];
}

當我們不使用時,要記得刪掉通知:

//RootViewController.m
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

 

 調用,顯示

//RootViewController.m

-(void)ChangeNameNotification:(NSNotification*)notification{
    NSDictionary *nameDictionary = [notification userInfo];
    self.nameLabel.text = [nameDictionary objectForKey:@"name"];
}

 

(3)block方式實現

block介紹:http://blog.csdn.net/totogo2010/article/details/7839061

鏈接一篇描述block回調挺有意思的文章: http://blog.csdn.net/mobanchengshuang/article/details/11751671

分析:

在B試圖控制器中,定義一個block,參數為字符串

//SecondViewController.h
typedef void (^ablock)(NSString *str);
//SecondViewController.h

@property (nonatomic, copy) ablock block;

在B試圖控制器中,當輸入名字,點擊對應的確定按鈕后

- (IBAction)blockMethod:(id)sender {
    if ([self notEmpty]) {
        if (self.block) {
            self.block(self.nameTextField.text);
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }else{
        [self showAlert];
    }
}

在A試圖顯示,回調block

- (IBAction)showSecondWithBlock:(id)sender {
    SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self presentViewController:second animated:YES completion:nil];
    second.block = ^(NSString *str){
        self.nameLabel.text = str;
    };
}

 

(4)KVO方式實現 

KVO實現原理介紹:http://blog.csdn.net/kesalin/article/details/8194240

在A視圖中,編寫以下代碼 

//A視圖
//一個指向B視圖的成員變量
@property (nonatomic, strong) SecondViewController *second;

//在A視圖跳轉到B視圖的地方添加如下代碼
    self.second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self.second addObserver:self forKeyPath:@"userName" options:NSKeyValueObservingOptionNew context:nil];
    [self presentViewController:self.second animated:YES completion:nil];


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//此處監聽key對應值的變化情況
    if ([keyPath isEqualToString:@"userName"]) {
        self.myLabel.text = self.second.userName;
    }
}

//清理觀察
- (void)dealloc
{
    [self.second removeObserver:self forKeyPath:@"userName"];
}

 

 在B視圖編寫以下代碼

//在B視圖
//.h文件

@property (nonatomic, strong) NSString *userName;//待監聽的成員變量

//可以在兩處修改userName的值。一個是設置textfield的UITextFieldDelegate。實現一下方法

-(void)textFieldDidEndEditing:(UITextField *)textField{
self.userName = self.myField.text;
}

//或者在B視圖,點擊確定按鈕,跳轉回A視圖的時候,修改userName的值也可以

- (IBAction)buttonPressed:(id)sender {
    self.userName = self.myField.text;
    [self dismissViewControllerAnimated:YES completion:nil];
}

 

 

 

在查閱資料的過程中,我還看到了以下幾種方案:

(1)使用SharedApplication,定義一個變量來傳遞(感覺和單例的方式一樣)

 

(2)使用文件,或者NSUserdefault來傳遞

 

//通過文件或者UserDefault方式存值(感覺不太適合此類傳值,如果要用文件或者UserDefault方式存值的話,可以考慮此方式)
- (IBAction)userDefaultMethod:(id)sender {
    if ([self notEmpty]) {
        [[NSUserDefaults standardUserDefaults] setObject:self.nameTextField.text forKey:@"myNameText"];
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self showAlert];
    }
}

在A試圖控制器顯示

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //如果想測試通過UserDefault方式傳值或者通過單例方式傳值,取消以下注釋即可
/*
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"] length] != 0) {
        self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"myNameText"];
    }
    DataSource *dataSource = [DataSource sharedDataSource];
    if ([dataSource.myName length] != 0) {
        self.nameLabel.text = dataSource.myName;
        dataSource.myName = @"";
    }
*/
}

 

(3)通過一個單例的class來傳遞

B試圖控制器

//通過單例方式傳值(感覺不太適合此類傳值,如果要用單例方式傳值的話,可以考慮此方式)
- (IBAction)singletonMethod:(id)sender {
    if ([self notEmpty]) {
        DataSource *dataSource = [DataSource sharedDataSource];
        dataSource.myName = self.nameTextField.text;
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self showAlert];
    }
}

A試圖控制器顯示

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //如果想測試通過UserDefault方式傳值或者通過單例方式傳值,取消以下注釋即可
/*
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"] length] != 0) {
        self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"myNameText"];
    }
    DataSource *dataSource = [DataSource sharedDataSource];
    if ([dataSource.myName length] != 0) {
        self.nameLabel.text = dataSource.myName;
        dataSource.myName = @"";
    }
*/
}
@end

這里面用到了單例模式,編寫了DataSource這個類,存放數據

//
//  DataSource.h
//  TestCallBack
//
//  Created by csdc-iMac on 14-7-17.
//  Copyright (c) 2014年 JuneWang. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface DataSource : NSObject
@property (nonatomic, strong) NSString *myName;
+(DataSource*)sharedDataSource;
@end 

 

//
//  DataSource.m
//  TestCallBack
//
//  Created by csdc-iMac on 14-7-17.
//  Copyright (c) 2014年 JuneWang. All rights reserved.
//

#import "DataSource.h"

@implementation DataSource
+(DataSource *)sharedDataSource{
    static DataSource *dataSource = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        dataSource = [DataSource new];
    });
    return dataSource;
}
@end

 

程序運行截圖

A視圖:

B視圖

當輸入姓名,並點擊對應的確認按鈕后,會回到A視圖,並顯示在B視圖中輸入的姓名

 

PS:用全局變量的方式也可以實現頁面傳值的效果。

 

祝:玩得開心,有什么別的辦法或者不正確的地方,歡迎指正。

如果寫得不詳細,可以通過源碼分析。

 

 參考:http://blog.csdn.net/cocoarannie/article/details/11857141

http://www.cnblogs.com/heri/archive/2013/03/18/2965815.html

 

源碼地址:https://github.com/wangtao169447/PassValue

轉載請注明出處:http://www.cnblogs.com/JuneWang/p/3850859.html 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM