一昭齐、控制器之間的正向傳值:
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)友)
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];
}