ToDoアプリを作ってみよう！　＃はじめてプログラミング

#ワークショップ進行
基本的には前方のスクリーンで進めていきますが、コードをコピペする際にこの記事を活用ください

##StoryBoardでViewを作ってみよう

##アイコンを設定しよう

##実機ビルドをしてみよう

##StoryBoardとコードを紐づけてみよう

##コードを書いてみよう
###FirstViewController.swiftのコードを書いてみよう
```
//
//  FirstViewController.swift
//  To Do
//
//  Created by Aoki Riku on 2018/12/21.
//  Copyright © 2018 Aoki Riku. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController {
    @IBOutlet weak var taskText: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    @IBAction func addTask(_ sender: Any) {
        
        if taskText.text == ""{
            let title = "タスクが入力されてないよ"
            let message = "タスクを追加してね！"
            let okText = "入力する"
            
            let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
            let okayButton = UIAlertAction(title: okText, style: UIAlertAction.Style.cancel, handler: nil)
            alert.addAction(okayButton)
            
            present(alert, animated: true, completion: nil)
            
        }else{
            
            toDoList?.append(taskText.text!)
            taskText.text = ""
            taskText.placeholder = "タスクを入力しよう"
        }
        
     
        
        
    }
    
    
            
            
        }
```
###TaskSave.swiftを書いてみよう
```
//
//  TaskSave.swift
//  To Do
//
//  Created by Aoki Riku on 2018/12/21.
//  Copyright © 2018 Aoki Riku. All rights reserved.
//

import Foundation

var toDoList:[String]?

func saveData(todoList:[String]) {
    UserDefaults.standard.set(todoList, forKey: "toDoList")
}

func fetchData() -> [String]? {
    if let todo = UserDefaults.standard.array(forKey: "toDoList") as? [String] {
        return todo
    }
    else {
        return nil
    }
}

```
###SecondViewController.swiftのコードを書いてみよう
```
//
//  SecondViewController.swift
//  To Do
//
//  Created by Aoki Riku on 2018/12/21.
//  Copyright © 2018 Aoki Riku. All rights reserved.
import UIKit

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let todo = toDoList {
            return todo.count
        } else {
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        if let todo = toDoList{
            cell.textLabel?.text = todo[indexPath.row]
        }
        return cell
    }
    
  
    
    @IBOutlet weak var toDoTableVIew: UITableView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        toDoTableVIew.delegate = self
        toDoTableVIew.dataSource = self
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        toDoTableVIew.reloadData()
    }
    
}


```

##AppDelegate.swiftのコードを書いてみよう

```
//
//  AppDelegate.swift
//  ToDoList
//
//  Created by Aoki Riku on 2018/12/21.
//  Copyright © 2018 Aoki Riku. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        if let todo = fetchData() {
            toDoList = todo
        }else {
            toDoList = [String]()
        }

        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}
```

##キーボードが閉じない？？？
```
//
//  FirstViewController.swift
//  To Do
//
//  Created by Aoki Riku on 2018/12/21.
//  Copyright © 2018 Aoki Riku. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController {
    @IBOutlet weak var taskText: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    
    @IBAction func addTask(_ sender: Any) {
        
        if taskText.text == ""{
            let title = "タスクが入力されてないよ"
            let message = "タスクを追加してね！"
            let okText = "入力する"
            
            let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
            let okayButton = UIAlertAction(title: okText, style: UIAlertAction.Style.cancel, handler: nil)
            alert.addAction(okayButton)
            
            present(alert, animated: true, completion: nil)
            
        }else{
            
            toDoList?.append(taskText.text!)
            taskText.text = ""
            taskText.placeholder = "タスクを入力しよう"
        }
        
        self.view.endEditing(true)
        
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        self.view.endEditing(true)
    }
            
            
        }
        
```
##タスクが完了しない？？？
```
//
//  SecondViewController.swift
//  To Do
//
//  Created by Aoki Riku on 2018/12/21.
//  Copyright © 2018 Aoki Riku. All rights reserved.
import UIKit

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let todo = toDoList {
            return todo.count
        } else {
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        if let todo = toDoList{
            cell.textLabel?.text = todo[indexPath.row]
        }
        return cell
    }
    
  
    
    @IBOutlet weak var toDoTableVIew: UITableView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        toDoTableVIew.delegate = self
        toDoTableVIew.dataSource = self
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        toDoTableVIew.reloadData()
    }
    


    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let completeButten: UITableViewRowAction = UITableViewRowAction(style: .normal, title: "完了") { (action, index) -> Void in
            toDoList?.remove(at: indexPath.row)
            tableView.reloadData()
        }
        
        completeButten.backgroundColor = UIColor.blue
        
        return [completeButten]
        
    }
    
    
}


```