SlideShare a Scribd company logo
1 of 16
第四回iOSアプリ勉強会
今日の内容
・前回の復習
・ViewControllerについて
・BMIアプリを完成させよう!!
・Delegateについて
・StoryBoardを使わないで実装してみる
前回の復習
XcodeのPlaygroundを開いてください
前回の復習
・Classについて
Classは以下のように宣言し、
Classの中で扱う変数をプロパティ、関数をメソッドと呼ぶ
←Classのインスタンス生成方法
プロパティやメソッドにアクセスする為
には「.」をつける
前回の復習
・Classの継承
クラス名の後に「:」をつけ継承する親クラスのクラス名を書く
親クラスがイニシャライザを持っている場合、子クラスでは親クラスのイニシャライザを呼ぶ必要
がある。
親クラスのメソッドを上書きする場合は override修飾子をつける
おさらい
プロパティに「身長」「体重」
BMIの計算をするメソッド
判定をするメソッド
を持つクラスを作成
StoryBoardを使って簡単な画面
を作成(←ここまで)
今回はBMI計算アプリを完成させ
ます
前回作った画面
ViewControllerについて
メインとなるViewControllerはUIViewControllerクラスを継承したクラス
UIViewControllerに定義されている viewDidLoad(), didReceiveMemoryWarning() を上書きしている
viewDidLoad() は画面の読み込みが完了したら呼ばれるメソッドで基本的にアプリの処理はこのメソ
ッド中に書く
didReceiveMemoryWarning() はメモリが不足した時呼ばれるメソッドで今は気にしなくて良い
ViewControllerについて
viedDidLoad()内に書いた処理はビルドすれば実行される。
上記の例はアラートを表示させる
StoryBoardを使わないで実装する場合(@IBOutletを使わない場合)はここに各部品を表示させる処理
を書く必要がある
Delegateについて
Delegateは直訳すると「移譲」とか「委任」とか
処理を任せる先を決めるのがdelegateの役割で、処理の定義自体はすでにされている
UITextFieldを例に解説していきます
画面に配置するText FieldはUITextFieldクラスを継承している。(UITextFieldのインスタンス)
UITextFieldの各種処理(Return Keyを押す等々)は
UITextFieldDelegateクラス (protocol) に定義されている
なので各種処理を行いたいViewControllerでUITextFieldDelegateクラスを継承する必要が有る
protocolとは?
ちょっと話が脱線しますが「プロトコル (protocol) 」について軽く説明します
簡単に言うとクラスの挙動を決めるための設計図
JavaやC#でいうインターフェースに似たもの(宣言ファイル、インターフェースのみ定義)
画面に配置するText FieldはUITextFieldクラスを継承している。(UITextFieldのインスタンス)
UITextFieldの各種処理(Return Keyを押す等々)は
UITextFieldDelegateクラス (protocol) に定義されている
なので各種処理を行いたいViewControllerでUITextFieldDelegateクラスを継承する必要が有る
protocolとは・・?
プロトコル(test)内にこのプロトコルを継承
するクラス(構造体、列挙型)内で実装する関
数を定義する。
プロトコル内で使用するプロパティの宣言も
できる
プロトコルを継承したクラス(useProtocol)内
で宣言した関数を実装する。
この時プロトコル内で定義した関数を実装し
ない場合エラーになる
「実装してもしなくてもいいよ」っていう便
利な宣言は今のとこSwiftではできないっぽ
いのでおそらく改修される??
Delegateの仕組み
Sender Reciever
delegate = Reciever
処理を移譲する先
を指定する
UITextFeildDelegate
(protocol)
継承
処理を実装
Sender
Reciever
delegate = self
UITextFeildDelegate
(protocol)
継承
処理を実装
StoryBoardに配置した段階でデフォルト
で delegate = self が定義される
のでViewControllerでUITextDelegateを
継承して処理を実装する
UITextFieldDegate
UITextFieldDelegateには以下のような関数が宣言されています
textFieldShouldBeginEditing: テキストフィールドを編集する直前に呼び出される
textFieldDidBeginEditing: テキストフィールドを編集する直後に呼び出される
textFieldShouldEndEditing: テキストフィールドの編集が終了する直前に呼び出される
textFieldDidEndEditing: テキストフィールドの編集が終了する直後に呼び出される
textField:shouldChangeCharactersInRange:replacementString:
テキストフィールドが編集された時に呼ばれるが、どーやら使い勝手がわるい子らしい・・・
textFieldShouldClear: クリアボタンがタップされた時に呼ばれる
textFieldShouldReturn: Returnボタンがタップされた時に呼ばれる
textFieldSholdReturn が使用頻度が高いと思われるので今回はこれを実装してみましょう!
UITextFieldDegate
上記のメソッドをUITextFieldを配置したViewControllerに記述することで、Returnキーを押した時
に入力フォームを隠す処理が実装される。
endEditing() メソッドはUIViewControllerクラスのメソッド
ついでにBMIを計算するcalculate() メソッドもここで実行させるようにすることでReturnキーを押す
と計算を開始させるようにする
各個人ですすめる課題として考える簡単なアプリ例 仕様
①地図アプリ(MapKit.frameworkを使用)
地図表示させる
現在地を表示
更新ボタンで現在地を更新
地図上にピンを立て情報を表示
(経路検索)
②APIで外部からデータを取得し、アプリ内で表示(サーバ側のプログラムが必要)
DBは使っても使わなくてもどっちでもOK
サーバ側(PHPかな?)にAPIの機能を実装
NSURLConnectでアクセスしデータを取得
TableViewControllerに一覧をlistで表示(出力形式はなんでもいいです。扱うデータによります。)
(listを押したら画面遷移し詳細なデータを表示)
ニュースアプリ的なものをイメージ

More Related Content

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

第四回iOSアプリ勉強会