用協(xié)議代理實(shí)現(xiàn)兩個(gè)界面?zhèn)髦?/h2>

界面二的.m文件

// 制定協(xié)議

import@protocol WTwoViewControllerDelegate

// 聲明協(xié)議方法

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

?@end?

@interface WTwoViewController : UIViewController?

// 聲明一個(gè)Block類型的屬性

@property (nonatomic, unsafe_unretained) iddelegate;?

// 聲明一個(gè)UITextField類型的全局變量*txtValue

@property (nonatomic, strong) IBOutlet UITextField *txtValue;?

// 聲明Button的點(diǎn)擊事件方法

- (IBAction)pressChange:(id)sender;?

@end

// 界面二的.h文件

?- (IBAction)pressChange:(id)sender 方法中把代理派發(fā)出去,順便把窗口給銷毀,

代碼如下:- (IBAction)pressChange:(id)sender {???

// 代理傳值

? [self.delegate changeValue:self.txtValue.text];??

? [self dismissViewControllerAnimated:YES completion:nil];?

}?

界面二中的設(shè)置已經(jīng)完成,接下要在界面一中調(diào)用界面二的頭文件,并實(shí)現(xiàn)界面二協(xié)議中所制定的方法。

首先在WViewController.h中實(shí)現(xiàn)代理,

代碼如下:

#import "WTwoViewController.h" // 包含頭文件

// 聲明方法

@interface WViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *lblValue;

- (IBAction)pressCasting:(id)sender;

@end

其在WViewController.m的?

- (IBAction)pressCasting:(id)sender 方法中調(diào)用WTwoViewController,

并設(shè)置代理的回調(diào)方法,代碼如下:

- (IBAction)pressCasting:(id)sender {

WTwoViewController *controller = [[WTwoViewController alloc]initWithNibName:@"WTwoViewController" bundle:nil];

controller.delegate = self;

[self presentViewController:controller animated:YES completion:nil];

}

- (void)changeValue:(NSString *)value{

// 改變UILabel的值

self.lblValue.text = value;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容