CS193p Lecture 6 - UINavigation, UITabBar
抽象类(Abstract):指的是这个类不能被实例化,只能被继承;
OC中没有关键词来标明某个类是抽象类,只能在注释中标注一下;
抽象类中的抽象方法,必须是public的,使方法称为public的方法是,将其声明放置到 .h 文件的interface中;
Multiple MVCs in an Application
如何添加多个MVC呢?
1. 在 object library 中找到 UIViewController,拖拽到 storyboard;
2. New - File,创建 UIViewController 的子类;
3. 在 identity inspector 中将storyboard中的 UIViewController 关联到新建的 UIViewController 的子类;
4. 添加完MVC后,你就可以在 view 中加些button、label,还有outlets、actions之类的;
如何将多个MVC展现给用户呢?
(下面这个解释有点抽象,结合后面的例子来理解吧)
you use a controller whose view is other MVCs.
有一个控制器,这个控制器的视图是其他MVC。
UINavigationController
组成:
左上角:返回按钮
上部:标题(title)
右上角的按钮:an NSArray of UIBarButtonItems
底部的按钮:an NSArray of UIBarButtonItems
rootViewController
设置根控制器
每次向 UINavigationController 中压入新MVC时,都是从storyboard中新建一个,在堆中新实例化一个;返回后,它就消失被释放;
所以这些MVC需要知道如何变为活动状态,准备出现在屏幕上,做要做但事情,完成之后,保存工作进度,然后离开。
如果某些数据需要继续使用,就需要对它进行保存。可以通过向将你压进来的那个MVC发送消息来实现,需要借助不可视结构化通信(blind structured communication)来实现。
压入操作(push)
segue
A segue is just when you're going to move or segue, from one MVC to another.
这里用到的称为:push segue
scene:表示一个控制器和一个对应视图的组合;
创建一个segue的方法:
从segue开始的地方按住control键,拖动至想要segue到的视图控制器上;
弹出操作(pop)
方法一:点击左上角返回按钮;
方法二:
- (void)popViewControllerAnimated:(BOOL)animated
[self.navigationController popViewControllerAnimated:YES];
如果你是一个视图控制器(view controller),且嵌套在一个导航控制器(navigation controller)中,那你就拥有一个属性:navigationController,指向当前所在的导航控制器;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if (segue.identifier = "DoSomething") {
if ([segue.destinationViewController isKindOfClass:[DoSomethingVC class]]) {
DoSomethingVC *doVC = (DoSomethingVC *)segue.destinationViewController;
doVC.needInfo = ...;
}
}
}
两个重要属性:
1. segue.identifier:因为一个视图可能可以segue到不同的视图,所以要通过identifier来区分;
2. segue.destinationViewController:为了确保segue过去的视图是我们想要的视图类型;
doVC.needInfo就是segue到目的视图之前需要做的准备工作;
注意:
当 prepareForSegue: sender: 被调用时,目标MVC的输出口(outlet)并没有设置好,也就是说,它是位于awakeFromNib和viewDidLoad之间被调用的;
Demo
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"Statistic Text"]) {
if ([segue.destinationViewController isKindOfClass:[TextStatisticsViewController class]]) {
TextStatisticsViewController *tsVC = (TextStatisticsViewController *)segue.destinationViewController;
tsVC.textToAnalyse = self.body.textStorage;
}
}
}
- (void)setTextToAnalyse:(NSAttributedString *)textToAnalyse{
_textToAnalyse = textToAnalyse;
[self updateUI];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self updateUI];
}
- (void)updateUI{
self.colorLabel.text = [NSString stringWithFormat:@"%lu colored characters",
[[self characterWithAttribute:NSForegroundColorAttributeName] length]];
self.outlineLabel.text = [NSString stringWithFormat:@"%lu outlined characters",
[[self characterWithAttribute:NSStrokeWidthAttributeName] length]];
}
- (NSAttributedString *)characterWithAttribute:(NSString *)attributeName{
NSMutableAttributedString *character = [[NSMutableAttributedString alloc] init];
int index = ;
while (index < [self.textToAnalyse length]) {
NSRange range;
id value = [self.textToAnalyse attribute:attributeName atIndex:index effectiveRange:&range];
if (value) {
[character appendAttributedString:[self.textToAnalyse attributedSubstringFromRange:range]];
index = (int)(range.location + range.length);
} else {
index ++;
}
}
return character;
}
分析下这个Demo,虽然比较简单,但是包含对设计模式是很通用的,熟悉之:
UITabBarController
CS193p Lecture 6 - UINavigation, UITabBar的更多相关文章
- CS193p Lecture 11 - UITableView, iPad
UITableView 的 dataSource 和 delegate dataSource 是一种协议,由 UITableView 实现,将 Model 的数据给到 UITableView: del ...
- CS193p Lecture 10 - Multithreating, UIScrollView
Multithreating(多线程) 网络请求例子: NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithStrin ...
- CS193p Lecture 9 - Animation, Autolayout
Animation(动画) Demo Dropit续 Autolayout(自动布局) 三种添加自动布局的方法: 使用蓝色辅助虚线,右键选择建议约束(Reset to Suggested Constr ...
- CS193p Lecture 8 - Protocols, Blocks and Animation
一.协议(Protocols) 1. 声明协议 @protocol Foo <Xyzzy, NSObject> // ... @optinal // @required //... @en ...
- CS193p Lecture 7 - Views, Gestures
Views 如何绘制自定义图像 Gestures 如何处理用户手势操作 Views 1.它是基本的构造块,代表屏幕上一块矩形区域,定义了一个坐标空间,在此空间中可以绘制,可以添加触控事件: 2.它是分 ...
- CS193p Lecture 5 - View Controller Lifecycle
1. UITextView @property(nonatomic,readonly,retain) NSTextStorage *textStorage 是 NSMutableAttributedS ...
- CS193p Lecture 4 - Foundation, Attributed Strings
消息机制 调用一个实例(instance)的方法(method),就是向该实例的指针发送消息(message),实例收到消息后,从自身的实现(implementation)中寻找响应这条消息的方法. ...
- [C2P3] Andrew Ng - Machine Learning
##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...
- uiscrollView UINavigation和uitabbar添加约束的问题
首先是层次问题, 我的storyboard中 底层是一个View(viewcontroller自带)上面添加了一个UIScrollview(添加约束, 上下左右全为0), 在UIScrollview上 ...
随机推荐
- [Xcode 实际操作]八、网络与多线程-(5)使用UIApplication对象发送邮件
目录:[Swift]Xcode实际操作 本文将演示如何使用应用程序单例对象,发送邮件的功能. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] 注:需要使用真机进行测 ...
- [Xcode 实际操作]九、实用进阶-(2)遍历设备(输出系统)上的所有字体
目录:[Swift]Xcode实际操作 在实际工作中,经常需要调整界面元素的字体种类. 本文将演示输出系统提供的所有字体,方便检索和使用. 在项目导航区,打开视图控制器的代码文件[ViewContro ...
- 大数(string 之间的快速幂)
//字符串的乘法 string multi(string a, string b){ ], len = a.length() + b.length(); memset(arr, , sizeof ar ...
- socket连接 代码
dispatch_sync(dispatch_get_global_queue(, ), ^{ // 处理耗时操作的代码块... // 创建socket /* 1.AF_INET: ipv4 执行ip ...
- CATIA 使用技巧--转换出轻巧的tif格式文件
问题描述: 我们在与客户和供应商打交道的过程中经常需要TIF格式2D图纸文件,而默认的CATIA设置保存出来TIF文件非常大,不利于保存和传送.对于该问题,我们可以通过修改CATIA的默认设置选项,将 ...
- 082 Remove Duplicates from Sorted List II 有序的链表删除重复的结点 II
给定一个有序的链表,删除所有有重复数字的节点,只保留原始列表中唯一的数字.例如:给定 1->2->3->3->4->4->5 ,则返回 1->2->5给 ...
- 17995 Stupid thief 组合数学
17995 Stupid thief 时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: 不限定 Description A stupid thie ...
- 牛客网Java刷题知识点之什么是迭代器
不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?query=&asc=true&order=&page=20 ...
- 关于Winform控件调用插入点(光标)的用法
我们自定义控件中可能会有一些光标的使用,比如插入文字和图片提示,下面是调用WIN32 API的光标用法 Winform控件调用插入点的用法 // 导入处理光标的 Windows 32 位 API // ...
- 剑指tomcat之多项目部署问题
部署项目时遇到的问题,tomcat的webapps文件夹中有两个war包,但每次启动Tomcat服务时,只会默认启动一个war包. 解决方案一:在Tomcat主页中进入应用管理页面,手动开启项目.(进 ...