YIem`s Blog -心比天高命比纸薄-链接找不到的请在站内搜索内容!

2015-15-16- UI8_协议传值

//
// AppDelegate.h
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (retain, nonatomic) UIWindow *window;

@end


//
// AppDelegate.m
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "AppDelegate.h"

import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

@end



//
// RootViewController.h
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end


//
// RootViewController.m
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "RootViewController.h"

import "SecondViewController.h"

warning 4.签订协议

@interface RootViewController () <PassDelegate>

@property (nonatomic, retain)UITextField *textField;
@end

@implementation RootViewController

warning 5.设置代理人(创建对象之后 push页面之前)

secVC.delegate = self;
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];

}

warning 6.实现协议方法

// 参数中为 第二页输入框的texz字符串

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

*/

@end



//
// SecondViewController.h
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

warning 界面通信之协议传值(2->1)

// 2.页面上的值想要改变1页面上的控件 无法直修改 设置1页面对象为代理人 帮助修改

warning 1.声明协议

@protocol PassDelegate <NSObject>

@end

@interface SecondViewController : UIViewController
// 1.在第二页定义保存数据的属性
@property (nonatomic, copy)NSString *str;

warning 2.设置代理人属性

@property (nonatomic, assign)id<PassDelegate> delegate;
@end


//
// SecondViewController.m
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "SecondViewController.h"

import "ThirdViewController.h"

@interface SecondViewController ()<pp>
@property (nonatomic, retain)UITextField *textField;
@end

@implementation SecondViewController

// 第三页

//

thVC.delegate = self;
[self.navigationController pushViewController:thVC animated:YES];

[thVC release];

}

warning 3.代理人调用协议方法(页面前返回)

[self.delegate passValue:_textField.text];

//


[self.navigationController popToRootViewControllerAnimated:YES];

}

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

*/

@end



//
// ThirdViewController.h
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

@protocol pp <NSObject>

@end
@interface ThirdViewController : UIViewController
@property (nonatomic, copy)NSString *thc;
@property (nonatomic, assign) id <pp>delegate;
@end


//
// ThirdViewController.m
// UI8_协议传值
//
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "ThirdViewController.h"

@interface ThirdViewController ()
@property (nonatomic, retain)UITextField *textField;
@end

@implementation ThirdViewController

// self.textField.text = self.thc;

self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
_textField.backgroundColor = [UIColor yellowColor];

// [_textField addTarget:self action:@selector(aa) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:_textField];
[_textField release];
 _textField.text = self.thc;

//


UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
button.backgroundColor = [UIColor redColor];

[self.view addSubview:button];

[button addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];
[button release];

}

{

//
[self.delegate pass:_textField.text];

//
[self.navigationController popViewControllerAnimated:YES];

}

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

*/

@end

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »