iOS開發(fā)之界面通信(界面?zhèn)髦担\談

本文簡單介紹幾種界面通信的方式,給出相應的示例丁屎。

1. 屬性傳值

屬性傳值是最簡單的一種傳值方式负饲,通常用于從前向后傳值瘫筐,即在第二個視圖控制器獲得第一個視圖控制器的部分信息蜜暑。

示例:創(chuàng)建兩個UIViewController,在第一個頁面的UITextField里輸入文字策肝,跳轉到第二個頁面之后肛捍,在第二個頁面的UILabel上顯示出來。

屬性傳值示例

第一個頁面:

FirstViewController.m文件:

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()
@property (nonatomic, strong) UITextField *firstTextField;
@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationItem.title = @"第一頁";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一頁" style:UIBarButtonItemStyleDone target:self action:@selector(nextVC:)];
    
    self.firstTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 214, 40)];
    _firstTextField.backgroundColor = [UIColor whiteColor];
    _firstTextField.placeholder = @"輸入要傳入下一頁面的值";
    [self.view addSubview:_firstTextField];
    
}

在觸發(fā)的點擊事件中 將第一個頁面的輸入框中輸入的信息賦值給第二個頁面的label之众,然后跳轉到第二個頁面:

-(void)nextVC:(UIBarButtonItem *)sender{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    secondVC.stringFromFirstVC = _firstTextField.text;
    [self.navigationController pushViewController:secondVC animated:YES];
}

@end

第二個頁面:

SecondViewController.h文件:

暴露一個屬性拙毫,用于傳值。

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
@property (nonatomic, strong) NSString *stringFromFirstVC;
@end

SecondViewController.m文件:

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"第二頁";
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back:)];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 214, 40)];
    label.text = self.stringFromFirstVC;
    [self.view addSubview:label];
}

-(void)back:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

@end

2. 代理傳值

定義一個用來傳值的協(xié)議棺禾,設置代理去執(zhí)行傳值缀蹄。
示例:創(chuàng)建兩個UIViewController,在第二個頁面的UITextField里輸入文字膘婶,跳轉回第一個頁面之后缺前,在第一個頁面的UILabel上顯示出來。

從第一頁進入第二頁
在第二個頁面輸入文字悬襟,跳轉回第一個頁面

第一個頁面:

FirstViewController.m文件:

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()<PassValueDelegate>   //遵守協(xié)議
@property (nonatomic, strong) UILabel *label;
@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cyanColor];
    self.navigationItem.title = @"第一頁";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一頁" style:UIBarButtonItemStyleDone target:self action:@selector(nextVC:)];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 300, 214, 40)];
    _label.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_label];
}

觸發(fā)的點擊事件:設置代理后跳轉到第二個頁面:

-(void)nextVC:(UIBarButtonItem *)sender{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    //將第一個頁面指定為第二個頁面的代理!
    secondVC.delegate = self;
    [self.navigationController pushViewController:secondVC animated:YES];
}

實現(xiàn)代理方法:

-(void)passValue:(NSString *)value{
    _label.text = value;
}

@end

第二個頁面:

SecondViewController.h文件:

定義一個用來傳值的協(xié)議衅码,聲明代理屬性

#import <UIKit/UIKit.h>

@protocol PassValueDelegate <NSObject>

-(void)passValue:(NSString *)value;

@end

@interface SecondViewController : UIViewController

//聲明代理屬性,其中<PassValueDelegate>指定其遵守的協(xié)議
@property(nonatomic,assign)id<PassValueDelegate>delegate;

@end

SecondViewController.m文件:

#import "SecondViewController.h"

@interface SecondViewController ()
@property (nonatomic, strong) UITextField *textField;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"第二頁";
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back:)];

    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 214, 40)];
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.placeholder = @"輸入要傳入上一頁面的值";
    [self.view addSubview:_textField];
    
}

-(void)back:(UIBarButtonItem *)sender{
    //代理去執(zhí)行傳值!!!
    [_delegate passValue:_textField.text];
    //跳轉回第一頁
    [self.navigationController popViewControllerAnimated:YES];
}

3. block傳值

使用block實現(xiàn)回調功能的優(yōu)點是比較清晰古胆,簡化代碼肆良。
注意:在block實現(xiàn)部分筛璧,不要直接使用self逸绎、實例變量、屬性夭谤,因為會造成循環(huán)引用棺牧。
示例:創(chuàng)建兩個UIViewController,在第二個頁面的UITextField里輸入文字朗儒,跳轉回第一個頁面之后颊乘,在第一個頁面的UILabel上顯示出來采呐。

從第一頁進入第二頁
在第二個頁面輸入文字瘪松,跳轉回第一個頁面

第一個頁面:

FirstViewController.m文件:

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()
@property(nonatomic,strong)UILabel *label;
@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor blueColor];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 300, 214, 50)];
    _label.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_label];
}

添加觸摸事件,模態(tài)出下一個視圖(用別的方式也可以)

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    //block的實現(xiàn)
    __weak FirstViewController *temp = self;
    secondVC.block = ^(NSString *string){
        //通過回調將傳進來的字符串賦值給_label
        temp.label.text = string;
    };
    //跳轉到第二個頁面
    [self presentViewController:secondVC animated:YES completion:nil];
}

@end

第二個頁面:

SecondViewController.h文件:

在這里定義一個block陕凹,聲明block屬性恳不。
ARC中block的語義設置使用strong即可檩小,MRC中block的語義設置是copy(把block從棧區(qū)拷貝到堆區(qū),使用完之后烟勋,在dealloc中釋放)规求。

#import <UIKit/UIKit.h>
//定義有參無返回值的匿名函數(shù)(傳遞字符串)
typedef void (^PassValueBlock)(NSString *);

@interface SecondViewController : UIViewController
@property (nonatomic, strong) PassValueBlock block;
@end

SecondViewController.m文件:

#import "SecondViewController.h"

@interface SecondViewController ()
@property (nonatomic, strong) UITextField *textField;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 214, 50)];
    _textField.placeholder = @"block傳值";
    _textField.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_textField];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //調用block
    self.block(_textField.text);   
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末筐付,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子阻肿,更是在濱河造成了極大的恐慌瓦戚,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件丛塌,死亡現(xiàn)場離奇詭異较解,居然都是意外死亡,警方通過查閱死者的電腦和手機姨伤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門哨坪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人乍楚,你說我怎么就攤上這事当编。” “怎么了徒溪?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵忿偷,是天一觀的道長。 經常有香客問我臊泌,道長鲤桥,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任渠概,我火速辦了婚禮茶凳,結果婚禮上,老公的妹妹穿的比我還像新娘播揪。我一直安慰自己贮喧,他們只是感情好,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布猪狈。 她就那樣靜靜地躺著箱沦,像睡著了一般。 火紅的嫁衣襯著肌膚如雪雇庙。 梳的紋絲不亂的頭發(fā)上谓形,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天,我揣著相機與錄音疆前,去河邊找鬼寒跳。 笑死,一個胖子當著我的面吹牛竹椒,可吹牛的內容都是我干的童太。 我是一名探鬼主播,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼康愤!你這毒婦竟也來了儡循?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤征冷,失蹤者是張志新(化名)和其女友劉穎择膝,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體检激,經...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡肴捉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了叔收。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片齿穗。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖饺律,靈堂內的尸體忽然破棺而出窃页,到底是詐尸還是另有隱情,我是刑警寧澤复濒,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布脖卖,位于F島的核電站,受9級特大地震影響巧颈,放射性物質發(fā)生泄漏畦木。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一砸泛、第九天 我趴在偏房一處隱蔽的房頂上張望十籍。 院中可真熱鬧,春花似錦唇礁、人聲如沸勾栗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽械姻。三九已至妒蛇,卻和暖如春机断,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背绣夺。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工吏奸, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人陶耍。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓奋蔚,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子泊碑,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內容