一.正向傳值方式
這種方式傳值應該是最簡單的方式,我們先來建立兩個視圖控制器暫且稱爲OneViewControl和TwoViewControl,然後第一個視圖控制器上面有一個UIButton(按鈕)和一個UIlabel(標籤),第二個控制器中有一個UIButton和一個UITexField(文本框)。然後我們在AppDelegate加入如下代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
OneViewController *root = [[OneViewController alloc]init];
self.window.rootViewController = root;
return YES;
}
通俗的說上面的代碼就是讓程序一運行的時候,首先執行的是OneViewControl中的代碼,很簡單。
現在我們想要達到的目的就是,點擊OneViewControl中的按鈕時,能把這個按鈕上面的文字傳到TwoViewControl中按鈕上面。
我們在TwoViewControl中.h文件中聲明一個屬性
@PRoperty(nonatomic,copy)NSString *str;
在OneViewControl中的按鈕事件中添加如下代碼
-(void)onClick:(UIButton*)sender
{
TwoViewController *twoView = [[TwoViewController alloc]init];
//使用屬性傳值
twoView.str = sender.titleLabel.text;
//跳轉到下一個視圖,是否有動畫,爲了簡潔,就不寫動畫了
[self presentViewController:twoView animated:YES completion:nil];
}
好了~這樣TwoViewControl中按鈕上面的值就跟OneViewControl中按鈕值一樣了.達到了傳值的效果~~~很簡單的啦。
二.使用代理傳值(反向傳值)
這次我們在TwoViewControl上面的文本框輸入一些內容,然後點擊按鈕,返回到OneViewControl中,將內容顯示到OneViewControl的UILabel上。(所謂反向傳值就是從後一個視圖控制器傳值到前一個視圖控制器中)
先在TwoViewControl中.h文件中聲明協議:並聲明弱引用指針
@protocol TwoViewControllerDelegate<NSObject>
//聲明協議
//在接收方調用
-(void)inputString:(NSString*)textStr;
@end
@interface TwoViewController : UIViewController
//委託方聲明弱引用指針
@property(nonatomic,weak)id<TwoViewControllerDelegate>delegate;
@end
在TwoViewControl中.m的文件中按鈕事件加入如下代碼:
-(void)onClick
{
//找到textField文本
UITextField *tf = (id)[self.view viewWithTag:2];
[tf resignFirstResponder];
//回傳數據
[self.delegate inputString:tf.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
在OneViewControl中的按鈕方法中加入如下代碼
-(void)onClick:(UIButton*)sender
{
TwoViewController *two = [[TwoViewController alloc]init];
two.delegate = self;
[self presentViewController:two animated:YES completion:nil];
}
好了第二種代理傳值就是這樣,~~~別忘記在第一個視圖控制器中.m文件加入遵守協議
三.通知傳值(反向傳值)
在TwoViewControl中先創建一個通知對象,併發送通知,在按鈕事件中加入如下代碼
//第一個參數是通知的名字,必須填寫
//第二個參數發送的對象
//第三個參數是字典,攜帶信息,沒有信息傳入nil
NSNotification *noti = [NSNotification notificationWithName:@"myNotification" object:self userInfo:@{@"inputstr":tf.text}];
//發送通知
[[NSNotificationCenter defaultCenter]postNotification:noti];
[self dismissViewControllerAnimated:YES completion:nil];
在OneViewControl中加入監聽通知的方法及響應方法
//3.監聽通知
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//第一個參數是觀察者是誰
//第二個是調用的方法
//第三個是監聽通知的名字
//通知發送的對象,nil表示任何對象
[center addObserver:self selector:@selector(receiveNoti:) name:@"myNotification" object:nil];
}
//4.響應
-(void)receiveNoti:(NSNotification*)noti
{
UILabel *label = (id)[self.view viewWithTag:1];
label.text = noti.userInfo[@"inputstr"];
}
應該注意的是,通知的名稱兩邊必須一致,不然是接收不到發送過來的通知
四.使用Block傳值(反向傳值)
使用Block傳值,先聲明一個無返回值,有一個參數的Block屬性在TwoViewControl中
@property (nonatomic,copy)void(^returnStrBlock)(NSString*);
在TwoViewControl中,按鈕事件中加入如下代碼,當前視圖調用block
UITextField *tf = (id)[self.view viewWithTag:2];
[tf resignFirstResponder];
self.returnStrBlock(tf.text);
[self dismissViewControllerAnimated:YES completion:nil];
在oneViewControl中按鈕事件設置回調的的block函數
TwoViewController *two = [[TwoViewController alloc]init];
//設置回調的block函數
two.returnStrBlock = ^(NSString* inputStr)
{
UILabel *label = (id)[self.view viewWithTag:1];
label.text = inputStr;
};
[self presentViewController:two animated:YES completion:nil];
這個寫的比較簡單,但是效果是可以達到的~~~
五.使用全局變量傳值(全局變量傳值)
這種方式我覺得是很low的一種方式,也非常簡單,在TwoViewControl和oneViewControl中分別加入下面兩句代碼就可以了
NSString *inputStr;
//引用聲明在其他文件中的數據
extern NSString *inputStr;
我個人是不建議使用這種方式的~~~
六.單例傳值
這種方式也是比較容易理解的,新建一個類文件.h文件中加入如下代碼
@interface SingletonModel : NSObject
@property(nonatomic,strong)NSString *textStr;
//聲明單例方法
+(SingletonModel *)shareSingletonModel;
@end
在.m文件中實現這個類方法(單例模式大部分都是這樣創建實例的,簡單易懂,反正就一個嘛~~~)
static SingletonModel *shareObj = nil;
@implementation SingletonModel
+(SingletonModel *)shareSingletonModel
{
if(shareObj==nil)
{
shareObj = [[SingletonModel alloc]init];
}
return shareObj;
}
@end
然後就很簡單啦,在TwoViewControl中將值傳給單例對象,在OneViewControl中獲取這個值就歐啦~~~
六.使用AppDelegate傳值
簡單來說,就是 在AppDelegate中聲明一個屬性,然後TwoViewControl中創建一個AppDelegate對象
AppDelegate *appdelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appdelegate.textStr = tf.text;
然後在OneViewControl中
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
AppDelegate *appdelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
UILabel *label = (id)[self.view viewWithTag:1];
label.text = appdelegate.textStr;
}