以下是在iOS中最简单的界面切换示例。使用了多个Controller,并演示Controller之间在切换界面时的代码处理。

实现的应用界面:

首先,创建一个window-based application,即:

使用window-base application的目的是,尽量从最基本的情况下说明程序的编写过程。项目创建好后,即可以编译运行,执行结果是白屏页面:

编写第一个视图和控制器,我管它叫Topic,即主题,因此控制器命名为:TopicController,视图TopicView。

创建TopicController:

这样将创建3个文件:

视图xib文件也一同创建了。而且:

会自动生成File’s Owner的Class。

在MainWindow.xib中,将刚刚创建的控制器(TopicController)加进来。

先要拖拽一个View Controller进来:

然后,给View Controller改名:

下面,要把这个Controller设置给WindowDemoAppDelegate。在它的头文件中:

#import
#import "TopicController.h"@interface WindowDemoAppDelegate : NSObject  {UIWindow *window;IBOutlet TopicController *topicController;
}@property (nonatomic, retain) IBOutlet UIWindow *window;@end

在实现文件中:

#import "WindowDemoAppDelegate.h"@implementation WindowDemoAppDelegate@synthesize window;#pragma mark –#pragma mark Application lifecycle- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   // Override point for customization after application launch.[self.window addSubview:topicController.view];[self.window makeKeyAndVisible];return YES;}

然后,为了看的清楚,把TopicController.xib文件中的视图颜色改为绿色:

运行应用,会看到如下效果:

为该界面添加一个按钮:

为该按钮创建处理方法。在TopicController的头文件中:

#import <UIKit/UIKit.h>@interface TopicController : UIViewController {}-(IBAction) getDetail:(id)sender;@end

在实现文件中:

#import "TopicController.h"@implementation TopicController-(IBAction) getDetail:(id)sender{NSLog(@"get detail …");
}

在IB中,将按钮和控制器的Action连接:

再次运行应用,可看到日志中的打印内容:

按照上面创建Controller的方法,再创建一个DetailController。先把DetailController.xib的视图设置颜色,为了以后调试观察识别。

#import "TopicController.h"@implementation TopicController-(IBAction) getDetail:(id)sender{NSLog(@"get detail …");}

基本思路是找到window实例,可通过window的rootViewController属性设置新的控制器实例(比如DetailController),取代TopicController。代码可这样写:

#import "TopicController.h"#import "DetailController.h"@implementation TopicController-(IBAction) getDetail:(id)sender{NSLog(@"get detail …, window.views: %@",self.view.window.subviews);DetailController *detailController=[[DetailController alloc] initWithNibName:@"DetailController" bundle:nil];self.view.window.rootViewController=detailController;NSLog(@"window.views: %@",detailController.view.window.subviews);}

加上这部分代码后,点击按钮就可生效,产生这样的效果:

上面的代码做一下解释:

  • 首先创建了一个新的DetailController实例
  • 然后,当前的controller的view属性,可以获得window实例,通过window实例的rootViewController属性的设置,将当前的控制器替换为新的控制器
  • window对象是一个非常重要的类,你可以把它看作ios开发的画框,视图是放在画框里的,window有个subvews列表,里面可以存放多个View
  • 当设置window.rootViewController属性的时候,window会自动将该属性的UIViewController的view添加到window的subview中,这也是为什么日志中打印的window.subviews列表中有两个实例的原因

这个代码很不完备,比如存在内存泄漏,需要这样:

DetailController *detailController=[[[DetailController alloc] initWithNibName:@"DetailController" bundle:nil] autorelease];

因为,这个detailController这句话后,计数器为1了,再赋值给window.rootViewController属性,就是2了。因此这里要做自动释放。

这个代码还有个问题,就是看上去很别扭,在一个控制器代码里去创建另一个控制器。这一方面很容易出问题,另一方面,代码的结构不清晰。下面用委托模式给代码解耦,也为下一步做返回按钮做准备。

委托模式,一般用protocol来实现。先写个protocol:

    #import <UIKit/UIKit.h>@protocol SwitchViewDelegate-(void)getDetail;@end

然后,需要让UIApplicationDelegate实现类实现该protocol:

    #import <UIKit/UIKit.h>#import "TopicController.h"#import "SwitchViewDelegate.h"@interface WindowDemoAppDelegate : NSObject <UIApplicationDelegate,SwitchViewDelegate> {UIWindow *window;IBOutlet TopicController *topicController;}@property (nonatomic, retain) IBOutlet UIWindow *window;@end

在实现类中:

    #import "WindowDemoAppDelegate.h"#import "DetailController.h"@implementation WindowDemoAppDelegate@synthesize window;#pragma mark –#pragma mark Application lifecycle- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   // Override point for customization after application launch.[self.window addSubview:topicController.view];topicController.delegate=self;[self.window makeKeyAndVisible];return YES;}- (void)applicationWillResignActive:(UIApplication *)application {/*Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.*/}- (void)applicationDidEnterBackground:(UIApplication *)application {/*Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.If your application supports background execution, called instead of applicationWillTerminate: when the user quits.*/}- (void)applicationWillEnterForeground:(UIApplication *)application {/*Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.*/}- (void)applicationDidBecomeActive:(UIApplication *)application {/*Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.*/}- (void)applicationWillTerminate:(UIApplication *)application {/*Called when the application is about to terminate.See also applicationDidEnterBackground:.*/}#pragma mark –#pragma mark Memory management- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {/*Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.*/}- (void)dealloc {[window release];[super dealloc];}-(void)getDetail{DetailController *detailController=[[[DetailController alloc] initWithNibName:@"DetailController" bundle:nil] autorelease];self.window.rootViewController=detailController;}@end

另外,就是要为控制器里增加delegate属性,头文件:

    #import <UIKit/UIKit.h>#import "SwitchViewDelegate.h"@interface TopicController : UIViewController {id<SwitchViewDelegate> delegate;}@property(nonatomic,retain) id<SwitchViewDelegate> delegate;-(IBAction) getDetail:(id)sender;@end

实现文件:

#import "TopicController.h"
#import "DetailController.h"@implementation TopicController@synthesize delegate;-(IBAction) getDetail:(id)sender{NSLog(@"get detail …, window.views: %@",self.view.window.subviews);[delegate getDetail];
}

实现的效果和上面的是类似的,但是引入委托模式后,代码的架构就比较清楚了,利于以后的维护。

原文链接:http://marshal.easymorse.com/archives/4415

iOS编写最简单的界面切换应用相关推荐

  1. as实现简单微信界面切换

    要求: 1.上方栏有标题 2.中间显示内容,内容随下方栏的选择而切换 3.下方栏分成四个小板块可点击切换 实现步骤: xml文件: top.xml 界面上方栏标题. <?xml version= ...

  2. 【iOS】简单的界面制作

    文章目录 整体构思 三个视图控制器+分栏控制器 主页 购买 我的 整体构思 通过近两个周的控件学习,了解到了关于iOS的一些基本控件用法.想通过这些基本控件写一个简单的界面,类似于ZARA和得物的混合 ...

  3. iOS 简单引导界面

    代码地址如下: http://www.demodashi.com/demo/11607.html 前言 现在很多APP在用户第一次用的时候,由于用户可能并不知道其中一些功能点的时候,这个时候就需要我们 ...

  4. QT界面树形浏览与界面切换简单设计

    什么是树形浏览 像这种左侧带有可以点击切换页面就是树形浏览,树形浏览在我们做界面的时候经常能够用到并且非常的实用有木有. 我们要做的树形浏览是什么样子的 首先给你们看一下我们要做的树形浏览大概的样子. ...

  5. iOS简单的界面制作(第一周)

    [iOS]简单的界面制作 一. 整体构思 二.分栏控制器 三.首页 四."我的"页面 一. 整体构思 需要写一个简单界面,第一个界面为"首页",第二个界面为&q ...

  6. Android布局之线性布局LinearLayout(二) ----简单模仿ios端小米计算器主界面UI

    Android布局之线性布局LinearLayout(二) ----简单模仿ios端小米计算器主界面UI   今天老师的要求是让用LinearLayout布局做自己手机自带的计算器的UI设计,因为io ...

  7. 使用Java编写一个简单的 JFrame登陆注册界面(一)

    使用Java awt 及 Swing 组件编写一个简单的JFrame登陆注册界面. 示例: 下面开始介绍如何编写. 通过调用实例化一个JFrame框架,在框架内嵌入JPanel,在JPanel上进行添 ...

  8. 超强一代JupyterLab发布,可视化调试、中文显示、简单交互界面

    点上方蓝字计算机视觉联盟获取更多干货 在右上方 ··· 设为星标 ★,与你不见不散 仅作学术分享,不代表本公众号立场,侵权联系删除 转载于:机器之心 AI博士笔记系列推荐 周志华<机器学习> ...

  9. iOS 编写高质量Objective-C代码(八)

    前言: 这几篇文章是小编在钻研<Effective Objective-C 2.0>的知识产出,其中包含作者和小编的观点,以及小编整理的一些demo.希望能帮助大家以简洁的文字快速领悟原作 ...

最新文章

  1. 智能合约开发环境搭建及Hello World合约
  2. New Year Tree(dfs序+线段树+二进制)
  3. 利用Gallery和ImageView实现图片浏览器
  4. inotify+rsync
  5. 实例31:python
  6. 解决Android中的ERROR: the user data image is used by another emulator. aborting的方法
  7. 异常单据锁定涉及的数据库表
  8. JavaScript的this关键字的调用位置和绑定
  9. 基础C语言 学习总结3
  10. 文件服务器 远程访问,远程访问文件服务器
  11. 华为nova 8 Pro王者荣耀定制版曝光:内置小鲁班定制主题 电池盖镭雕小鲁班
  12. NeurIPS 2019:进入NLP的黄金时代
  13. Android短信页面
  14. python三天简单学习Day2
  15. python 颜色调整(饼状图)
  16. 管理_立项任务书怎么写——毛宇菲
  17. 适合初学者的大数据学习路线
  18. 元学习提高黑盒对抗攻击
  19. trivial、standard layout、POD和literal类型解析
  20. C/C++笔试题(很多)

热门文章

  1. 校门外的树(信息学奥赛一本通-T1107)
  2. java junit 怎么写_使用JUnit测试java代码
  3. 你离云计算还差一个云管平台(CMP)
  4. linux 内核设备管理模型sysfs(进阶篇)
  5. Pandas+Pyecharts 数据分析与可视化 3D地图+柱状图
  6. python比特币挖矿_比特币如何挖矿(挖矿原理)-工作量证明
  7. 如何查看git是否添加到环境变量 - cmd篇
  8. 趣味娱乐小程序源码多流量主 趣味制作/藏头诗/隐藏图
  9. 天人短文网站系统v5.53源码
  10. python3抓取b站弹幕_python3写爬取B站视频弹幕功能