国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

簡述UIViewControl之間的七種傳值方式~~~

2019-11-14 18:05:04
字體:
供稿:網(wǎng)友

將自己學(xué)習(xí)到的UIViewControl之間傳值的幾種方式在這里做一下總結(jié),希望童鞋們多多支持哈~~~

一.正向傳值方式

    這種方式傳值應(yīng)該是最簡單的方式,我們先來建立兩個視圖控制器暫且稱為OneViewControl和TwoViewControl,然后第一個視圖控制器上面有一個UIButton(按鈕)和一個UIlabel(標(biāo)簽),第二個控制器中有一個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;

}

 

 通俗的說上面的代碼就是讓程序一運行的時候,首先執(zhí)行的是OneViewControl中的代碼,很簡單。

現(xiàn)在我們想要達到的目的就是,點擊OneViewControl中的按鈕時,能把這個按鈕上面的文字傳到TwoViewControl中按鈕上面。

我們在TwoViewControl中.h文件中聲明一個屬性

 

@PRoperty(nonatomic,copy)NSString *str;

 

 在OneViewControl中的按鈕事件中添加如下代碼

 

 

-(void)onClick:(UIButton*)sender{    TwoViewController *twoView = [[TwoViewController alloc]init];     //使用屬性傳值    twoView.str = sender.titleLabel.text;   //跳轉(zhuǎn)到下一個視圖,是否有動畫,為了簡潔,就不寫動畫了      [self presentViewController:twoView animated:YES completion:nil];}

 

 好了~這樣TwoViewControl中按鈕上面的值就跟OneViewControl中按鈕值一樣了.達到了傳值的效果~~~很簡單的啦。

 

二.使用代理傳值(反向傳值)

這次我們在TwoViewControl上面的文本框輸入一些內(nèi)容,然后點擊按鈕,返回到OneViewControl中,將內(nèi)容顯示到OneViewControl的UILabel上。(所謂反向傳值就是從后一個視圖控制器傳值到前一個視圖控制器中)

先在TwoViewControl中.h文件中聲明協(xié)議:并聲明弱引用指針

@protocol TwoViewControllerDelegate<NSObject>//聲明協(xié)議//在接收方調(diào)用-(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];    //回傳數(shù)據(jù)    [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文件加入遵守協(xié)議

 三.通知傳值(反向傳值)

在TwoViewControl中先創(chuàng)建一個通知對象,并發(fā)送通知,在按鈕事件中加入如下代碼

    //第一個參數(shù)是通知的名字,必須填寫    //第二個參數(shù)發(fā)送的對象    //第三個參數(shù)是字典,攜帶信息,沒有信息傳入nil    NSNotification *noti = [NSNotification notificationWithName:@"myNotification" object:self userInfo:@{@"inputstr":tf.text}];        //發(fā)送通知    [[NSNotificationCenter defaultCenter]postNotification:noti];

          [self dismissViewControllerAnimated:YES completion:nil];

 在OneViewControl中加入監(jiān)聽通知的方法及響應(yīng)方法

//3.監(jiān)聽通知-(void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];    //第一個參數(shù)是觀察者是誰    //第二個是調(diào)用的方法    //第三個是監(jiān)聽通知的名字    //通知發(fā)送的對象,nil表示任何對象    [center addObserver:self selector:@selector(receiveNoti:) name:@"myNotification" object:nil];   }//4.響應(yīng)-(void)receiveNoti:(NSNotification*)noti{    UILabel *label = (id)[self.view viewWithTag:1];    label.text = noti.userInfo[@"inputstr"];    }

 應(yīng)該注意的是,通知的名稱兩邊必須一致,不然是接收不到發(fā)送過來的通知

四.使用Block傳值(反向傳值)

 使用Block傳值,先聲明一個無返回值,有一個參數(shù)的Block屬性在TwoViewControl中

@property (nonatomic,copy)void(^returnStrBlock)(NSString*);

 在TwoViewControl中,按鈕事件中加入如下代碼,當(dāng)前視圖調(diào)用block

    UITextField *tf = (id)[self.view viewWithTag:2];    [tf resignFirstResponder];        self.returnStrBlock(tf.text);    [self dismissViewControllerAnimated:YES completion:nil];

 在oneViewControl中按鈕事件設(shè)置回調(diào)的的block函數(shù)

    TwoViewController *two = [[TwoViewController alloc]init];        //設(shè)置回調(diào)的block函數(shù)    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;
//引用聲明在其他文件中的數(shù)據(jù)extern NSString *inputStr;

 我個人是不建議使用這種方式的~~~

六.單例傳值

 這種方式也是比較容易理解的,新建一個類文件.h文件中加入如下代碼

@interface SingletonModel : NSObject@property(nonatomic,strong)NSString *textStr;//聲明單例方法+(SingletonModel *)shareSingletonModel;@end

 在.m文件中實現(xiàn)這個類方法(單例模式大部分都是這樣創(chuàng)建實例的,簡單易懂,反正就一個嘛~~~)

static SingletonModel *shareObj = nil;@implementation SingletonModel+(SingletonModel *)shareSingletonModel{    if(shareObj==nil)    {        shareObj = [[SingletonModel alloc]init];    }    return shareObj;}@end

 然后就很簡單啦,在TwoViewControl中將值傳給單例對象,在OneViewControl中獲取這個值就歐啦~~~

 六.使用AppDelegate傳值

簡單來說,就是 在AppDelegate中聲明一個屬性,然后TwoViewControl中創(chuàng)建一個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;}

 這種方式跟上面的單例模式有著異曲同工之妙,舉個很簡單的例子,兩個人要交換東西,先將東西交給第三個人,再由第三個人轉(zhuǎn)交給兩人,而上面的單例和AppDelegate方式傳值,都是相當(dāng)于那個第三個人。

 

好啦  ~~~~七種方式總算寫完了,從第一個字到最后一個符號,純手打,代碼也是一個個敲的~~~望各位小伙伴多多理解,如果哪里有問題,希望多多交流,如果覺得有用的話,就給個小小的贊啦~~~PS:我這個人很容易滿足的~~~謝謝各位小伙伴哈!

 

 

 

 

 

 

 

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 裕民县| 玉溪市| 泾川县| 即墨市| 通化县| 丹棱县| 锦屏县| 全州县| 那坡县| 襄垣县| 濉溪县| 余江县| 永胜县| 阳山县| 屏南县| 雅江县| 禹城市| 太仓市| 买车| 太白县| 景泰县| 湖北省| 通渭县| 淮阳县| 安多县| 大竹县| 余姚市| 南澳县| 增城市| 灵台县| 巩留县| 五家渠市| 金昌市| 景德镇市| 榆树市| 团风县| 锡林郭勒盟| 共和县| 政和县| 张家界市| 西乌|