ios实现页面间传值
2016-09-07 11:30
155 查看
1.简单的传值,使用类、属性
在viewcontrol2中声明一个接受传值的变量(需要在.h中声明)。
使用:在跳转时调用----代码如下:
TwoActivity *t=[[TwoActivity
alloc]init];
t.s=@"sb";
self.delegate=t;
[self.navigationController
pushViewController:t animated:YES];
2.使用代理传值
在viewcontrol1中声明协议,并在属性中持有改协议,在m中实现该协议,在跳转时为协议注明代理为跳转到的viewcontrol,后调用协议中方法,即可实现传值,viewcontrol2也必须实现该协议,代码如下:
viewcontrol1.h:
#import <UIKit/UIKit.h>
@protocol MyDelegate
-(void)sendValue:(NSString*)values;
@end
@interface ViewController :
UIViewController
@end
============
#viewcontrol1.m
#import "ViewController.h"
#import "TwoActivity.h"
@interface ViewController ()
@property (retain,nonatomic)
id <MyDelegate> delegate;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
UIView *back= [[UIView
alloc]initWithFrame:CGRectMake(0,
0, [[UIScreen
mainScreen] bounds].size.width, [[UIScreen
mainScreen] bounds].size.height)];
back.backgroundColor=[UIColor
blueColor];
[self.view
addSubview:back];
// Do any additional setup after loading the view, typically from a nib.
TwoActivity *t=[[TwoActivity
alloc]init];
self.delegate=t;
[self.delegate
sendValue:@"test"];
[self.navigationController
pushViewController:t animated:YES];
}
====================
viewcontrol2.m
#import "TwoActivity.h"
#import "ViewController.h"
@interface TwoActivity ()<MyDelegate>
@end
@implementation TwoActivity
- (void)viewDidLoad {
[super
viewDidLoad];
NSLog(@"%@",self.s);
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)sendValue:(NSString *)values{
//这里方法的参数就是传递过来的数据
NSLog(@"%@",values);
}
在viewcontrol2中声明一个接受传值的变量(需要在.h中声明)。
使用:在跳转时调用----代码如下:
TwoActivity *t=[[TwoActivity
alloc]init];
t.s=@"sb";
self.delegate=t;
[self.navigationController
pushViewController:t animated:YES];
2.使用代理传值
在viewcontrol1中声明协议,并在属性中持有改协议,在m中实现该协议,在跳转时为协议注明代理为跳转到的viewcontrol,后调用协议中方法,即可实现传值,viewcontrol2也必须实现该协议,代码如下:
viewcontrol1.h:
#import <UIKit/UIKit.h>
@protocol MyDelegate
-(void)sendValue:(NSString*)values;
@end
@interface ViewController :
UIViewController
@end
============
#viewcontrol1.m
#import "ViewController.h"
#import "TwoActivity.h"
@interface ViewController ()
@property (retain,nonatomic)
id <MyDelegate> delegate;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
UIView *back= [[UIView
alloc]initWithFrame:CGRectMake(0,
0, [[UIScreen
mainScreen] bounds].size.width, [[UIScreen
mainScreen] bounds].size.height)];
back.backgroundColor=[UIColor
blueColor];
[self.view
addSubview:back];
// Do any additional setup after loading the view, typically from a nib.
TwoActivity *t=[[TwoActivity
alloc]init];
self.delegate=t;
[self.delegate
sendValue:@"test"];
[self.navigationController
pushViewController:t animated:YES];
}
====================
viewcontrol2.m
#import "TwoActivity.h"
#import "ViewController.h"
@interface TwoActivity ()<MyDelegate>
@end
@implementation TwoActivity
- (void)viewDidLoad {
[super
viewDidLoad];
NSLog(@"%@",self.s);
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)sendValue:(NSString *)values{
//这里方法的参数就是传递过来的数据
NSLog(@"%@",values);
}
相关文章推荐
- iOS 使用代理实现页面传值
- IOS 委托代理(delegate)实现页面传值
- IOS 委托代理(delegate)实现页面传值
- iOS开发 使用Block实现两个页面互相传值
- iOS中详解Block作为property属性实现页面之间传值
- iOS 使用代理实现页面传值
- IOS 委托代理(delegate)实现页面传值
- iOS delegate 实现页面之间的传值
- IOS 委托代理(delegate)实现页面传值
- iOS 使用代理实现页面传值
- IOS block 块实现简单的页面跳转传值
- 用协议实现ios页面间传值
- IOS委托,实现两个UIView相互传值
- IOS页面滚动指示器实现实例
- iOS 用delegate方式实现页面之间的传值
- iOS delegate 实现页面之间的传值
- 【转】用JS实现弹出模式对话框,实现页面传值
- Google Analytics in IOS(二)——实现页面统计和事件追踪
- 父页面向子页面传值的问题 纯js实现 不拼url传参
- 不同页面之间通过传递控件实现页面传值