当前位置: 代码迷 >> Iphone >> iOS 页面传值 -该如何解决
  详细解决方案

iOS 页面传值 -该如何解决

热度:75   发布时间:2016-04-25 06:06:50.0
iOS 页面传值 --
 我要从A页面传值(字典)给B页面,什么方式传值好呢?
球类似的demo或者链接  谢谢
iOS?传值

------解决方案--------------------
 跳转到页面B的时候 ,B已经创建了吧,那么,你可以在B里面声明一个属性,在A里面创建B的时候,将B的属性赋值即可。

------解决方案--------------------
例如:

xxxViewController *document = [[xxxViewController alloc] initWithStyle:UITableViewStyleGrouped];
document.docDict = [self.dataArray objectAtIndex:indexPath.row];
document.properties = 要传的值
[self.navigationController pushViewController:document animated:YES];
[document release];

------解决方案--------------------
可以使用全局变量,也可以使用类的属性进行传值。

比如可以使用ClassB中的对外公开的变量进行赋值
比如:
在自定义ClassB.h中代码如下:
#import <Foundation/Foundation.h>
@interface ClassB : NSObject
@property (nonatomic, retain) NSMutableDictionary *myDic;
@end

在自定义ClassB.m中代码如下:
#import "ClassB.h"
@implementation ClassB
@synthesize myDic;
@end

在自定义类ClassA中要想引用ClassB类,并且给ClassB中的参数传值,可以如下:
自定义ClassA.h中代码如下:
#import <Foundation/Foundation.h>
@interface ClassA : NSObject
-(void)testMethod;
@end

自定义ClassA.m中代码如下:
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA
-(void)testMethod
{
    NSMutableDictionary *tmpDic = [NSDictionary dictionaryWithObject:@"xixi" forKey:@"name"];
    ClassB *myClassB = [[ClassB alloc] init];
    myClassB.myDic = tmpDic;
}
@end
如上就可以给对象myClassB中的字典参数传值了。

另外,也可以使用全局变量,比如使用单例AppDelegate的对象进行传值:
AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];

------解决方案--------------------
想到四个途径:
1)使用代理
2)使用通知notification
3)使用NSUserdefault
4)使用全局变量,如在appdelegate里面定义一个变量用于传值
------解决方案--------------------
引用:
想到四个途径:
1)使用代理
2)使用通知notification
3)使用NSUserdefault
4)使用全局变量,如在appdelegate里面定义一个变量用于传值


1,2楼说得最简单了,为什么要这么麻烦呢?没有异步操作,最好不要用以上几种途径,个人建议而已。
  相关解决方案