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];
}
}
单例传值(实现共享)
AppStatus.h 创建一个单例类 AppStatus
#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 "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
B页面DetailViewController.m
#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;
}
@end
A页面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
//无返回值
void (^block1) (void);
block1 = ^{
NSLog(@"bock demo");
};
block1();
//int返回类型
int (^block2) (void);
block2 = ^(void)
{
int a = 1 ,b =1;
int c = a+b;
return c;
};
//有返回 有参数
int (^block3)(int, int)= ^(int a, int b)
{
int c = a +b;
return c;
};
NSLog(@"bock=%d",block3(1,2));
//有返回值,有参数并且可以修改block之外变量的block
static int sum = 10;// __blcik and static关键字 或者 _block int sum = 10
int (^block4) (int) =^(int a)
{
sum=11;
int c = sum+a; //此时sum就是可以修改的了,若没加static或_block关键字则不能修改block之外变量
return c;
};
NSLog(@"block4= %d",block4(4));
Block传值
例如A(Ablock)页面的值传道B(Bblock)页面
在A页面中ABlock.h
@interface Ablock : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
UITableView *_tableview;
UILabel *labe;
UIImageView *imagevies;
}
@end
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_tableview deselectRowAtIndexPath:indexPath animated:YES];
Bblcok *bblock = [[Bblcok alloc] initwithBlock:Block_copy(^(NSString *aBlock){
labe.text = aBlock;
NSLog(@"%@",aBlock);
})];
bblock.imgeviews = imagevies.image;
bblock.String = labe.text;
[self.navigationController pushViewController:bblock animated:YES];
[bblock release];
}
在A页面中Bblock.h
#import <UIKit/UIKit.h>
typedef void (^MyBlock) (NSString *);
@interface Bblcok : UIViewController
{
UIImageView *image;
UITextField *aField;
UIButton *aButt;
NSString *_String;
id _imgeviews;
MyBlock myBlock;
}
@property(nonatomic,copy)MyBlock myBlock;
@property(nonatomic,retain) id imgeviews;
@property(nonatomic,retain) NSString *String;
-(id)initwithBlock:(MyBlock)aBlcok;
@end
//
// Bblcok.m
// Blcok
//
// Created by zhu on 13-8-12.
// Copyright (c) 2013年 Zhu Ji Fan. All rights reserved.
//
#import "Bblcok.h"
@interface Bblcok ()
@end
@implementation Bblcok
@synthesize imgeviews = _imgeviews , String = _String;
@synthesize myBlock = _myBlock;
-(id)initwithBlock:(MyBlock)aBlcok
{
if (self = [super init])
{
self.myBlock = aBlcok;
}
return self;
}
-(void) dealloc
{
[super dealloc];
}
-(void) loadView
{
UIControl *cont = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 320, 568-44)];
[cont addTarget:self action:@selector(Clcik) forControlEvents:UIControlEventTouchUpInside];
self.view = cont;
aField = [[UITextField alloc] initWithFrame:CGRectMake(60, 10, 160, 30)];
aField.borderStyle = UITextBorderStyleLine;
aField.placeholder = self.String;
[self.view addSubview:aField];
aButt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButt.frame = CGRectMake(60, 50, 70, 30);
[aButt setTitle:@"修改" forState:0];
[aButt addTarget:self action:@selector(aButtClcik:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButt];
image = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100, 210, 260)];
image.backgroundColor = [UIColor blueColor];
image.image = self.imgeviews;
[self.view addSubview:image];
[image release];
}
-(IBAction)aButtClcik:(id)sender
{
NSString *sting = aField.text;
myBlock(sting);
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)Clcik
{
[aField resignFirstResponder];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS 各种传值方式的更多相关文章
- iOS页面传值方式
普遍传值方式如下: 1.委托delegate方式: 2.通知notification方式: 3.block方式: 4.UserDefault或者文件方式: 5.单例模式方式: 6.通过设置属性,实现页 ...
- ios 多种传值方式
在网上看了看传值方法,没有找到完整的.在这把自己看到的几种传值方法写写吧. 1 . 属性传值 2 . 通知传值 3 . 代理传值 4 . block传值 5 . 单列传值 6 . ShareAppli ...
- iOS学习之六种传值方式
iOS页面传值方式 应用于: 两个互动的界面:1)页面一跳转到页面二,页面一的textField的值传给页面二的label.2)A页面跳转到B页面,B页面再跳转回A页面(注册页面跟登录页面) 两个不互 ...
- ios常见的页面传值方式
iOS页面间的传值细分有很多种,基本的传值方式有三种:委托Delegate传值.通知NSNotification传值.Block传值,其他在项目中可能会遇到的还有:UserDefault或文件方式传值 ...
- iOS多视图传值方式之通知传值(NSNotification;NSNotificationCenter)
iOS传值方式之5:通知传值 第一需要发布的消息,再创建NSNotification通知对象,然后通过NSNotificationCenter通知中心发布消息(NSNotificationCenter ...
- iOS学习——页面的传值方式
一.简述 在iOS开发过程中,页面跳转时在页面之间进行数据传递是很常见的事情,我们称这个过程为页面传值.页面跳转过程中,从主页面跳转到子页面的数据传递称之为正向传值:反之,从子页面返回主页面时的数据传 ...
- iOS 浅谈MVC设计模式及Controllers之间的传值方式
1.简述你对MVC的理解? MVC是一种架构设计.它考虑了三种对象:Model(模型对象).View(试图对象).Controller(试图控制器) (1)模型:负责存储.定义.操作数据 (2)视图: ...
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...
- iOS的四种传值方式
传值有四种方法 : 1. 属性传值 2. 单例传值 3. 代理传值 4. block传值 一.属性传值 (前-->后) 1. 后面的界面定义一个属性 存放前一个界面传过来的值 ...
随机推荐
- iOS学习之iOS沙盒(sandbox)机制和文件操作(一)
1.iOS沙盒机制 iOS应用程序仅仅能在为该改程序创建的文件系统中读取文件,不能够去其他地方訪问,此区域被成为沙盒,所以全部的非代码文件都要保存在此,比如图像,图标,声音,映像,属性列表,文本文件等 ...
- Starting httpd:Could not reliably determine the server's fully qualified domain name
#service httpd start #Starting httpd: httpd: Could not reliably determine the server's fully qualifi ...
- WinForm常用代码
//ToolStripSplitButton是标准按钮和下拉按钮的组合,各自工作,但有联系,感觉上后者是没有向下箭头ToolStripDropDownButton:ToolStripDropDownB ...
- 全分布式环境下,DataNode不启动的问题解决
问题出现:机器重启之后,再次在master结点上面执行start-all.sh,发现有一个datanode没有启动,通过jps检查之后,发现slave1上面的datanode进程未启动 原因:每次na ...
- java之集合类框架的简要知识点:泛型的类型擦除
这里想说一下在集合框架前需要理解的小知识点,也是个人的肤浅理解,不知道理解的正不正确,请大家多多指教.这里必须谈一下java的泛型,因为它们联系紧密,我们先看一下这几行代码: Class c1 = n ...
- C - The Hardest Problem Ever
Description Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever ...
- Java并发编程小记
1. Semaphore 信号量是一种计数器,用来保护一个或者多个共享资源的访问.如果线程要访问一个共享资源,必须先获得信号量.若内部计数器大于0,则减1,若等于0,则线程进入休眠直至计数器大于等于0 ...
- Hadoop配置文件-mapred-site.xml
name value Description hadoop.job.history.location job历史文件保存路径,无可配置参数,也不用写在配置文件里,默认在logs的history文件 ...
- SharedPreferences基础
见归档项目:SharedPreferencesDemo.zip 1.对于数据量较小,且有明显的K-V形式的数据而言,适合用SharedPreferences保存.SharedPreferences的数 ...
- mac10.9 设置vim tab
vimrc系统文件目录 /usr/share/vim 在vimrc中添加以下代码后,重启vim即可实现按TAB产生4个空格:set ts=4 (注:ts是tabstop的缩写,设TAB宽4个空格)s ...