LoginSignup
12
13

More than 5 years have passed since last update.

【第3回】Swiftアプリ開発チュートリアル -Food Tracker- のお供に(Work with View Controllers 編)

Last updated at Posted at 2015-10-22

はじめに

AppleのSwiftアプリ開発チュートリアルを進めていて出てきた関数やクラスなどについて、調べたことを残していきます。
チュートリアルを進めながら、ざっくりとどういうものかを把握するためのお供になればと思います。

初学者のため、間違い等あればご指摘ください。 :bow:

第1回:Swiftアプリ開発チュートリアル -Food Tracker- のお供に(Build a Basic UI 編)
第2回:Swiftアプリ開発チュートリアル -Food Tracker- のお供に(Connect the UI to Code 編)
第3回:Swiftアプリ開発チュートリアル -Food Tracker- のお供に(Work with View Controllers 編)←今回はここ
第4回:Swiftアプリ開発チュートリアル -Food Tracker- のお供に(Implement a Custom Control 編)
第5回:Swiftアプリ開発チュートリアル -Food Tracker- のお供に(Define Your Data Model 編)

チュートリアルページ

以下のような構成でレッスンが進んでいくので、それに沿って進めていきます。

  • Building the UI
    • Build a Basic UI
    • Connect the UI to Code
    • Work with View Controllers(今回はここ)
    • Implement a Custom Control
    • Define Your Data Model
  • Working with Table Views
    • Create a Table View
    • Implement Navigation
    • Implement Edit and Delete Behavior
    • Persist Data

Work with View Controllers

今回は以下のレッスンで出てきたものについて見ていきます。

■ UIImageView

画面上での画像の表示を管理するクラス。
単一の画像の表示や複数画像を用いたアニメーションを行う機能もある。

チュートリアルでの使用例

ViewController.swift
@IBOutlet weak var photoImageView: UIImageView!

ドキュメント

■ UITapGestureRecognizer

UIGestureRecogniterのサブクラスでタップを認識する。

numberOfTapsRequiredプロパティで認識するタップの回数、
numberOfTouchesRequiredプロパティでタップする指の本数を指定する。

チュートリアルでの使用例

ViewController.swift
    @IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {

ドキュメント

■ UIImagePickerController

画像や動画の選択または撮影を行う際に用いるクラス。

後述のUIImagePickerControllerDelegateUIImagePickerControllerDelegateの採用が必要。

チュートリアルでの使用例

ViewController.swift
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()

ドキュメント

■ UIImagePickerControllerDelegate

フォトライブラリーから画像を選択する等、イメージピッカーを使用する際に必要なメソッドが定義されたプロトコル。

チュートリアルでは、UIImagePickerControllerを使用してフォトライブラリーから画像を選択するため、このプロトコルを実装する必要がある。

チュートリアルでの使用例

ViewController.swift
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

ドキュメント

■ UINavigationControllerDelegate

画面遷移を管理するUINavigationControllerを使用する際に必要なプロトコル。

チュートリアルでは、UINavigationControllerのサブクラスであるUIImagePickerControllerを使用するため、このプロトコルを実装する必要がある。

チュートリアルでの使用例

ViewController.swift
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

ドキュメント

■ UIImagePickerControllerSourceType

画像をどこから取得するかを指定できる。

列挙型で宣言されており、

  • PhotoLibrary(フォトライブラリ)
  • Camera(カメラを起動して撮影したもの)
  • SavedPhotosAlbum(カメラロールアルバム)

から選択可能。

UIImagePickerControllerではsourceTypeプロパティとして定義されている。

チュートリアルでの使用例

ViewController.swift
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .PhotoLibrary

ドキュメント

■ presentViewController

UIViewControllerのメソッド。
指定したビューコントローラをモーダルで表示する。

チュートリアルでは、imagePickerControllerを指定してフォトライブラリを表示させている。

チュートリアルでの使用例

ViewController.swift
presentViewController(imagePickerController, animated: true, completion: nil)

ドキュメント

■ imagePickerControllerDidCancel

UIImagePickerControllerDelegateプロトコルによって定義されており、実装する必要があるメソッド。
呼び出したイメージピッカービューでユーザがキャンセルをした時に呼ばれる。

この中でdismissModalViewControllerAnimatedメソッドを実装してイメージピッカーの表示を表示をなくす。

チュートリアルでの使用例

ViewController.swift
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    // Dismiss the picker if the user canceled.
    dismissViewControllerAnimated(true, completion: nil)
}

ドキュメント

■ dismissViewControllerAnimated

モーダルで表示されているビューコントローラを閉じる。

チュートリアルでの使用例

ViewController.swift
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    // Dismiss the picker if the user canceled.
    dismissViewControllerAnimated(true, completion: nil)
}

ドキュメント

■ imagePickerController

UIImagePickerControllerDelegateプロトコルによって定義されており、実装する必要があるメソッド。
画像が選択された時に呼ばれるメソッドで、選択された画像に何か処理をする際にここに実装する。

チュートリアルでの使用例

ViewController.swift
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    // The info dictionary contains multiple representations of the image, and this uses the original.
    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    // Set photoImageView to display the selected image.
    photoImageView.image = selectedImage

    // Dismiss the picker.
    dismissViewControllerAnimated(true, completion: nil)
}

ドキュメント


参考サイト

http://iphone-tora.sakura.ne.jp/uiimageview.html
https://akira-watson.com/iphone/uigesturerecognizer.html

次のレッスン

第4回:Swiftアプリ開発チュートリアル -Food Tracker- のお供に(Implement a Custom Control 編)

12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13