iOS-ViewController之間的傳值

一、控制器之間的正向傳值:
1、屬性傳值
1.1、通過storyboard創(chuàng)建控制器
//Segue
定義:Storyboard上每一根用來界面跳轉(zhuǎn)的線,都是一個UIStoryboardSegue對象
屬性:

//Segue ID
NSString *identifier; 
//來源控制器
UIViewController *sourceViewController;   
//目標(biāo)控制器
UIViewController *destinationViewController;

類型:
(1)Show
根據(jù)當(dāng)前屏幕中的內(nèi)容,在master area或者detail area中展示內(nèi)容。
例如:如果app當(dāng)前同時顯示master和detail視圖,內(nèi)容將會壓入detail區(qū)域。
如果app當(dāng)前僅顯示master或者detail視圖,內(nèi)容則壓入當(dāng)前視圖控制器堆棧中的頂層視圖。
(2)Show Detail
在detail area中展現(xiàn)內(nèi)容。
例如:即使app同時顯示master和detail視圖,那么內(nèi)容將被壓入detail區(qū)域
如果app當(dāng)前僅顯示Master或者detail視圖,那么內(nèi)容將替換當(dāng)前視圖控制器堆棧中的頂層視圖。
(3)Present Modally
使用模態(tài)展示內(nèi)容。屬性面板中提供presentation style (UIModalPresentationStyle)與 transition style (UIModalTransitionStyle)兩種選項
(4)Present as Popover
在某個現(xiàn)有視圖中的錨點(diǎn)處使用彈出框展示內(nèi)容。這個選項可指定顯示在彈出框視圖一邊上的箭頭可用方向,同時也是指定錨點(diǎn)視圖的一個選項。
(翻譯來自網(wǎng)友)


image.png

image.png

1.2、通過代碼創(chuàng)建控制器
首先創(chuàng)建控制器AViewController和BViewController,點(diǎn)擊控制器A中的button跳轉(zhuǎn)到B控制器傳值
,B控制器有個name屬性,在跳轉(zhuǎn)的方法里傳值

-(void)btnClick{
BViewController *BVC = [[BViewController alloc] init];
BVC.name = @"要傳的值";
[self.navigationController pushViewController:BVC animated:YES ];
}

2、通知
2.1.在A控制器發(fā)送通知

//需要傳的參數(shù)
NSDictionary *dict = @{@"key":value};
//發(fā)送通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"sendDataToTwoVc" object:nil userInfo:dict];

2.2、在B控制器監(jiān)聽通知

//監(jiān)聽通知(通知名字一定要寫正確)
 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setData:) name:@"sendDataToTwoVc" object:nil];
//監(jiān)聽通知后調(diào)用
-(void)setData:(NSNotification *)notification{
     NSLog(@"dict - %@",notification.userInfo);
}
//移除需要觀察的通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
//注意:如果發(fā)送的通知指定了object對象,那么觀察者接收的通知設(shè)置的object對象與其一樣,才會接收到通知,但是接收通知如果將這個參數(shù)設(shè)置為了nil,則會接收一切通知。

3、NSUserDefault

//設(shè)置需要保存的對象
[[NSUserDefaults standardUserDefaults] setObject:@"value" forKey:@"key"];
//同步磁盤
[[NSUserDefaults standardUserDefaults] synchronize];
//獲取保存的對象
[[NSUserDefaults standardUserDefaults]objectForKey:@"key"];

二、控制器之間的逆向傳值:
1、block
有兩個控制器分別是AViewController和BViewController,點(diǎn)擊A控制器中的按鈕跳轉(zhuǎn)到B控制器,點(diǎn)擊B控制器的屏幕回到A控制器,并把B控制器textField.text傳到A控制器中,改變A控制器中btn的title.

//A控制器.m文件
#import "AViewController.h"
#import "BViewController.h"

@implementation AViewController
//button的點(diǎn)擊事件
- (IBAction)clickDown:(id)sender {
    BViewController *vc = [[BViewController alloc] init];
    vc.backBlock = ^(NSString *titleStr) {
            [self.btn setTitle:titleStr forState:UIControlStateNormal];
        };
    [self.navigationController pushViewController:vc animated:YES];
}
//B控制器.h文件
#import <UIKit/UIKit.h>

@interface BViewController : UIViewController
@property(nonatomic,copy)void(^backBlock)(NSString *titleStr);
@end

//B控制器.m文件
#import "BViewController.h"

@interface BViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end

@implementation BViewController

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.backBlock(self.textField.text);
}

2、代理
在B控制器中指定協(xié)議,讓A控制器遵守協(xié)議設(shè)置A控制器為代理并實現(xiàn)協(xié)議中的方法

A控制器.m文件
#import "AViewController.h"
#import "BViewController.h"

@interface AViewController ()<BViewControllerDelegate>

@end

@implementation AViewController

- (IBAction)clickDown:(id)sender {
//在使用block和代理時注意循環(huán)引用問題,當(dāng)一個對象持有block,而該block又持有該對象時,類似下面的偽代碼會照成循環(huán)引用,__weak typeof(self) weakself=self;
    __weak typeof(self) weakSelf = self;
    BViewController *vc = [[BViewController alloc] init];
    vc.delegate = weakSelf;
    [self.navigationController pushViewController:vc animated:YES];
}
//協(xié)議中的方法:
-(void)sendString:(NSString *)str{
//B控制器傳過來的值str
    [self.btn setTitle:str forState:UIControlStateNormal];
}
//A控制器.h文件
#import <UIKit/UIKit.h>

@protocol BViewControllerDelegate <NSObject>
-(void)sendString:(NSString *)str;
@end
@interface BViewController : UIViewController
@property(weak,nonatomic)id<BViewControllerDelegate>delegate;
@end

//A控制器.m文件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.delegate sendString:self.textField.text];
    [self.navigationController popViewControllerAnimated:YES];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容