UIday1201:模态

模态 viewController :
  介绍:
 程序中切换⻚面,可以使⽤ UINavigationController 。通过导航功能实现⻚面切换。
 某种情况下,可以使⽤视图控制器的⼀对⽅法实现切换⻚面
 presentViewController:animated:completion: 展⽰新的⻚面
 dismissViewControllerAnimated:completion: 从新⻚面返回
 通过上述⽅法显⽰的 controller 被称为模态视图控制器。
 注:模态视图控制器不是
 ⼀个类,只是⼀种⽅式显示的 controller 。
 
 使用场景:
 UIImagePickerController。调⽤系统相册、照相机。
 临时展⽰一些内容。例如:程序中⽤户登录,通讯录中添加联系人,弹出的临时广告页等等。
 导航控制器控制的 controller 具有层级关系。present 控制显示的 controller 与之前的 controller 是两个层级。
 
 使用方式:
 presentViewController:animated:completion: 模态显⽰
 controller 在 controllerA 中模态显⽰ controllerB
     presenting view controller  => controllerA
     presented view controller   => controllerB ( 被弹出的 )
 
     dismissViewControllerAnimated:completion: 弹出模态 controller
 两种实现方式,在模态 controller 中使⽤:
     [self dismissViewControllerAnimated:YES completion: nil];
     [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
 使⽤self调用⽅法,系统会使⽤ self.presentingViewController 调⽤这个方法。
 
  属性设置
 modalPresentationStyle 模态 controller 显⽰样式
 modalTransitionStyle 模态显⽰动画样式


AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


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

@end


RootViewController.m

#import "RootViewController.h"
#import "NextViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NextViewController * nextVC = [[NextViewController alloc]init];
    
    //模态推出视图
    //模态直接用controller就可以直接推出,它没有层级结构
    [self presentViewController:nextVC animated:YES completion:^{
        
    }];
}

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

@end
 

NextViewController.m

#import "NextViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor cyanColor];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //取消模态视图
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

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

@end



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值