ios代理模式-(ViewControler之間傳值)

來源:互聯網
上載者:User

標籤:

1,建立Single View Application工程,建立SecondViewController

2,在SecondViewController中設定代理

  

#import <UIKit/UIKit.h>@protocol secondViewControllerDelegate <NSObject>- (NSString *)value;@end@interface SecondViewController : UIViewController@property (nonatomic,assign) id <secondViewControllerDelegate> delegate;@end

 

3,在ViewController中添加UIButton,綁定事件,用於跳轉到SecondViewController,在ViewController中添加UITextField,用於輸入需要傳遞的值

4,在ViewController中添加代理協議,設定SecondViewController的代理為ViewController,實現代理方法

代碼如下:

#import "ViewController.h"#import "SecondViewController.h"@interface ViewController () <secondViewControllerDelegate>{    UITextField *_textField;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //添加按鈕    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];    btn.backgroundColor = [UIColor grayColor];    [btn setTitle:@"ToSecondViewController" forState:UIControlStateNormal];    [btn addTarget:self action:@selector(toSecondViewController) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];        //添加textField    _textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 300, 40)];    _textField.backgroundColor = [UIColor yellowColor];    [self.view addSubview:_textField];}- (void)toSecondViewController {    SecondViewController *second = [[SecondViewController alloc] init];    //設定SecondViewController的代理為ViewController自己    second.delegate = self;    [self presentViewController:second animated:YES completion:nil];}

 //實現代理方法

-(NSString *)value {    return _textField.text;}@end

 

5,在SecondViewController中添加UIButton,綁定事件,用於返回到ViewController

6,在SecondViewController中添加UILabel,用於顯示傳遞的值

代碼如下:

#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    // 添加label用於顯示傳遞的值    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 40)];    label.text = [_delegate value];    label.backgroundColor = [UIColor greenColor];    [self.view addSubview:label];        //添加按鈕    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];    btn.backgroundColor = [UIColor blueColor];    [btn setTitle:@"ToViewController" forState:UIControlStateNormal];    [btn addTarget:self action:@selector(backFirstViewController) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];}- (void)backFirstViewController {    [self dismissViewControllerAnimated:YES completion:nil];}@end

 源碼地址:https://github.com/rokistar/PassValueBetweenViewController

ios代理模式-(ViewControler之間傳值)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.