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

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

 

presentViewController与dismissViewControllerAnimated是针对普通的界面框架(不用UINavigationController)。

 

从A界面打开B界面

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completioncompletion:(void (^)(void))completion 在B界面的viewDidAppear()调用后执行

 

关闭当前界面(B界面),返回之前的界面(A界面)

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completioncompletion:(void (^)(void))completion 在A界面的viewDidAppear()调用后执行

 

假设

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

 

其中界面1为项目的rootViewController.

 

AppDelegate.h代码

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSString* localImagePath;

@end

 

AppDelegate.m的部分代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    
    ViewController01* mViewController01 = [[ViewController01 alloc]init];
    self.window.rootViewController = mViewController01;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

 

ViewController01.h代码

#import <UIKit/UIKit.h>

@interface ViewController01 : UIViewController

@property (strong,nonatomic) NSMutableDictionary* parameter;

@end

 

ViewController01.m代码

#import "ViewController01.h"
#import "MyViewTool.h"
#import "ViewController02.h"


@interface ViewController01 ()

@end

@implementation ViewController01

- (void)viewDidLoad {
    NSLog(@"viewDidLoad():视图1");
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initViews];
    [self initParameter];
}

-(void)viewDidAppear:(BOOL)animated{
    NSLog(@"viewDidAppear():视图1");
    [super viewDidAppear:animated];
    //接收到的参数
    NSLog(@"viewDidAppear():视图1,收到的参数:from=%@",[self.parameter objectForKey:@"from"]);
}

- (void)initViews{
    
    CGSize size = [MyViewTool loadScreenSize];
    CGFloat width = size.width;
    
    UILabel* label = [MyViewTool createLabel:CGRectMake(10, 40, width-20, 100) withText:@"视图1" withBgcolor:[UIColor yellowColor]];
    
    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"
                                 withBgColor:[UIColor lightGrayColor]];
    
    
    btn.center = CGPointMake(width/2, btn.center.y);
    
    [self.view addSubview:label];
    [self.view addSubview:btn];
}

#pragma mark 初始化要传递的参数
- (void)initParameter{
    self.parameter = [[NSMutableDictionary alloc]init];
}


-(void)buttonClick : (UIButton*) sender {
    NSLog(@"buttonClick:%@",sender.titleLabel.text);
    
    //设置传递参数的数据
    [self.parameter setObject:@"我是视图1设置的参数" forKey:@"from"];
    
    //打开 ViewController02
    ViewController02* mViewController02 = [[ViewController02 alloc]init];
    mViewController02.parameter=self.parameter;
    
    [self presentViewController:mViewController02 animated:YES completion:^{
        NSLog(@"presentViewController成功");
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

ViewController02.h代码

#import <UIKit/UIKit.h>

@interface ViewController02 : UIViewController

@property (strong,nonatomic) NSMutableDictionary* parameter;

@end

 

ViewController02.m代码

#import "ViewController02.h"
#import "MyViewTool.h"

@interface ViewController02 ()

@end

@implementation ViewController02


- (void)viewDidLoad {
    NSLog(@"viewDidLoad():视图2");
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initViews];
}

-(void)viewDidAppear:(BOOL)animated{
    NSLog(@"viewDidAppear():视图2");
    [super viewDidAppear:animated];
    //接收到的参数
    NSLog(@"viewDidAppear():视图2,收到的参数:from=%@",[self.parameter objectForKey:@"from"]);
}

- (void)initViews{
    
    CGSize size = [MyViewTool loadScreenSize];
    CGFloat width = size.width;
    
    UILabel* label = [MyViewTool createLabel:CGRectMake(10, 40, width-20, 100) withText:@"视图2" withBgcolor:[UIColor yellowColor]];
    
    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]];
    
    
    btn.center = CGPointMake(width/2, btn.center.y);
    
    [self.view addSubview:label];
    [self.view addSubview:btn];
}

-(void)buttonClick : (UIButton*) sender {
    NSLog(@"buttonClick:%@",sender.titleLabel.text);
    //设置返回给视图1的参数
    [self.parameter setObject:@"我是视图2设置的参数" forKey:@"from"];
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"dismissViewControllerAnimated成功");
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

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

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值