Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS アプリ作成入門

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

iOS アプリ作成入門

Avatar for nonylene

nonylene

March 13, 2016
Tweet

More Decks by nonylene

Other Decks in Technology

Transcript

  1. 4UBDL0WFSqPX"1* w υΩϡϝϯτ w IUUQTBQJTUBDLFYDIBOHFDPNEPDT w TFBSDI"1* ྫλΠτϧʹ4XJGU  w

    IUUQTBQJTUBDLFYDIBOHFDPNTFBSDI PSEFSEFTDTPSUBDUJWJUZJOUJUMFTXJGUTJUF TUBDLPWFSqPXpMUFS GQO[U%1
  2. J04

  3. 4XJGUͷจ๏ʢܰ͘ʣ class Piyo : Foo { var text: String? func

    makeHoge(str: String) -> Bar { self.text = str let hoge = Hoge(text: str) return home as! Bar } func printText() { print("text: " + text!) } } Optional
 (Nullable) Unwrap
 (NonNull) ఆ਺ ม਺ ֎෦Ҿ਺໊ Ωϟετ
  4. 7JFX$POUSPMMFSTXJGU class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad()

    } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } w ը໘੍ޚ w ಛʹԿ΋͍ͯ͠ͳ͍
  5. import UIKit class MainViewController: UIViewController, UISearchBarDelegate { @IBOutlet weak var

    searchBar: UISearchBar! @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() searchBar.delegate = self } } 6*4FBSDI#BS%FMFHBUF • UISearchBarDelegate Λ࣮૷
 (SearchBar ؔ܎ͷϝιουΛ࣮ߦͰ͖ΔΑ͏ʹ͢Δ)
  6. import UIKit class MainViewController: UIViewController, UISearchBarDelegate { @IBOutlet weak var

    searchBar: UISearchBar! @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() searchBar.delegate = self } } • searchBar ؔ܎ͷϝιουͷݺͼઌΛ self(ࣗ෼) ʹ 6*4FBSDI#BS%FMFHBUF
  7. ϘλϯͷΫϦοΫ import UIKit class MainViewController: UIViewController, UISearchBarDelegate { … override

    func viewDidLoad() { super.viewDidLoad() searchBar.delegate = self } func searchBarSearchButtonClicked(searchBar: UISearchBar) { let searchText = searchBar.text } } • searchBarSearchButtonClicked ͕ݺ͹ΕΔͷͰɺ
 searchBar ͷςΩετΛऔಘ
  8. func searchBarSearchButtonClicked(searchBar: UISearchBar) { let searchText = searchBar.text let encodedText

    = searchText!.
 stringByAddingPercentEncodingWithAllowedCharacters(
 .URLQueryAllowedCharacterSet())! let url = "https://api.stackexchange.com/2.2/search?~~~~" +
 encodedText + "&site=stackoverflow&filter=! 6JEajsykLFu3W" } 63-Λ࡞੒ w  63- Τϯίʔυ
  9. NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!,
 completionHandler: {data, response, error in … dispatch_async(dispatch_get_main_queue(), {

    let message = NSString(data: data!, …)! as String let alert = UIAlertController(…, message: message, …) alert.addAction(UIAlertAction(title: "OK", …)) self.presentViewController(alert, …) }) }).resume() • ϥϜμࣜͷ಺෦͸ϝΠϯεϨουͰ͸ͳ͍ • ΞϥʔτΛग़͍͕ͨ͠མͪͯ͠·͏ /463-4FTTJPOΛ࢖ͬͨϦΫΤετ
  10. NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!,
 completionHandler: {data, response, error in … dispatch_async(dispatch_get_main_queue(), {

    let message = NSString(data: data!, …)! as String let alert = UIAlertController(…, message: message, …) alert.addAction(UIAlertAction(title: "OK", …)) self.presentViewController(alert, …) }) }).resume() • dispatch_get_main_queue() Λ࢖ͬͯ
 ϝΠϯεϨουͰ࣮ߦ • ͜͜Ͱ͸ΞϥʔτΛग़͍ͯ͠Δ /463-4FTTJPOΛ࢖ͬͨϦΫΤετ
  11. import Foundation class Question { let isAnswered: Bool let ownerName:

    String let upVoteCount: Int let tags: [String] init(dict: [String: AnyObject]) { isAnswered = 
 dict["is_answered"] as! Bool upVoteCount = 
 dict["up_vote_count"] as! Int title = dict[“title"] as! String let owner = dict["owner"]
 as! [String: AnyObject] ownerName = owner["display_name"] as! String } }
  12. import Foundation class Question { let isAnswered: Bool let ownerName:

    String let upVoteCount: Int let tags: [String] init(dict: [String: AnyObject]) { isAnswered = 
 dict["is_answered"] as! Bool upVoteCount = 
 dict["up_vote_count"] as! Int title = dict[“title"] as! String let owner = dict["owner"]
 as! [String: AnyObject] ownerName = owner["display_name"] as! String } }
  13. import Foundation class Question { let isAnswered: Bool let ownerName:

    String let upVoteCount: Int let tags: [String] init(dict: [String: AnyObject]) { isAnswered = 
 dict["is_answered"] as! Bool upVoteCount = 
 dict["up_vote_count"] as! Int title = dict[“title"] as! String let owner = dict["owner"]
 as! [String: AnyObject] ownerName = owner["display_name"] as! String } }
  14. KTPOΛ2VFTUJPOʹม׵ let dict = try! NSJSONSerialization.JSONObjectWithData(…)
 as! NSDictionary as! [String:

    AnyObject] let questions = (dict["items"] as! [[String: AnyObject]]).map{ item -> Question in Question(dict: item) }
  15. w ͢΂ͯΛอ࣋ɾ؅ཧ ී௨ͷ7JFX View 1 2 3 4 5 6

    7 8 9 10 11 12 13 14 15 16 17 18 ࣮ࡍͷදࣔྖҬ →
  16. w อ࣋ɾ؅ཧ͸ը໘্ͷΈ 5BCMF7JFX TableView 3 4 5 6 7 8

    9 10 11 ະੜ੒ ࣮ࡍͷදࣔྖҬ →
  17. εΫϩʔϧ͞ΕΔͱʜ TableView 3 4 5 6 7 8 9 10

    11 ະੜ੒ View ͷ࠶ར༻ දࣔྖҬ →
  18. εΫϩʔϧ͞ΕΔͱʜ View 5 6 7 8 9 10 11 3

    ->12 4 ->13 ະੜ੒ ← ৽͍͠஋Λઃఆ දࣔྖҬ →
  19. 

  20. $FMM಺ͷ7JFXΛඥ෇͚ import UIKit class QuestionTableViewCell: UITableViewCell { @IBOutlet weak var

    countLabel: UILabel! @IBOutlet weak var checkImageView: UIImageView! @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var tagLabel: UILabel! @IBOutlet weak var bodyLabel: UILabel! @IBOutlet weak var dateLabel: UILabel! @IBOutlet weak var authorLabel: UILabel! }
  21. 2VFTUJPOઃఆͰ6*ߋ৽ class QuestionTableViewCell: UITableViewCell { @IBOutlet weak var countLabel: UILabel!

    … var question: Question? { didSet { bindQuestion(question!) } } private func bindQuestion(question: Question) { countLabel.text = String(question.upVoteCount) checkImageView.hidden = !question.isAnswered … } } Setter
  22. class MainViewController: UIViewController, UISearchBarDelegate, 
 UITableViewDelegate, UITableViewDataSource { @IBOutlet weak

    var searchBar: UISearchBar! @IBOutlet weak var questionTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() searchBar.delegate = self questionTableView.dataSource = self questionTableView.delegate = self } } w Delegate Λ࣮૷ 5BCMF7JFX
  23. class MainViewController: UIViewController, UISearchBarDelegate, 
 UITableViewDelegate, UITableViewDataSource { @IBOutlet weak

    var searchBar: UISearchBar! @IBOutlet weak var questionTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() searchBar.delegate = self questionTableView.dataSource = self questionTableView.delegate = self } } w UITableViewDelegateΫϦοΫ͞Εͨ࣌ͱ͔ w UITableViewDataSourceσʔλ͔Β$FMMΛ࡞Δ࣌ 5BCMF7JFX
  24. • εΫϩʔϧͯ͠ Cell Λ࡞੒͢Δͱ͖ʹݺ͹ΕΔ • ৔ॴ͕Ҿ਺ͱͯ͠དྷΔ $FMMΛ࡞Δ func tableView(tableView: UITableView,

    cellForRowAtIndexPath 
 indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("questionCell",
 forIndexPath: indexPath) as! QuestionTableViewCell cell.question = self.questions[indexPath.row] return cell }
  25. • ઌ΄Ͳࢦఆͨ͠ Identifier ΛೖΕͯ
 ࠶ར༻Մೳͳ Cell ΛಘΔ • ͳ͚Ε͹ࣗಈͰ࡞ͬͯ͘ΕΔ $FMMΛ࡞Δ

    func tableView(tableView: UITableView, cellForRowAtIndexPath 
 indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("questionCell",
 forIndexPath: indexPath) as! QuestionTableViewCell cell.question = self.questions[indexPath.row] return cell }
  26. func tableView(tableView: UITableView, cellForRowAtIndexPath 
 indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("questionCell",
 forIndexPath: indexPath) as! QuestionTableViewCell cell.question = self.questions[indexPath.row] return cell } • ಘΒΕͨ Cell ͸ QuestionTableViewCell $FMMΛ࡞Δ
  27. func tableView(tableView: UITableView, cellForRowAtIndexPath 
 indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("questionCell",
 forIndexPath: indexPath) as! QuestionTableViewCell cell.question = self.questions[indexPath.row] return cell } • อ͍࣋ͯ͠Δ Question Λ cell ʹηοτ $FMMΛ࡞Δ
  28. w ηΫγϣϯͷ਺ • Cell ͷ਺ 5BCMF7JFXͷઃఆ func numberOfSectionsInTableView(tableView: 
 UITableView)

    -> Int { return 1 } func tableView(tableView: UITableView, 
 numberOfRowsInSection section: Int) -> Int { return self.questions.count }
  29. 5BCMF7JFXͷߋ৽ let dict = try! NSJSONSerialization.JSONObjectWithData(…)
 as! NSDictionary as! [String:

    AnyObject] self.questions = (dict["items"] as! [[String: AnyObject]]).map{ item -> Question in Question(dict: item) } self.questionTableView.reloadData() w tableViewʹσʔλͷߋ৽Λ఻͑Δ w ͜ΕͰCell͕࠶ಡΈࠐΈ͞ΕΔ
  30. ΫϦοΫͯ͠4BGBSJΛ։͘ w tableViewͷCellΛΫϦοΫͨ࣌͠ʹݺ͹ΕΔ w CellΛ࡞Δؔ਺ͱ͸֎෦Ҿ਺໊͕ҧ͏ func tableView(tableView: UITableView, didSelectRowAtIndexPath 


    indexPath: NSIndexPath) { let question = questions[indexPath.row] let url = NSURL(string: question.link)! UIApplication.sharedApplication().openURL(url) }
  31. w indexPath͔ΒQuestionΛऔಘ w question.linkΛSafariͰ։͘ ΫϦοΫͯ͠4BGBSJΛ։͘ func tableView(tableView: UITableView, didSelectRowAtIndexPath 


    indexPath: NSIndexPath) { let question = questions[indexPath.row] let url = NSURL(string: question.link)! UIApplication.sharedApplication().openURL(url) }