ページ

2014年12月4日木曜日

[Obj-C, Swift] Modal Segue 基本(後半)

【Obj-C, Swift】 Modal Segue 基本(後半)

はじめに

前回の続き,Modal Segue の基本的実装(Swift版)です。

【Obj-C, Swift】 Modal Segue 基本(前半)
 【Link】  ジャンル:UI,StoryBoard 関係

比較して書くの初めてでさぁやるぞーって意気込んで作業に入ったものの...
あっさり終わってしまい,拍子抜けでした。

目指す動作

前回と同じです。
起動したらボタンが 2 つあって,各ボタンの遷移先が異なる ViewController になる。
各遷移先から元の画面に戻ってこれる。初級だとこの辺よく使いたがるところですね。
開発はじめたころはボタンから遷移先に Modal 引っ張ってたんですが,
遷移先が複数になるとごちゃごちゃしてくるので,Storyboard Segue の
Identifier を設定してそれごとに遷移させる書き方を使います。

実装

画像はクリックで RAW 画像見れます。見難いところあればお使いください。
前回は,Objective-C で実装しました。今回は Swift で書きます。
Single View Application → プロジェクト名適当 → Swift

下記画像のように,ViewController,部品を設置しました。作業は前回と同じです。
Ctr ボタン押しながらドラッグして present modality をそれぞれ選択し,
各 Storyboard Segue の Identifier にそれぞれ Segue1, Segue2 と名前をつけます。


UIButton(1st, 2nd) を直接 ViewController.swift に Action で結びつける。


続けて追加された ViewController.swift の Action 動作のプログラムを下記のように実装する。

    @IBAction func segueAction(sender: AnyObject) {
        
        // Objective-C だと
        // [self performSegueWithIdentifier:@"Segue1" sender:self];
        performSegueWithIdentifier("Segue1",sender: nil)
    }

    @IBAction func segueAction2(sender: AnyObject) {
        
        performSegueWithIdentifier("Segue2",sender: nil)
    }

そっくりですよねー。少し省かれてる感じ。

次に File -> New -> File... -> Cocoa Touch Class で,
新たに SegueViewController を SubClass:UIViewController で作成。


クラス名を指定する。(今回は同じ実装なのでどちらも同じクラス名にする)


Back ボタンを,SegueViewController.swift に下記画像のように紐付けする。
もう一つの Back ボタンにも紐付けする。二重丸◉からドラッグしてつなげればいいです。


SegueViewController.swift の Action メソッドに下記のように
元の ViewController に戻る処理を書く。

    @IBAction func BackAction(sender: AnyObject) {
        
        // Objective-C だと
        // [self dismissViewControllerAnimated:YES completion:nil];
        self.dismissViewControllerAnimated(true, completion: nil)
        
    }

ほぼ一緒なきがする。

これで実装自体は終わりです。動作させてみると下記のようになります。
動作はもちろん同じなんですが…
http://youtu.be/TQnb5IKpFDY


今回のコード

// ViewController.swift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func segueAction(sender: AnyObject) {
        
        // Objective-C だと
        // [self performSegueWithIdentifier:@"Segue1" sender:self];
        performSegueWithIdentifier("Segue1",sender: nil)
    }

    @IBAction func segueAction2(sender: AnyObject) {
        
        performSegueWithIdentifier("Segue2",sender: nil)
    }
    
}

// SegueViewController.swift
import UIKit

class SegueViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func BackAction(sender: AnyObject) {
        
        // Objective-C だと
        // [self dismissViewControllerAnimated:YES completion:nil];
        self.dismissViewControllerAnimated(true, completion: nil)   
    }
}

今回感じた Objective-C と Swift の違い

h ファイル,m ファイルが一つにまとまり swift ファイル一つというのが思い切ったなと。
結構律儀に書きたがる私からすれば別れてくれていた方がいい気もしますが,
コードの少なさ,見通しのよさは Swift の方がいいのかなと思ったり。
今回は Storyboard 主体で実装したので,コードが少ない感じになってしまいました。
違いも大きくでた感じもありませんでした。動作も単純なので速度も体感では感じられなかった。

おわりに

Modal Segue のテストアプリを Swift で実装しました。
わかっていたことですが,ほとんどの表現が使い回しになりました。
次回は,Modal Segue と 絡ませた UserDefault について比較してみようと思います。
ご覧いただきありがとうございました。ここはこうしたほうがいいなどの
ご意見もいただけると勉強になります。おねがいします。

今回のサンプルコード:https://github.com/MilanistaDev/swift_modal_segue_basic

Twitterボタン
Twitter:@milanista_2nd
Apple 関係のこと,ガジェット系,ブログ更新などいろいろつぶやいています。
よろしければフォローお願いします!

初めてのアプリ作りました。無料なのでよろしければ是非。
 
FLAT Weather Clock

カテゴリ: ユーティリティ, 天気
価格:無料

Category: Utilities, Weather
Price: Free



 サポートページ日本語版:リンク
 Support Website(English):LINK

2014.12.04 Milanista



TAG index

0 件のコメント:

コメントを投稿