• [Objective-C] 019_UIVIewController


    UIViewController是iOS程序中的一个重要组成部分,对应MVC设计模式的C,它管理着程序中的众多视图,何时加载视图,视图何时消,界面的旋转等。

    1.UIViewController 创建与初始化

      [1].通过nib文件创建与初始化

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    {   
    	self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    	UIViewController *test = [[TestViewController alloc] initWithNibName:@"test" bundle:nil];
    	self.window.rootViewController = test;
    	[self.window makeKeyAndVisible];
    
    	return YES;
    } 

      [2].自定义创建与初始化

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    {   
    	self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    	UIViewController *test = [[TestViewController alloc] init];
    	self.window.rootViewController = test;
    	[self.window makeKeyAndVisible];
    
    	return YES;
    } 
    

      

    2.UIViewController 旋转方向控制

    • iOS6之前,shouldAutorotateToInterfaceOrientation方法单独控制UIViewController的方向。
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
                interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    • iOS6及更高,重写这个2个方法,iOS6里面要旋转还有一些需要注意的地方,需要在Info.plist文件里面添加Supported interface orientations 程序支持的方向.
    - (BOOL)shouldAutorotate {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
    	return UIInterfaceOrientationMaskPortrait;//竖立
    }
    

    3.UIViewController 切换

    • UIViewController自身之间的调用
    UIViewController *test = [[UIViewController alloc] init];
    //跳转  test
    [self.window.rootViewController presentViewController:test animated:YES completion:^{
        NSLog(@"present 完成");
    }];
    //返回到self  (test controller消失)
    [self.window.rootViewController dismissViewControllerAnimated:YES completion:^{
            NSLog(@"dismiss 完成");
    }];
    • UINavigationControlle导航控制器的Controller来控制ViewContrller之间的切换(层次逻辑性的ViewContrller之间的切换)
    //跳转
    [self.navigationController pushViewController:test animated:YES]; 
    //返回
    [self.navigationController popViewControllerAnimated:YES];
    

     从以上几点可以看出,UIViewController 之间的切换管理,正确的做法是"谁污染谁治理"。

     

    本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 

    转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4771718.html

  • 相关阅读:
    计算1至n中数字X出现的次数
    DOM处理
    SQL Server中日志
    怎样玩转千万级别的数据
    协议的分用以及wireshark对协议的识别
    序列化json对象,通过ajax传入asp.net mvc后台
    新时代的Vim C++自动补全插件 clang_complete
    ASP.NET Web API 接口执行时间监控
    应用之星在线app开发平台,菜鸟也会做应用
    1.11 查找空值
  • 原文地址:https://www.cnblogs.com/superdo/p/4771718.html
Copyright © 2020-2023  润新知