Notes App Fast AF

Swift Fast AF
3 min readDec 20, 2022

Welcome to this tutorial on creating a notes app for iOS using swift and Xcode! In this tutorial, we will go through the steps to build a simple notes app that allows users to create, edit, and delete notes.

Before we begin, make sure you have Xcode and swift installed on your computer. If you don’t have them already, you can download Xcode for free from the App Store and swift is included with Xcode.

  1. Create a new project in Xcode by going to File > New > Project. Select “Single View App” from the template options and click Next.
  2. Name your project “Notes App” and select “Swift” as the programming language. Click Next and choose a location to save your project.
  3. Open the main storyboard file by clicking on the “Main.storyboard” file in the project navigator.
  4. Drag a Table View from the Object Library onto the canvas. This will be used to display the list of notes.
  5. Add a Navigation Controller to the canvas by clicking on Editor > Embed > Navigation Controller. This will allow us to navigate between different screens in the app.
  6. Control-drag from the table view cell to the new Navigation Controller and select “Push > scene” from the pop-up menu. This will create a segue between the table view cell and the Navigation Controller, allowing us to navigate to a new screen when a cell is tapped.
  7. Add a View Controller to the canvas by dragging a View Controller from the Object Library onto the canvas. This will be used to display the contents of a note.
  8. Control-drag from the table view cell to the new View Controller and select “Push > scene” from the pop-up menu. This will create a segue between the table view cell and the View Controller, allowing us to navigate to the note detail screen when a cell is tapped.
  9. Drag a Text View from the Object Library onto the View Controller canvas. This will be used to display the contents of the note.
  10. Add a bar button item to the top right corner of the Navigation Bar on the View Controller by dragging a Bar Button Item from the Object Library onto the Navigation Bar.
  11. Control-drag from the bar button item to the View Controller and select “Action > saveNote” from the pop-up menu. This will create an action method that will be called when the save button is tapped.
  12. Open the Assistant Editor by clicking on View > Assistant Editor > Automatic. This will allow us to create outlets and actions for the UI elements we just added.
  13. Control-drag from the Text View to the View Controller and create an outlet named “noteTextView”.
  14. Control-drag from the bar button item to the View Controller and create an action method named “saveNote”.
  15. Close the Assistant Editor and open the ViewController.swift file.
  16. Add the following code to the saveNote action method to save the contents of the note to UserDefaults:
let defaults = UserDefaults.standard
defaults.set(noteTextView.text, forKey: "note")
defaults.synchronize()

Open the TableViewController.swift file and add the following code to the viewDidLoad method to retrieve the saved note from UserDefaults:

let defaults = UserDefaults.standard
if let note = defaults.string(forKey: "note") {
notes.append(note)
}

Add the following code to the numberOfRowsInSection method to set the number of rows in the table view to the number of notes in the notes array:

return notes.count

Add the following code to the cellForRowAt method to set the text of the table view cell to the corresponding note in the notes array:

let selectedNote = notes[indexPath.row]
let vc = storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
vc.currentNote = selectedNote
navigationController?.pushViewController(vc, animated: true)

Open the DetailViewController.swift file and add a property for the current note at the top of the file:

if let note = currentNote {
noteTextView.text = note
}

That’s it! You now have a basic notes app that allows users to create, edit, and delete notes. To improve the app, you can add features such as a search function or the ability to categorize notes.

I hope this tutorial was helpful in guiding you through the process of creating a notes app for iOS using swift and Xcode. If you have any questions or need further clarification, please don’t hesitate to ask in the comments below.

Citations:

- Apple Developer Documentation. (n.d.). UserDefaults. Retrieved from https://developer.apple.com/documentation/foundation/userdefaults
- Apple Developer Documentation. (n.d.). Table View. Retrieved from https://developer.apple.com/documentation/uikit/table_views

--

--