iOSm界面跳转和参数传递之presentViewController与dismissViewControllerAnimated

假设

界面1为 ViewController01 : UIViewController 界面2为 ViewController02 : UIViewController 

 

其中界面1为项目的rootViewController.

 

AppDelegate.h代码

C代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  4.   
  5. @property (strong, nonatomic) UIWindow *window;  
  6. @property (strong, nonatomic) NSString* localImagePath;  
  7.   
  8. @end  

 

AppDelegate.m的部分代码

C代码   收藏代码
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  3.     // Override point for customization after application launch.  
  4.       
  5.       
  6.     ViewController01* mViewController01 = [[ViewController01 alloc]init];  
  7.     self.window.rootViewController = mViewController01;  
  8.     self.window.backgroundColor = [UIColor whiteColor];  
  9.     [self.window makeKeyAndVisible];  
  10.     return YES;  
  11. }  

 

ViewController01.h代码

C代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController01 : UIViewController  
  4.   
  5. @property (strong,nonatomic) NSMutableDictionary* parameter;  
  6.   
  7. @end  

 

ViewController01.m代码

C代码   收藏代码
  1. #import "ViewController01.h"  
  2. #import "MyViewTool.h"  
  3. #import "ViewController02.h"  
  4.   
  5.   
  6. @interface ViewController01 ()  
  7.   
  8. @end  
  9.   
  10. @implementation ViewController01  
  11.   
  12. - (void)viewDidLoad {  
  13.     NSLog(@"viewDidLoad():视图1");  
  14.     [super viewDidLoad];  
  15.     // Do any additional setup after loading the view.  
  16.     [self initViews];  
  17.     [self initParameter];  
  18. }  
  19.   
  20. -(void)viewDidAppear:(BOOL)animated{  
  21.     NSLog(@"viewDidAppear():视图1");  
  22.     [super viewDidAppear:animated];  
  23.     //接收到的参数  
  24.     NSLog(@"viewDidAppear():视图1,收到的参数:from=%@",[self.parameter objectForKey:@"from"]);  
  25. }  
  26.   
  27. - (void)initViews{  
  28.       
  29.     CGSize size = [MyViewTool loadScreenSize];  
  30.     CGFloat width = size.width;  
  31.       
  32.     UILabel* label = [MyViewTool createLabel:CGRectMake(10, 40, width-20, 100) withText:@"视图1" withBgcolor:[UIColor yellowColor]];  
  33.       
  34.     UIButton* btn = [MyViewTool createButton:CGRectMake(label.frame.origin.x, label.frame.origin.y+label.frame.size.height+20, 200, 50) withDelegate:self withAction:@selector(buttonClick:) withTitle:@"按钮1:点击打开视图2"  
  35.                                  withBgColor:[UIColor lightGrayColor]];  
  36.       
  37.       
  38.     btn.center = CGPointMake(width/2, btn.center.y);  
  39.       
  40.     [self.view addSubview:label];  
  41.     [self.view addSubview:btn];  
  42. }  
  43.   
  44. #pragma mark 初始化要传递的参数  
  45. - (void)initParameter{  
  46.     self.parameter = [[NSMutableDictionary alloc]init];  
  47. }  
  48.   
  49.   
  50. -(void)buttonClick : (UIButton*) sender {  
  51.     NSLog(@"buttonClick:%@",sender.titleLabel.text);  
  52.       
  53.     //设置传递参数的数据  
  54.     [self.parameter setObject:@"我是视图1设置的参数" forKey:@"from"];  
  55.       
  56.     //打开 ViewController02  
  57.     ViewController02* mViewController02 = [[ViewController02 alloc]init];  
  58.     mViewController02.parameter=self.parameter;  
  59.       
  60.     [self presentViewController:mViewController02 animated:YES completion:^{  
  61.         NSLog(@"presentViewController成功");  
  62.     }];  
  63.       
  64. }  
  65.   
  66. - (void)didReceiveMemoryWarning {  
  67.     [super didReceiveMemoryWarning];  
  68.     // Dispose of any resources that can be recreated.  
  69. }  
  70.   
  71. @end  

 

ViewController02.h代码

C代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController02 : UIViewController  
  4.   
  5. @property (strong,nonatomic) NSMutableDictionary* parameter;  
  6.   
  7. @end  

 

ViewController02.m代码

C代码   收藏代码
  1. #import "ViewController02.h"  
  2. #import "MyViewTool.h"  
  3.   
  4. @interface ViewController02 ()  
  5.   
  6. @end  
  7.   
  8. @implementation ViewController02  
  9.   
  10.   
  11. - (void)viewDidLoad {  
  12.     NSLog(@"viewDidLoad():视图2");  
  13.     [super viewDidLoad];  
  14.     // Do any additional setup after loading the view.  
  15.     [self initViews];  
  16. }  
  17.   
  18. -(void)viewDidAppear:(BOOL)animated{  
  19.     NSLog(@"viewDidAppear():视图2");  
  20.     [super viewDidAppear:animated];  
  21.     //接收到的参数  
  22.     NSLog(@"viewDidAppear():视图2,收到的参数:from=%@",[self.parameter objectForKey:@"from"]);  
  23. }  
  24.   
  25. - (void)initViews{  
  26.       
  27.     CGSize size = [MyViewTool loadScreenSize];  
  28.     CGFloat width = size.width;  
  29.       
  30.     UILabel* label = [MyViewTool createLabel:CGRectMake(10, 40, width-20, 100) withText:@"视图2" withBgcolor:[UIColor yellowColor]];  
  31.       
  32.     UIButton* btn = [MyViewTool createButton:CGRectMake(label.frame.origin.x, label.frame.origin.y+label.frame.size.height+20, 200, 50) withDelegate:self withAction:@selector(buttonClick:) withTitle:@"按钮2:点击返回视图1" withBgColor:[UIColor lightGrayColor]];  
  33.       
  34.       
  35.     btn.center = CGPointMake(width/2, btn.center.y);  
  36.       
  37.     [self.view addSubview:label];  
  38.     [self.view addSubview:btn];  
  39. }  
  40.   
  41. -(void)buttonClick : (UIButton*) sender {  
  42.     NSLog(@"buttonClick:%@",sender.titleLabel.text);  
  43.     //设置返回给视图1的参数  
  44.     [self.parameter setObject:@"我是视图2设置的参数" forKey:@"from"];  
  45.     [self dismissViewControllerAnimated:YES completion:^{  
  46.         NSLog(@"dismissViewControllerAnimated成功");  
  47.     }];  
  48. }  
  49.   
  50.   
  51. - (void)didReceiveMemoryWarning {  
  52.     [super didReceiveMemoryWarning];  
  53.     // Dispose of any resources that can be recreated.  
  54. }  
  55.   
  56. @end  

 

 项目源代码参见附近中的demo010.zip

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值