ios中传值的方式有哪些?怎么理解中介传值
2个回答
推荐于2016-05-02 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517195
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
属性传值 将A页面所拥有的信息通过属性传递到B页面使用
B页面定义了一个naviTitle属性,在A页面中直接通过属性赋值将A页面中的值传到B页面。
A页面DetailViewController.h文件
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
@interface RootViewController :UIViewController<ChangeDelegate>
{
UITextField *tf;
}
@end
A RootViewController.m页面实现文件
#import "RootViewController.h"
#import "DetailViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
//核心代码
-(void)loadView
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 30);
[btn setTitle:@"Push" forState:0];
[btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)pushAction:(id)sender
{
tf = (UITextField *)[self.viewviewWithTag:1000];
//导航push到下一个页面
//pushViewController 入栈引用计数+1,且控制权归系统
DetailViewController *detailViewController = [[DetailViewControlleralloc]init];
//属性传值,直接属性赋值
detailViewController.naviTitle =tf.text;
//导航push到下一个页面
[self.navigationControllerpushViewController:detailViewController animated:YES];
[detailViewControllerrelease];
}
B页面DetailViewController.h文件
#import <UIKit/UIKit.h>
@interface DetailViewController :UIViewController
{
UITextField *textField;
NSString *_naviTitle;
}
@property(nonatomic,retain)NSString *naviTitle;
@end
B页面.m实现文件
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
@synthesize naviTitle =_naviTitle;
-(void)loadView
{
self.view = [[[UIViewalloc]initWithFrame:CGRectMake(0,0, 320,480)]autorelease];
self.title = self.naviTitle ;
}
代理传值
A页面push到B页面,如果B页面的信息想回传(回调)到A页面,用用代理传值,其中B定义协议和声明代理,A确认并实现代理,A作为B的代理
A页面RootViewController.h文件
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
@interface RootViewController : UIViewController<ChangeDelegate>
{
UITextField *tf;
}
@end
A页面RootViewController.m实现文件
#import "RootViewController.h"
#import "DetailViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
//核心代码
-(void)loadView
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 30);
[btn setTitle:@"Push" forState:0];
//A页面push到B页面
[btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)pushAction:(id)sender
{
tf = (UITextField *)[self.view viewWithTag:1000];
//导航push到下一个页面
//pushViewController 入栈引用计数+1,且控制权归系统
DetailViewController *detailViewController = [[DetailViewController alloc]init];
//代理传值
detailViewController.delegate =self;//让其自身作为代理人
//导航push到下一个页面
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
//实现代理方法
-(void)changeTitle:(NSString *)aStr
{
tf = (UITextField *)[self.view viewWithTag:1000];
tf.text = aStr;//将从B页面传入的参数赋给A页面中的TextField
tf.text = aStr;
}
B页面DetailViewController.m文件
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
{
UITextField *textField;
//定义代理
id<ChangeDelegate>_delegate;
}
@property(nonatomic,assign)id<ChangeDelegate> delegate;
@end
//定义协议
@protocol ChangeDelegate <NSObject>
-(void)changeTitle:(NSString *)aStr;//协议方法
@end
B页面DetailViewController.h实现文件
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
-(void)loadView
{
self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
UIBarButtonItem *doneItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(doneAction:)];
self.navigationItem.rightBarButtonItem = doneItem;
[doneItemrelease];
}
//pop回前一个页面
-(void)doneAction:(id)sender
{
if (self.delegate && [self.delegaterespondsToSelector:@selector(changeTitle:)])//若代理存在且响应了changeTitle这个方法
{
//[self.delegate changeTitle:textField.text];
[self.delegatechangeTitle:textField.text];//将textField.text参数传给changeTitle方法 让代理,也就是A页面去实现这个方法
NSLog(@"%@",self.navigationController.viewControllers);
[self.navigationControllerpopViewControllerAnimated:YES];
}
}
单例传值(实现共享)
AppStatus.h 创建一个单例类 AppStatus
#import <Foundation/Foundation.h>
@interface AppStatus : NSObject
{
NSString *_contextStr;
}
@property(nonatomic,retain)NSString *con
B页面定义了一个naviTitle属性,在A页面中直接通过属性赋值将A页面中的值传到B页面。
A页面DetailViewController.h文件
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
@interface RootViewController :UIViewController<ChangeDelegate>
{
UITextField *tf;
}
@end
A RootViewController.m页面实现文件
#import "RootViewController.h"
#import "DetailViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
//核心代码
-(void)loadView
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 30);
[btn setTitle:@"Push" forState:0];
[btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)pushAction:(id)sender
{
tf = (UITextField *)[self.viewviewWithTag:1000];
//导航push到下一个页面
//pushViewController 入栈引用计数+1,且控制权归系统
DetailViewController *detailViewController = [[DetailViewControlleralloc]init];
//属性传值,直接属性赋值
detailViewController.naviTitle =tf.text;
//导航push到下一个页面
[self.navigationControllerpushViewController:detailViewController animated:YES];
[detailViewControllerrelease];
}
B页面DetailViewController.h文件
#import <UIKit/UIKit.h>
@interface DetailViewController :UIViewController
{
UITextField *textField;
NSString *_naviTitle;
}
@property(nonatomic,retain)NSString *naviTitle;
@end
B页面.m实现文件
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
@synthesize naviTitle =_naviTitle;
-(void)loadView
{
self.view = [[[UIViewalloc]initWithFrame:CGRectMake(0,0, 320,480)]autorelease];
self.title = self.naviTitle ;
}
代理传值
A页面push到B页面,如果B页面的信息想回传(回调)到A页面,用用代理传值,其中B定义协议和声明代理,A确认并实现代理,A作为B的代理
A页面RootViewController.h文件
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
@interface RootViewController : UIViewController<ChangeDelegate>
{
UITextField *tf;
}
@end
A页面RootViewController.m实现文件
#import "RootViewController.h"
#import "DetailViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
//核心代码
-(void)loadView
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 30);
[btn setTitle:@"Push" forState:0];
//A页面push到B页面
[btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)pushAction:(id)sender
{
tf = (UITextField *)[self.view viewWithTag:1000];
//导航push到下一个页面
//pushViewController 入栈引用计数+1,且控制权归系统
DetailViewController *detailViewController = [[DetailViewController alloc]init];
//代理传值
detailViewController.delegate =self;//让其自身作为代理人
//导航push到下一个页面
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
//实现代理方法
-(void)changeTitle:(NSString *)aStr
{
tf = (UITextField *)[self.view viewWithTag:1000];
tf.text = aStr;//将从B页面传入的参数赋给A页面中的TextField
tf.text = aStr;
}
B页面DetailViewController.m文件
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
{
UITextField *textField;
//定义代理
id<ChangeDelegate>_delegate;
}
@property(nonatomic,assign)id<ChangeDelegate> delegate;
@end
//定义协议
@protocol ChangeDelegate <NSObject>
-(void)changeTitle:(NSString *)aStr;//协议方法
@end
B页面DetailViewController.h实现文件
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
-(void)loadView
{
self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
UIBarButtonItem *doneItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(doneAction:)];
self.navigationItem.rightBarButtonItem = doneItem;
[doneItemrelease];
}
//pop回前一个页面
-(void)doneAction:(id)sender
{
if (self.delegate && [self.delegaterespondsToSelector:@selector(changeTitle:)])//若代理存在且响应了changeTitle这个方法
{
//[self.delegate changeTitle:textField.text];
[self.delegatechangeTitle:textField.text];//将textField.text参数传给changeTitle方法 让代理,也就是A页面去实现这个方法
NSLog(@"%@",self.navigationController.viewControllers);
[self.navigationControllerpopViewControllerAnimated:YES];
}
}
单例传值(实现共享)
AppStatus.h 创建一个单例类 AppStatus
#import <Foundation/Foundation.h>
@interface AppStatus : NSObject
{
NSString *_contextStr;
}
@property(nonatomic,retain)NSString *con
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询