iOS各种传值
属性传值 将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]; } }
单例传值(实现共享)
#import <Foundation/Foundation.h>
@interface AppStatus : NSObject { NSString *_contextStr; } @property(nonatomic,retain)NSString *contextStr; +(AppStatus *)shareInstance; @end AppStatus.m
#import "AppStatus.h"
@implementation AppStatus @synthesize contextStr = _contextStr;
static AppStatus *_instance = nil; +(AppStatus *)shareInstance { if (_instance == nil) { _instance = [[super alloc]init]; } return _instance; }
-(id)init { if (self = [super init]) {
} return self; }
-(void)dealloc { [super dealloc]; }
@end A页面RootViewController.h
#import "DetailViewController.h" #import "AppStatus.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.view viewWithTag:1000];
//单例传值 将要传递的信息存入单例中(共享中) // [[AppStatus shareInstance]setContextStr:tf.text]; 跟下面这种写法是等价的 [AppStatus shareInstance].contextStr = tf.text; //导航push到下一个页面 //pushViewController 入栈引用计数+1,且控制权归系统
DetailViewController *detailViewController = [[DetailViewController alloc]init];
//导航push到下一个页面 [self.navigationController pushViewController:detailViewController animated:YES]; [detailViewController release]; }@end B页面DetailViewController.h
#import <UIKit/UIKit.h> @protocol ChangeDelegate;//通知编译器有此代理
@interface DetailViewController : UIViewController { UITextField *textField; } @end
#import "DetailViewController.h" #import "AppStatus.h"
@interface DetailViewController ()
@end
@implementation DetailViewController @synthesize naviTitle = _naviTitle;
-(void)loadView { self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
//单例 self.title = [AppStatus shareInstance].contextStr;
textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)]; textField.borderStyle = UITextBorderStyleLine; [self.view addSubview:textField]; [textField release];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)]; self.navigationItem.rightBarButtonItem = doneItem; [doneItem release];
} //这个方法是执行多遍的 相当于刷新view -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];
tf = (UITextField *)[self.view viewWithTag:1000]; tf.text = [AppStatus shareInstance].contextStr;
}
//pop回前一个页面 -(void)doneAction:(id)sender { // 单例传值 [AppStatus shareInstance].contextStr = textField.text; [self.navigationController popToRootViewControllerAnimated:YES]; }
通知传值 谁要监听值的变化,谁就注册通知 特别要注意,通知的接受者必须存在这一先决条件
A页面RootViewController.h #import <UIKit/UIKit.h> #import "DetailViewController.h"
@interface RootViewController : UIViewController<ChangeDelegate> { UITextField *tf; } @endA页面RootViewController.m #import "IndexViewController.h" #import "DetailViewController.h" #import "AppStatus.h"
@implementation IndexViewController
-(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"CHANGE_TITLE" object:nil]; [super dealloc]; }
-(id)init { if (self = [super init]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:@"CHANGE_TITLE" object:nil]; } return self; }
-(void)change:(NSNotification *)aNoti { // 通知传值 NSDictionary *dic = [aNoti userInfo]; NSString *str = [dic valueForKey:@"Info"];
UITextField *tf = (UITextField *)[self.view viewWithTag:1000]; tf.text = str; }
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];
/* // 单例传值 UITextField *tf = (UITextField *)[self.view viewWithTag:1000]; tf.text = [AppStatus shareInstance].contextStr; */ } @end DetailViewController.h #import <UIKit/UIKit.h> @protocol ChangeDelegate;//通知编译器有此代理
@interface DetailViewController : UIViewController { UITextField *textField; } @end DetailViewController.m
#import "DetailViewController.h" #import "AppStatus.h"
@implementation DetailViewController @synthesize naviTitle = _naviTitle;
-(void)loadView {
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)]; self.navigationItem.rightBarButtonItem = doneItem; [doneItem release]; }
// pop回前一个页面 -(void)doneAction:(id)sender {
NSDictionary *dic = [NSDictionary dictionaryWithObject:textField.text forKey:@"Info"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_TITLE" object:nil userInfo:dic];
[self.navigationController popViewControllerAnimated:YES];
}
Block 几种形式的Block //无返回值
Block传值 例如A(Ablock)页面的值传道B(Bblock)页面
在A页面中ABlock.h @interface Ablock : UIViewController<UITableViewDelegate,UITableViewDataSource>
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
在A页面中Bblock.h #import <UIKit/UIKit.h>
// |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。