iOS 中的页面间传值的各种方法

1.通过属性传值

  将A页面所拥有的信息通过属性传递到B页面使用,B页面定义了一个naviTitle属性,在A页面中直接通过属性赋值将A页面中的值传到B页面。

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface RootViewController :UIViewController<ChangeDelegate>

{

UITextField *textField;

}

@end

A页面   RootViewController.m面实现文件

#import "RootViewController.h"

#import "DetailViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

//核心代码

-(void)loadView

{

UIButton *BT = [UIButton buttonWithType:UIButtonTypeRoundedRect];

BT.frame = CGRectMake(0, 0, 100, 30);

[BT setTitle:@"Push" forState:0];

[BT addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

-(void)pushAction:(id)sender

{

textField = (UITextField *)[self.viewviewWithTag:1000];

//导航push到下一个页面

//pushViewController 入栈引用计数+1,且控制权归系统

DetailViewController *detailViewController = [[DetailViewControlleralloc]init];

//属性传值,直接属性赋值

detailViewController.naviTitle =textField.text;

//导航push到下一个页面

[self.navigationControllerpushViewController:detailViewController animated:YES];

[detailViewControllerrelease];

}

#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 ;

}

2.代理传值

A页面push到B页面,如果B页面的信息想回传(回调)到A页面,用代理传值,其中B定义协议和声明代理,A确认并实现代理,A作为B的代理

A页面RootViewController.h文件

#import <UIKit/UIKit.h>

#import "DetailViewController.h"

@interface RootViewController : UIViewController<ChangeDelegate>

{

UITextField *textField;

}

@end

A页面RootViewController.m实现文件

#import "RootViewController.h"

#import "DetailViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

//核心代码

-(void)loadView

{

UIButton *BT = [UIButton buttonWithType:UIButtonTypeRoundedRect];

BT.frame = CGRectMake(0, 0, 100, 30);

[btn setTitle:@"Push" forState:0];

//A页面push到B页面

[BT addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:BT];

}

-(void)pushAction:(id)sender

{

textField = (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

{

textField = (UITextField *)[self.view viewWithTag:1000];

textField.text = aStr;//将从B页面传入的参数赋给A页面中的TextField

textField.text = aStr;

}

B页面DetailViewController.m文件

B 页面 DetailViewController.h

#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.m实现文件

#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:UIBarButtonSystemItemDone target:self action:@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.delegate changeTitle:textField.text];//将textField.text参数传给changeTitle方法  让代理,也就是A页面去实现这个方法

NSLog(@"%@",self.navigationController.viewControllers);

[self.navigationController popViewControllerAnimated:YES];

}

}

3.单例传值(实现共享)
  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
#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.

#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];

}

4. 通知传值 谁要监听值的变化,谁就注册通知  特别要注意,通知的接受者必须存在这一先决条件

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];

}

5.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

时间: 2024-07-30 13:50:47

iOS 中的页面间传值的各种方法的相关文章

IOS开发之页面间传值的几种小方法

最正经的页面间传值方式莫过于代理传值,今天写的几种小方法不包括代理传值,因为是自学的原因,现在都不知道这几种方法的优缺点,往知道的朋友指正一下: 第一种:属性传值:(这种方法在故事版无效,适合纯代码编写时使用,为什么???) 前提:比如说有两个视图控制器:AViewController和BViewController,他们之间想从A传值到B, ——>在AViewController.m文件中 (1)导入BViewController #import "BViewController.h&q

web 页面间传值 js 封装方法

用法 var id = getParam("id"); function getParam(strKey) { var url=document.URL; //var url="file://wqasdasd/www/1.html?id=2&index=5"; var para=""; if(url.lastIndexOf(strKey)<0) { } else { if(url.lastIndexOf("?")

MUI框架-02-注意事项-适用场景-实现页面间传值

MUI框架-02-注意事项-适用场景-实现页面间传值 关于开发,我拷贝太多也没什么意义,就请查阅:官方文档:http://dev.dcloud.net.cn/mui/ui/ 快速入门 - 注意事项 有些可能看不懂,这样排是为了可以做 MUI 开发的时候,养成良好的习惯,避免不必要的错误 DOM 结构: 关于 mui 页面的 dom,你需要知道如下规则 固定栏靠前: 所谓的固定栏,也就是带有.mui-bar 属性的节点,都是基于 fixed 定位的元素: 常见组件包括:顶部导航栏(.mui-bar

iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)

iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notification方式:3.block方式:4.UserDefault或者文件方式:5.单例模式方式:6.通过设置属性,实现页面间传值 在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下: 情况1:A页面跳转到B页面 方法: 在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给

iOS 页面间传值 之 单例传值 , block 传值

ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同一个对象,对象的属性相同,所以可以用来传值. block 传值 与 代理传值相似,主要用于第二个页面向第一个页面传值,block 传值具体步骤: 在第二个页面: 1.声明: block typedef void(^SendMessagerBlock) (NSString *str); 2.创建方法:

iOS页面间传值的方式

实现了以下iOS页面间传值:1.委托delegate方式:2.通知notification方式:3.block方式:4.UserDefault或者文件方式:5.单例模式方式:6.通过设置属性,实现页面间传值 在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下: 情况1:A页面跳转到B页面 方法: 在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给B的属性赋值即可 //SecondViewController.h @property(nonatomic) NSInt

iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)

iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notification方式:3.block方式:4.UserDefault或者文件方式:5.单例模式方式:6.通过设置属性,实现页面间传值 在iOS开发中,我们经常会遇到页面间跳转传值的问题,

iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)

实现了以下iOS页面间传值:1.委托delegate方式:2.通知notification方式:3.block方式:4.UserDefault或者文件方式:5.单例模式方式:6.通过设置属性,实现页面间传值 在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下: 情况1:A页面跳转到B页面 方法: 在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给B的属性赋值即可 //SecondViewController.h @property(nonatomic) NSInt

iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)

iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值: 1.委托delegate方式:2.通知notification方式:3.block方式:4.UserDefault或者文件方式:5.单例模式方式:6.通过设置属性. 在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一