Start Developing iOS Apps Today をやってみる その2
Designing a User Interface
Use Auto Layout to Position Views
When you start positioning views in your storyboard, you need to consider a variety of situations. iOS apps run on a number of different devices, with various screen sizes, orientations, and languages.
To help you use views to create a versatile interface, Xcode offers a tool called Auto Layout. Auto Layout is a system for expressing relationships between views in your app’s user interface.
スクリーンサイズ、向き、言語の違いを考えないといけないが、Auto Layoutがよろしくやってくれる
Defining the Interaction
View Controllers
After you’ve built a basic view hierarchy, your next step is to control the visual elements and respond to user input. In an iOS app, you use a view controller (UIViewController) to manage a content view with its hierarchy of subviews.
viewの階層を作ったらview controllerを使ってviewを扱う
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.
content viewの数だけview controllerを作る
Actions
An action is a piece of code that’s linked to some kind of event that can occur in your app. When that event takes place, the code gets executed. You can define an action to accomplish anything from manipulating a piece of data to updating the user interface.
actionはアプリ上のイベントが起こった時にUIの更新操作をするためのもの。
You define an action by creating and implementing a method with an IBAction return type and a sender parameter.
- (IBAction)restoreDefaults:(id)sender;
The sender parameter points to the object that was responsible for triggering the action. The IBAction return type is a special keyword; it’s like the void keyword, but it indicates that the method is an action that you can connect to from your storyboard in Interface Builder (which is why the keyword has the IB prefix). You’ll learn more about how to link an IBAction action to an element in your storyboard in Tutorial: Storyboards.
senderというパラメータを受け取ってIBActionを返すmethodを実装する senderはactionをtriggerしたオブジェクト IBActionはstroyboardと繋げるためのspecial keyword
Outlets
Outlets provide a way to reference objects from your interface—the objects you added to your storyboard—from source code files. You create an outlet by Control-dragging from a particular object in your storyboard to a view controller file.
outletを使うとstoryboadで作ったUIをコードから参照することができる
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).
複数のviewがあればUINavigationControllerを使って遷移を管理する必要がある
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.
複数のview controllerはnavigation stackと呼ばれるもので管理される navigation stackはlast-in, first-outの集合 はじめのアイテムはroot view controllerで、これはstackからpopされない 他のcontrollerはnavigation stack にpushまたはpopできる
Use Storyboards to Define Navigation
In the first tutorial, the storyboard you worked with had one scene. In most apps, a storyboard is composed of a sequence of scenes, each of which represents a view controller and its view hierarchy. Scenes are connected by segues, which represent a transition between two view controllers: the source and the destination.
storyboardで複数のview controllerを表現吸う時にはsegueを使う
There are several types of segues you can create:
- Push. A push segue adds the destination view controller to the navigation stack. Push segues may only be used when the source view controller is connected to a navigation controller.
- Modal. A modal segue is simply one view controller presenting another controller modally, requiring a user to perform some operation on the presented controller before returning to the main flow of the app. A modal view controller isn’t added to a navigation stack; instead, it’s generally considered to be a child of the presenting view controller. The presenting view controller is responsible for dismissing the modal view controller it created and presented.
- Custom. You can define your own custom transition by subclassing UIStoryboardSegue.
- Unwind. An unwind segue moves backward through one or more segues to return the user to an existing instance of a view controller. You use unwind segues to implement reverse navigation.
segueの種類
- push:矢印の先のview controllerをnavigation stackにpushする
- modal:navigation stackにはpushしない。modal view controllerが終わると元のview controllerに戻る
- custom:UIStoryboardSegueのサブクラスを定義することで遷移をカスタマイズできる
- unwind:どのview controllerに戻るか定義できる
今回はここまで