IOS 页面之间的跳转以及传值

来源:互联网 发布:如何做好淘宝客推广 编辑:程序博客网 时间:2024/05/17 05:10

一.页面之间的跳转

1.navigation页面之间的跳转

[self.navigationController pushViewController:destViewCtl     animated:YES];//跳转到目的页面

[self.navigationController popViewController:sourceViewCtl  animated:YES];//从目的页面返回到原来页面

 [self.navigationController  popToRootViewControllerAnimated:YES];//返回根控制器,就是最开始初始化navigtion时选择的根页面

2.modal形式页面跳转

[self  presentViewController:newC animated: YES completion:nil];//跳转到目的页面

[ self dismissViewControllerAnimated: YES completion: nil ];//从目的页面返回到原来页面

3.使用tabBarCtl时

有时通过 [tabbarCtl addChildViewController :destViewCtl ];添加子视图,比较少用。

目前一般自定义TabBar控件,初始化时直接加载进设定好的几个子视图

4.比较极端的方法,直接修改根视图控制器

self.window.rootViewController=destViewCtl;

5.直接进行UIView的跳转

[self.view addSubview:destView];

[self.view remoFromSuperView];

[self.view bringSubviewToFront:destView]

[self.view sendSuperViewToBottom:destView];

二. 不同视图间的传值

定义A,B两个ViewCtl,实现从A向B传值

1.定义属性变量传值

在B头文件中定义属性变量 @property NSString *strValue;

在A的.m文件中引入B的头文件,在界面跳转前通过定义的B类对象实例给 strValue赋值,界面跳转后即可在B中使用

B=[[UIViewController alloc]init];     B.strValue=destValue;   跳转到B中

2.代理传值(感觉和1类似,是否有安全方面问题暂无研究)

首先定义协议

@protocol sendValueDelegate 

-(void)sendValue:(NSString *)destValue;

@end

在A中引用协议头文件并定义属性变量

@property(nonatomic, retain) id<sendValueDelegate >sendDelegate; 

在执行函数中调用代理函数并跳转到B

B *b=[[B alloc]init];

self.sendDelegate=b;

[self.sendDelegate sendValue:XXX];

跳转B页面

在B的头文件中加入代理头文件并引用代理

@interface B : NSObject <sendValueDelegate>

- (void) sendValue:(NSString *) destValue;     

@end

在B的头文件中实现方法,并接收传过来的值

- (void) setValue:(NSString *) destValue

NSString *str=destValue;

3通知传值

在A中

创建传值用的字典,里面保存键名键值

NSDictionary *sendDict=[[NSDictionary alloc]initWithObjectsAndKeys:keyName,keyValue,........,nil];

创建通知

NSNotification *sendNotific=[NSNotification notificationWithName:@"notiName" object:nil userInfo:sendDict];

发送通知

[[NSNotificationCenter defaultCenter]postNotification:sendNotific];

 在B中

[[NSNotificationCenterdefaultCenter]addObserver:self selector:@selector(notificAction:)name:@"notiName"object:nil];


通知方法

- (void)notificAction:(NSNotification *)sendInfo{

    NSDictionary *dict =sendInfo.userInfo;

   NSString *str= 从字典中根据具体键名取出具体数据;

    NSLog(@"%@",str);

}
  注:若是反向传值并显示,一般使用代理或通知,在执行函数中修改,若数据少,则可以只改固定控件;数据多则重刷页面。















1 0
原创粉丝点击