view types - view常见类型
Align : 校准。创建校准限制,比如将一个view在他的容器中居中或者校准两个view的左边缘
Pin: 创建间距限制,比如定义一个view的高度或者指定他到另一个view的水平距离。
Resolve Auto Layout Issues: 通过添加或者重新设置建议限制来解决layout问题。
Resizing Behavior: 指明resizing如何作用于限制。
View Controllers
A view controller isn’t part of the view hierarchy, and it’s not an element in your interface. Instead, it manages the view objects in the hierarchy and provides them with behavior. Each content view hierarchy that you build in your storyboard needs a corresponding view controller, responsible for managing the interface elements and performing tasks in response to user interaction. This usually means writing a custom UIViewController
subclass for each content view hierarchy. If your app has multiple content views, you use a different custom view controller class for each content view.
view controller不是view 层级关系中的一部分也不是你interface的一个元素。他管理层级关系中的view对象 and provides them with behavior.每一个你在storyboard中创建的content view都需要一个对应的view controller来负责管理interface元素和行为任务。这通常意味着为每一个content view层级来创建自定义的UIViewController子类。如果你的app有多个content view,你可以为每个content view创建不同的自定义view controller。
View controllers play many roles. They coordinate the flow of information between the app’s data model and the views that display that data, manage the life cycle of their content views, and handle orientation changes when the device is rotated. But perhaps their most obvious role is to respond to user input.
view controller扮演很多角色。他们协调app数据模型间的信息流程和展示数据的views。管理content view的生命周期并且当设备旋转时处理方向变动。但是或许他们最主要的作用是响应用户输入。
You also use view controllers to implement transitions from one type of content to another. Because iOS apps have a limited amount of space in which to display content, view controllers provide the infrastructure needed to remove the views of one view controller and replace them with the views of another.
你也使用view controller来执行从一个content到另一个content的传递。因为ios app有显示content的空间限制,view controller提供从一个view controller移除view并且使用其他view来替换的工作。
To define interaction in your app, you make your view controller files communicate with the views in your storyboard. You do this by defining connections between the storyboard and source code files through actions and outlets.
为了定义你的app中的交互,你另storyboard中的view controller files和views之间可以通信。你利用actions和outlets来定义storyboard和source code files之间的联系。
Navigation Controllers
If your app has more than one content view hierarchy, you need to be able to transition between them. For this, you’ll use a specialized type of view controller: a navigation controller (UINavigationController
). A navigation controller manages transitions backward and forward through a series of view controllers, such as when a user navigates through email accounts, inbox messages, and individual emails in the iOS Mail app.
如果你有多个content view层,你需要能够在他们中间过渡。为了实现这个,你就会使用一种特定类型的view controller:一个navigation controller(UINavigationController).一个navigation controller管理在一系列view controllers之间向后和向前传递。
The set of view controllers managed by a particular navigation controller is called its navigation stack. The navigation stack is a last-in, first-out collection of custom view controller objects. The first item added to the stack becomes the root view controller and is never popped off the stack. Other view controllers can be pushed on or popped off the navigation stack.
被naviagation controller管理的一系列view controllers叫做他的navigation stack。导航栈。navigation stack是一个后进先出的自定义view controller 对象集合。第一个添加进去的item就成为了root view controller。并且永远不会popped off堆栈。其他的view controllers会被压入或者推出navigation stack。
Although a navigation controller’s primary job is to manage the presentation of your content view controllers, it’s also responsible for presenting custom views of its own. Specifically, it presents a navigation bar—the view at the top of the screen that provides context about the user’s place in the navigation hierarchy—which contains a back button and other buttons you can customize. Every view controller that’s added to the navigation stack presents this navigation bar. You are responsible for configuring the navigation bar.
尽管一个navigation controller的首要任务是管理你的content view controllers。他也负责自定义view的行为。特别是,他呈现一个naviagtion bar-在屏幕上方的一个view-包含一个back按钮和其他你自定义的按钮。每一个添加到navigation stack中的view controller都显示这个navigation bar。你负责配置navigation bar。
You generally don’t have to do any work to pop a view controller off of the navigation stack; the back button provided by the navigation controller handles this for you. However, you do have to manually push a view controller onto the stack. You can do this using storyboards.
你通常不需要为弹出一个view controller做任何事情。navigation controller提供的返回按钮负责这个。但是你需要手动push一个view controller进stack中,你可以利用storyboards来实现这个目的。
Working with Foundation
Value Objects
Some examples of value objects in the Foundation framework are:
NSString
andNSMutableString
NSData
andNSMutableData
NSDate
NSNumber
NSValue
Collection Objects
NSArray
andNSMutableArray
. An array is an ordered collection of objects. You access an object by specifying its position (that is, its index) in the array. The first element in an array is at index 0 (zero).NSSet
andNSMutableSet
. A set stores an unordered collection of objects, with each object occurring only once. You generally access objects in the set by applying tests or filters to objects in the set.NSDictionary
andNSMutableDictionary
. A dictionary stores its entries as key-value pairs; the key is a unique identifier, usually a string, and the value is the object you want to store. You access this object by specifying the key.
Arrays
Sets
A set (NSSet
) object is similar to an array, but it maintains an unordered group of distinct objects.
Because sets don’t maintain order, they offer faster performance than arrays do when it comes to testing for membership.
Because the basic NSSet
class is immutable, its contents must be specified at creation, using either an initializer or a class factory method.
NSSet *simpleSet =
[NSSet setWithObjects:@"Hello, World!", @42, aValue, anObject, nil];
As with NSArray
, the initWithObjects:
and setWithObjects:
methods both take a nil
-terminated, variable number of arguments. The name of the mutable NSSet
subclass is NSMutableSet
.
Sets store only one reference to an individual object, even if you try adding an object more than once.
NSNumber *number = @42;
NSSet *numberSet =
[NSSet setWithObjects:number, number, number, number, nil];
// numberSet only contains one object
Dictionaries
Tutorial: Add Data
创建一个自定义的类来显示有用的数据。
This tutorial teaches you how to:
Work with common Foundation classes 同一般的foundation类合作
Create custom data classes 创建自定义的data classes
Implement a data source and delegate protocol 执行一个data source和delegate protocol
Pass data between view controllers 在view controller之间传递数据
Create a Data Class
To create the ToDoItem class
-
Choose File > New > File (or press Command-N).
A dialog appears that prompts you to choose a template for your new file.
On the left, select Source under iOS.
Select Cocoa Touch Class, and click Next.
In the Class field, type
ToDoItem
.-
Choose NSObject from the “Subclass of” pop-up menu. //继承自 NSObject 类
-
Click Next.
The save location defaults to your project directory.
The Group option defaults to your app name, ToDoList.
In the Targets section, your app is selected and the tests for your app are unselected.
Leave these defaults as they are, and click Create.
To configure the ToDoItem class //配置todoitem类
In the project navigator, select
ToDoItem.h
.-
Add the following properties to the interface so that the declaration looks like this:
@interface ToDoItem : NSObject
@property NSString *itemName;
@property BOOL completed;
@property (readonly) NSDate *creationDate;
@end
Load the Data //加载数据
-
In the project navigator, select
ToDoListTableViewController.m
.Because the array of items is an implementation detail of your table view controller, you declare it in the
.m
file instead of the.h
file. This makes it private to your custom class. -
Add the following property to the interface category Xcode created in your custom table view controller class. The declaration should look like this:
@interface ToDoListTableViewController ()
@property NSMutableArray *toDoItems;
@end
-
Find the
viewDidLoad
method. The template implementation looks like this:- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
The template implementation of this method includes comments that were inserted by Xcode when it created
ToDoListTableViewController
. Code comments like this provide helpful hints and contextual information in source code files, but you don’t need them for this tutorial. Feel free to delete the comments. -
Update the method implementation to allocate and initialize the
toDoItems
array in theviewDidLoad
method:- (void)viewDidLoad {
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
}