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

來源:互聯網
上載者:User

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

將自己學習到的UIViewControl之間傳值的幾種方式在這裡做一下總結,希望童鞋們多多支援哈~~~

一.正向傳值方式

    這種方式傳值應該是最簡單的方式,我們先來建立兩個視圖控制器暫且稱為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;}

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

 

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

 

 

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.