【Obj-C, Swift】 Modal Segue 基本(前半)
はじめに
Modal Segue の基本的実装です。慣れたら Objective-C だと 10 分もかからないくらいの作業だけど,
Swift だとどんな感じなんだろうって Objective-C と比較しながら実装しました(まだ書いてはないw)。
画面遷移って理解できるまで結構苦労するところかなと思います。
初級の方の役に立てばいいんですけど,個人的な趣味,備忘録なので気にしません!
前半は,Objective-C での実装です。やり方は何通りかあると思います。
目指す動作
起動したらボタンが 2 つあって,各ボタンの遷移先が異なる ViewController になる。各遷移先から元の画面に戻ってこれる。初級だとこの辺よく使いたがるところですね。
開発はじめたころはボタンから遷移先に Modal 引っ張ってたんですが,
遷移先が複数になるとごちゃごちゃしてくるので,Storyboard Segue の
Identifier を設定してそれごとに遷移させる書き方を使います。
実装
画像はクリックで RAW 画像見れます。見難いところあればお使いください。まずは,Objective-C から。
Single View Application → プロジェクト名適当 → Objective-C
新しく ViewController を用意して下記画像のように Ctr ボタン押しながらドラッグ。


今回は 2 つに結びつけ,それぞれの Storyboard Segue の
Identifier に下記画像(Segue1, Segue2)のように名前をつける(もちろん任意)。


UIButton を 2 つ設置し,.h に Action で結びつける。

.m に Action 動作のプログラムが追加されるので下記のように実装する。
- (IBAction)segueAction:(id)sender { // ボタンが押されたらStoryboard Segue Identifer に遷移 // Identifer ごとに遷移可能なのでボタンから直接遷移よりかは扱いやすい // Segue1 を任意で変更 [self performSegueWithIdentifier:@"Segue1" sender:self]; } - (IBAction)segueAction2:(id)sender { [self performSegueWithIdentifier:@"Segue2" sender:self]; }
次に File -> New -> File... -> Cocoa Touch Class で,
新たに SegueViewController を SubClass:UIViewController で作成。

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

Back ボタンを設置して,新しく追加したファイルの .h に下記画像のように紐付けする。

もう一つの Back ボタンにも紐付けする。二重丸◉からドラッグしてつなげればいいです。

.m に Action のメソッドが追加されてるので下記のように
元の ViewController に戻る処理を書く。
- (IBAction)BackAction:(id)sender { // Back ボタンが押されたら前の画面に遷移する // 確か iOS 7 からこの書き方に変わったはず [self dismissViewControllerAnimated:YES completion:nil]; }
これで実装自体は終わりです。動作させてみると下記のようになります。
http://youtu.be/pW63Rgd0O_s
今回のコード
// ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController - (IBAction)segueAction:(id)sender; - (IBAction)segueAction2:(id)sender; @end
// ViewController.m #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)segueAction:(id)sender { // ボタンが押されたらStoryboard Segue Identifer に遷移 // Identifer ごとに遷移可能なのでボタンから直接遷移よりかは扱いやすい // Segue1 を任意で変更 [self performSegueWithIdentifier:@"Segue1" sender:self]; } - (IBAction)segueAction2:(id)sender { [self performSegueWithIdentifier:@"Segue2" sender:self]; } @end
// SegueViewController.h #import <UIKit/UIKit.h> @interface SegueViewController : UIViewController - (IBAction)BackAction:(id)sender; @end
// SegueViewController.m #import "SegueViewController.h" @interface SegueViewController () @end @implementation SegueViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)BackAction:(id)sender { // Back ボタンが押されたら前の画面に遷移する // 確か iOS 7 からこの書き方に変わったはず [self dismissViewControllerAnimated:YES completion:nil]; } @end
おわりに
Modal Segue のテストアプリを Objective-C で実装しました。一気に書こうと思ったけど意外に長くなりそうだったので分けました。
コード自体は相当短いのになぁ〜。後半は出来次第書きます。
ご覧いただきありがとうございました。ここはこうしたほうがいいなどの
ご意見もいただけると勉強になります。おねがいします。
今回のサンプルコード:https://github.com/MilanistaDev/oc_modal_segue_basic

Twitter:@milanista_2nd
Apple 関係のこと,ガジェット系,ブログ更新などいろいろつぶやいています。
よろしければフォローお願いします!
初めてのアプリ作りました。無料なのでよろしければ是非。
FLAT Weather Clock
カテゴリ: ユーティリティ, 天気
価格:無料
Category: Utilities, Weather
Price: Free
サポートページ日本語版:リンク
Support Website(English):LINK
2014.12.03 Milanista

0 件のコメント:
コメントを投稿