IntroductionWelcome| 00:00 |
(music playing.)
| | 00:04 |
Welcome to Building a Note-Taking App for
iOS. I am Todd Perkins.
| | 00:07 |
I'll be walking you through building a
simple app in iOS from scratch.
| | 00:13 |
Throughout the course, we'll look at how
to lay out your apps visually, how to
| | 00:16 |
connect UI elements to your code, and how
to save data to persistent storage.
| | 00:22 |
So let's build an app.
| | 00:23 |
| | Collapse this transcript |
| What you should know before watching this course| 00:00 |
So, what should you know coming into this course?
| | 00:03 |
This course is for software developers who
want to build IOS applications and have
| | 00:08 |
experience in other mobile development
environments, such as Android and Windows
| | 00:12 |
Phone .
Here's some helpful skills that you should
| | 00:16 |
have as well.
Should be familiar with object-oriented
| | 00:19 |
programming and know at least one
programming language, especially a C-style
| | 00:24 |
programming language, like C, C#, or
JavaScript, while you don't need to know
| | 00:28 |
Objective-C for this course.
If you'd like more information on
| | 00:33 |
Objective-C, you can see ObjectiveC
Essential Training, which will give you an
| | 00:38 |
introduction to basic syntax in
Objective-C and I'll show you how to
| | 00:41 |
create projects and classes in Xcode.
There's also Foundations of Programming:
| | 00:48 |
Fundamentals and Foundations of
Programming: Object-Oriented Design.
| | 00:53 |
For more information on iOS, you can see
iOS SDK Essential Training.
| | 00:57 |
Currently, the newest one is from 2012.
That will show you the basic application
| | 01:02 |
architecture for creating an iOS
application, and it will introduce you to
| | 01:07 |
using the iOS Simulator in Xcode.
Again, as long as you're coming from a
| | 01:12 |
programming background and you're familiar
with software development on another
| | 01:16 |
platform, or know at least one programming
language, you should be okay to go for
| | 01:19 |
this course.
But if you find yourself wanting more
| | 01:23 |
information about a specific topic, then
you can see any of those courses I
| | 01:26 |
mentioned.
| | 01:28 |
| | Collapse this transcript |
| Related courses| 00:00 |
This course is one of three related
courses that teach you how to build the
| | 00:04 |
same app.
That way you can learn the different
| | 00:08 |
programming language and integrated
development environments that are used to
| | 00:12 |
create apps for various mobile platforms,
so here are the courses that are related
| | 00:16 |
to this one.
This course is how to build a simple app
| | 00:20 |
for iOS.
There are related courses on how to build
| | 00:24 |
the same app for Android and for Windows Phone.
| | 00:28 |
So here are some comparisons.
So the operating systems are Android, iOS,
| | 00:32 |
and Windows Phone 8.
Programming languages used is Java for
| | 00:37 |
Android, Objective-C for iOS, and XAML and
C# for Windows Phone 8.
| | 00:43 |
The development environment used for
Android is Eclipse with Android Developer
| | 00:48 |
Tools, Xcode is used on iOS, and Visual
Studio is used for Windows Phone 8.
| | 00:54 |
These courses are organized in the library
so that you can see how creating the same
| | 00:58 |
app differs from platform to platform.
So if you'd like to learn a new platform
| | 01:03 |
after this one, that's where to go.
| | 01:05 |
| | Collapse this transcript |
| Using the exercise files| 00:00 |
If you are a Premium subscriber to
Lynda.com, then you have access to the
| | 00:04 |
Exercise Files for this course.
Exercise Files are organized by Chapters
| | 00:09 |
and assets.
The assets folder has graphical assets and
| | 00:13 |
the Chapter folders are organized
differently depending on the chapter.
| | 00:17 |
Chapters 1 and two 2 no exercise files.
So for those chapters, you'll see, there
| | 00:22 |
are no exercise file for this chapter
inside of those folders.
| | 00:27 |
That's the text file that I saved in there.
| | 00:29 |
For the rest of the chapters, I put
folders that correspond to the different
| | 00:33 |
movies.
In those folders, you'll find, in some
| | 00:36 |
cases, just the app.
So in here, there's the Plain Ol' Notes,
| | 00:40 |
which is the app that we're developing,
and in other folders, you'll see see a
| | 00:43 |
final and a start folder.
The start folder contains the files that
| | 00:47 |
I'm using at the start of the movie and
the final contains the files that I"m
| | 00:51 |
using at the end of the movie that we
finish with, so if you get lost you can
| | 00:54 |
catch up.
If you don't have access to the Exercise
| | 00:59 |
Files, don't worry, you can still follow along.
| | 01:01 |
Throughout the course, I'll show you all
you need to set up your files in the same
| | 01:06 |
way.
| | 01:07 |
| | Collapse this transcript |
|
|
1. Project OverviewFeatures of the note-taking app| 00:00 |
Let's look at the features of the app that
we're going to make.
| | 00:04 |
Plain Ol' Notes is a simple note-taking app.
| | 00:08 |
Here are the features that it has.
You can create and edit simple text-based
| | 00:12 |
notes.
You can navigate and share data between
| | 00:16 |
multiple screens.
It will save data to the device's
| | 00:19 |
persistent storage.
The app will also have easily discoverable
| | 00:24 |
command patterns to accept user input.
So a simple plus button to add a new note,
| | 00:29 |
for example.
The app has two screens: a list screen,
| | 00:33 |
which is a list of the notes, and an
editor screen for editing the notes.
| | 00:41 |
The app will support older versions of
iOS, starting with 5.0, and the user
| | 00:46 |
interface will adapt to both 3.5 and
4-inch displays.
| | 00:52 |
The list screen is going to allow a user
to create a new note by tapping the plus
| | 00:56 |
icon.
And the list items are truncated with
| | 01:00 |
ellipses where necessary to fit on a
single line.
| | 01:03 |
Now you'll see that iOS actually does this
for you automatically.
| | 01:07 |
And if a list item has line feeds, only
the first line is displayed.
| | 01:12 |
This is done automatically as well.
For the Editor Screen, data entry will
| | 01:17 |
word wrap and accept line feeds.
So this is a pretty simple application
| | 01:22 |
that will get you introduced to building
applications within the iOS SDK.
| | 01:28 |
| | Collapse this transcript |
| Demoing the finished app| 00:00 |
Here is our note taking app, Plain Ol' Notes.
| | 00:04 |
Top left on the main list screen, you'll
see an Edit button and a Plus button.
| | 00:09 |
If you click the Plus button, you can
create a new note.
| | 00:12 |
And I'll just type in here, example note.
And then click back to go to play all
| | 00:16 |
notes.
And you'll see the note in there.
| | 00:19 |
And of course, I can click on that, and
then add some text to it, and return to
| | 00:22 |
the main view.
And you can see that it's updated
| | 00:25 |
appropriately.
Click the Plus button to add yet another
| | 00:30 |
note.
And there it is.
| | 00:33 |
And the newer notes at the top and older
notes are stored at the bottom.
| | 00:37 |
That's really all that there is to it.
And these notes are saved at the device's
| | 00:40 |
persistent storage.
So if I restart my phone, for example, the
| | 00:43 |
app would, then, reload all of this data.
So it saves the data to the phone.
| | 00:49 |
You can also find this application for
download to your device in the iTunes App
| | 00:54 |
Store.
However, this is actually a different
| | 00:59 |
version of the application that has
features like sharing your notes and
| | 01:03 |
changing the font.
I had to include these features to get the
| | 01:08 |
app approved by Apple.
They did not approve based on their
| | 01:12 |
guidelines, the app on how simple it was.
So they made me add these additional
| | 01:16 |
features before they would release it.
So that's why they're in there.
| | 01:20 |
And the reason why I'm not teaching them
in this course, is so that the course
| | 01:24 |
matches both the Android and the Windows
phone versions of this app.
| | 01:29 |
If you happen to come across this app in
the App Store and you're wondering why
| | 01:32 |
it's different.
That's why.
| | 01:35 |
So I'll close this window.
And that is the app that we're going to be
| | 01:38 |
building in this course.
And we'll look at building the entire
| | 01:42 |
thing from scratch.
| | 01:43 |
| | Collapse this transcript |
| Setting up the developer tools| 00:00 |
The development environment for iOS is
Xcode, which you can get in the Mac App
| | 00:05 |
Store.
This is really all that you need to
| | 00:09 |
develop an iOS application, but you can't
release an iOS application to the App
| | 00:14 |
Store or test on your device U\unless
you're also an Apple developer.
| | 00:20 |
You can sign up to be an Apple developer
at developer.apple.com.
| | 00:23 |
Go there and click iOS Dev Center.
And here you can sign up for the iOS
| | 00:29 |
Developer Program on the right side of the screen.
| | 00:34 |
Of course, this might change after this
course is released, so just look for the
| | 00:38 |
part of the page that says iOS Developer Program.
| | 00:41 |
So when you go here, you can sign up, and
you'll have to pay approximately US$100
| | 00:46 |
per year, which is the current price at
the time of this recording.
| | 00:51 |
To be an iOS developer, which means having
access to testing apps on your devices,
| | 00:56 |
and publishing apps to the App Store.
So, all you need is Xcode if you just want
| | 01:01 |
to learn how to write the code and test an
app in an iOS simulator.
| | 01:06 |
For more information about becoming an iOS
developer and testing applications on your
| | 01:10 |
device, I recommend looking at iOS SDK
Essential Training.
| | 01:15 |
Once you have at least Xcode set up, then
you're ready to start writing our app.
| | 01:20 |
| | Collapse this transcript |
|
|
2. Understanding iOS SpecificsIssues in cross-platform development| 00:00 |
You might be coming into this course with
the intention to port an app to a
| | 00:04 |
different mobile platform.
So, for that reason, I wanted to discuss
| | 00:08 |
cross-platform development and the
benefits and drawbacks to using
| | 00:12 |
cross-platform tools versus native tools.
And explain why I chose to use Objective-C
| | 00:18 |
and Xcode for this course.
So let's say your goal is to support more
| | 00:23 |
than one mobile operating system for a
particular app.
| | 00:27 |
So you want to deploy the same app on
multiple mobile operating systems and get
| | 00:32 |
the best possible performance, broadest
access to device features and direct
| | 00:36 |
support from operating system vendors,
like Apple or Google or Microsoft.
| | 00:43 |
Therefore, the best option is to use
native tools and languages for each
| | 00:47 |
operating system.
Let's talk about some alternatives that
| | 00:51 |
you might want to use.
First, you can wrap web pages in as native
| | 00:55 |
apps.
You can use PhoneGap or Sencha for this,
| | 01:00 |
to use web standard languages like HTML,
CSS, and JavaScript, to build a UI.
| | 01:06 |
Then, you can use PhoneGap and Sencha to
work with device features.
| | 01:09 |
And you can deploy the same app cross
platform without much work.
| | 01:15 |
You can find PhoneGap at PhoneGap.com and
Sencha at Sencha.com.
| | 01:20 |
If you want to do game development cross
platform, here are some popular options.
| | 01:25 |
There's Flash with AIR, Unity, GameSalad
and others like the Corono SDK.
| | 01:29 |
So with those you can build a rich
graphical interface, you can use an
| | 01:33 |
application that you already may be
familiar with or a language that you
| | 01:36 |
already may be familiar with like action
script for flash, javascript for unity or
| | 01:41 |
GameSalad that uses no code at all.
So you can find Flash at Adobe's website
| | 01:48 |
and find Unity at Unity3D.com and Game
Salad.com for GameSalad.
| | 01:53 |
If you want to use different languages
than the native languages for
| | 01:57 |
applications, you can do that as well.
You can use Titanium, Mono for Android,
| | 02:05 |
and Xamarin.iOS.
That way you can use your favorite
| | 02:08 |
language instead of the native language
for a particular operating system.
| | 02:12 |
So Titanium uses JavaScript.
Mono and Xamarin.iOS use C#.
| | 02:18 |
Now here's the challenge that you have
with native tools.
| | 02:20 |
You're going to have to learn multiple
programming languages, Java for Android,
| | 02:24 |
Objective-C for iOS, or C# or Visual Basic
for Windows Phone.
| | 02:30 |
Each language has it's own nuances,
including different vocabularies and
| | 02:34 |
design patterns.
There re also different developer tools
| | 02:37 |
that you'll have to learn.
And while you can re-use some application
| | 02:41 |
assets like artwork, code must be
rewritten for each platform.
| | 02:46 |
But here are the benefits to using native code.
| | 02:50 |
You get the best available performance,
the best access to device features, direct
| | 02:54 |
support from your operating system vendor,
and the cleanest upgrade path when new
| | 02:58 |
operating systems come out and new devices
are released.
| | 03:03 |
And for that reason, I've chosen for this
course to teach Xcode and Objective-C for
| | 03:07 |
developing an iPhone app.
| | 03:10 |
| | Collapse this transcript |
| Unique concepts for iOS| 00:00 |
Here are some items that are unique to iOS.
| | 00:03 |
First, the way that iOS screens are organized.
| | 00:09 |
Each screen that you view is known as a scene.
| | 00:12 |
And scenes are organized into storyboards.
Each screen has various UI elements.
| | 00:19 |
And Xcode gives you a visual interface for
laying out these elements to create the
| | 00:23 |
screens for your app.
There are a few differences between phone
| | 00:28 |
and tablet devices that you should be
aware of.
| | 00:31 |
First is, of course, the aspect ratio.
That also includes the number of pixels on
| | 00:36 |
the screen.
And, in some cases, you'll find alternate
| | 00:40 |
UI elements.
And so, of course, some UI elements are
| | 00:43 |
shared.
But the great thing about Xcode and using
| | 00:47 |
Apple's built-in tools is that most of the
code is the same between a tablet version
| | 00:52 |
of a UI element and a phone version of the
UI element.
| | 00:57 |
So you have to do very little to account
for that yourself.
| | 01:02 |
As far as graphics go in an application,
all of your assets are stored as
| | 01:06 |
resources.
Resources for an app are not actually file
| | 01:10 |
system folders.
So when you have resources for your
| | 01:13 |
application, Xcode displays a list of
files and folders but those are not
| | 01:17 |
mirrored to your file system, so you need
to manually import these files and folders
| | 01:22 |
into your project.
So when you provide graphics, you'll need
| | 01:28 |
to provide all graphics in different resolutions.
| | 01:32 |
So, as a different resolutions of devices
are concerned, all you have to do is
| | 01:36 |
provide a different file name with a
specific suffix such as at App2x.
| | 01:42 |
And the app will decide which version of
the graphic to serve to which device.
| | 01:47 |
So you can have the high resolution
devices supported and the low resolution
| | 01:51 |
devices supported fairly easily.
And those are just some of the features of
| | 01:56 |
iOS that are unique in terms of
development.
| | 01:59 |
| | Collapse this transcript |
| Android and Windows Phone developers beware| 00:00 |
If you're coming from an Android or
Windows Phone background, here are some
| | 00:03 |
things that you might want to know.
Some major differences include only one
| | 00:08 |
app store, little device fragmentation,
and stricter guidelines for releasing
| | 00:13 |
apps.
First, only one App Store, there's only
| | 00:18 |
one place to legitimately sell apps.
I put legitimately in there, because you
| | 00:25 |
could sell apps through Cydia on a
jailbroken phone.
| | 00:28 |
But there's only one legitimate standard
place to sell applications.
| | 00:33 |
The advantage of that though is that it's
possible for you to get featured by Apple
| | 00:36 |
in this massive store and there's this
huge market.
| | 00:40 |
However, it is easy to get lost in the
mix, because there are so many
| | 00:43 |
applications published every single day.
Device fragmentation is something you
| | 00:49 |
don't really have to worry about if you're
coming from another market.
| | 00:53 |
There are three basic resolutions.
So you have the original iPhone, which is
| | 00:57 |
3.5 inches.
The four inch iPhone and then an iPad, and
| | 01:02 |
so, you really worry about those but the
retina display versions of those screens
| | 01:07 |
are twice as dense.
So, it's pretty easy to adapt to changing
| | 01:12 |
screen sizes, since there are only three
different aspect ratios.
| | 01:17 |
Apple provides SDK tools to automatically
adapt to each screen size.
| | 01:23 |
So most of the time you don't have to
worry about that on your own.
| | 01:26 |
You just have to worry about custom UI
elements that you create.
| | 01:30 |
And finally, Stricter Guidelines.
And that means that Apple governs
| | 01:34 |
everything that is in the App Store.
So, they choose what's there, and what's
| | 01:39 |
not allowed to be there.
These rules and guidelines are constantly
| | 01:44 |
changing.
And even though Apple notifies its
| | 01:47 |
developers when a change takes place, it's
still a lot to keep up with.
| | 01:52 |
Further, because of these changing
guidelines, your app could be rejected or
| | 01:56 |
it could be removed from the app store at
any time.
| | 02:01 |
And, as an example of this, I tried to
release the application that we're
| | 02:04 |
creating in this course in the App store.
And Apple said there were not enough
| | 02:09 |
features in it.
Whereas, the Android version of the app
| | 02:12 |
got into Google Play and the Amazon App
Store without any disputation from Google
| | 02:17 |
or Amazon.
So, you are going to have to jump through
| | 02:21 |
a few more hoops, but of course there are
the benefits that I mentioned, as well.
| | 02:25 |
| | Collapse this transcript |
|
|
3. Getting StartedCreating the Xcode project| 00:00 |
At this point you should have Xcode
installed, but if you don't have it
| | 00:03 |
installed already you can get it at the
Mac app store as shown in a previous
| | 00:05 |
movie.
When you launch Xcode for the first time,
| | 00:09 |
you're going to see this screen.
It's a welcome screen that gives you quick
| | 00:13 |
access to doing things like creating a new
project or opening a recent project.
| | 00:18 |
You can of course hide this at launch if
you uncheck this box.
| | 00:22 |
I'm going to click Create A New Project.
And here I can choose a type of project
| | 00:27 |
that I want to create.
You can create an iOS or an OS X
| | 00:30 |
application.
If you want to create an iOS app, click
| | 00:34 |
Application under iOS and then choose one
of the options in this window.
| | 00:39 |
The option we're going to choose for our
app is a master detail application.
| | 00:44 |
And that's where you have a list of items.
And you tap an item.
| | 00:48 |
And you can see details about that item.
We're going to modify that to make our
| | 00:52 |
notes app.
Note that there are seven applications
| | 00:55 |
here right now.
And that these may change with later
| | 00:58 |
versions of x code.
I'm going to select Master Detail and
| | 01:03 |
click Next.
For product name, I'll type plain old
| | 01:06 |
notes.
And I have organization name at Todd
| | 01:08 |
Perkins, of course you can use your
organization name here if you wish.
| | 01:13 |
And for company identifier, I use my
reverse domain structure.
| | 01:18 |
So com.toddperkins there.
I've chosen iPhone for devices.
| | 01:23 |
And note that you can choose universal
which is iPad and iPhone.
| | 01:28 |
And if you choose that option Apple will
provide you with two sets of storyboards
| | 01:31 |
so you can design the interfaces
separately if you'd like.
| | 01:35 |
Now I've checked Use Storyboards, and
Automatic Reference Counting.
| | 01:39 |
Note that if you use storyboards you need
to use at least version 5.0 of the
| | 01:43 |
operating system.
And then I'm going to click Next.
| | 01:49 |
And I'm going to go to my Desktop >
Exercise Files > Chapter 3.
| | 01:53 |
And I'll create a new folder called create_project.
| | 01:58 |
And click Create.
And I'll click Create again.
| | 02:03 |
And then all of the file structure is
actually created for me.
| | 02:06 |
Note on the left side of the screen, that
if you click that Folder icon, then you
| | 02:10 |
can see all the files that are included in
your project.
| | 02:15 |
As I mentioned in a previous movie, this
is not a mirror of your file system.
| | 02:20 |
So if I go to that file on my desktop.
I'm going to the plain old notes folder
| | 02:25 |
that's been created by XCode for this project.
| | 02:28 |
You'll notice that the files and folders
don't quite match what you see in XCode.
| | 02:33 |
That's because these folders inside of
XCode are actually called groups.
| | 02:37 |
And they enable you to group files and folders.
| | 02:41 |
Independently of how they're organized in
the file system.
| | 02:44 |
So you can actually organize them however
you'd like.
| | 02:47 |
So that means if I add a file to this play
all notes folder in my file system, it
| | 02:51 |
will not automatically be imported into Xcode.
| | 02:55 |
I'll have to manually click and drag it
into Xcode or import it in some other way.
| | 03:00 |
So back to Xcode.
So in the middle of the screen in Xcode we
| | 03:04 |
have some settings for our apps.
If you don't see those settings click on
| | 03:08 |
Plain Ol' Notes on the left side of the
screen, and then click on Plain Ol' Notes
| | 03:12 |
under targets in the center of the screen.
And under the summary tab, you'll see the
| | 03:17 |
summary of info for your applications.
So here you can manage a few of the
| | 03:22 |
settings for your application.
On the right side of the screen, there are
| | 03:26 |
some other settings that, for right now,
we are not going to be using.
| | 03:30 |
So, I'm going to click this box at the top
right of the screen that's called hide or
| | 03:34 |
show utilities.
That way, we have some more working space.
| | 03:39 |
If you don't see that button, you can
right click the top area of the screen,
| | 03:42 |
just below your operating system status bar.
| | 03:46 |
And choose icon and text, or icon only you
see the toolbar.
| | 03:50 |
Let's test out this template app in the simulator.
| | 03:54 |
Now to choose the simulator you want to
test in, look at the top left of your
| | 03:57 |
screen, make sure you have plain old notes
chosen, and then to the right of that
| | 04:01 |
choose iPhone 6.1 simulator.
Or later simulator, if one exists, at the
| | 04:07 |
time that you are watching this recording.
Okay, now click the Run button, to launch
| | 04:12 |
the iOS simulator and you'll just have to
wait a few seconds and see the simulator
| | 04:15 |
pop up and the app should load and we'll
see what we have to work with.
| | 04:21 |
So, here's the app and the main view says Master.
| | 04:25 |
There's an edit button and a plus button.
I can click the Plus button.
| | 04:29 |
And that adds a new entry into this what's
called a Table view.
| | 04:33 |
And you can click on that to see the
details of it.
| | 04:36 |
Now we're going to modify this of course
so it turns into a note taking app.
| | 04:40 |
As you can tell, it's not going to be that
much of a complicated process because
| | 04:43 |
Apple handles a lot of it for us with this template.
| | 04:47 |
So as we go on we're going to create this
note taking app step by step from this
| | 04:50 |
template.
| | 04:52 |
| | Collapse this transcript |
| Applying an app icon| 00:00 |
Let's look at adding an icon for an iOS app.
| | 00:03 |
I'm looking at my Plain Ol' Notes target
in the Summary tab, and if I scroll down a
| | 00:07 |
little bit, you'll see the App Icons section.
| | 00:11 |
To set an app icon, right-click in the
space that says No image specified and
| | 00:15 |
chose Select File.
In the Exercise Files folder, there's a
| | 00:21 |
folder called assets and this file is 57
pixels by 57 pixels.
| | 00:26 |
So it's a square image, and it's a .png file.
| | 00:30 |
So that is the native resolution for the
non-Retina display version of the iPhone.
| | 00:37 |
So click Choose and you should see the
image pop up there.
| | 00:40 |
I'm going to right click for the Retina
Display version.
| | 00:43 |
And that's twice as wide by twice as high,
so it's 114 by 114.
| | 00:48 |
Again, .png.
Click Choose, and then I'm going to check
| | 00:52 |
Prerendered.
Now, this explains to Apple that the icon
| | 00:56 |
already has a gloss effect, so Apple won't
add its own gloss effect on my icon.
| | 01:03 |
So for most of the apps that I've done,
I've actually checked this.
| | 01:06 |
So you probably will want that for your app.
| | 01:09 |
So if you wanted to set the image that
shows while your application is loading,
| | 01:12 |
you can do the same thing.
You just right-click and choose Select
| | 01:15 |
File.
And when you hover over the image, you'll
| | 01:18 |
get a pop-up that explains how many pixels
the image should be.
| | 01:23 |
So I'm just going to leave that black for
now, because, the application is so
| | 01:26 |
lightweight that there's not really any
waiting time while it's loading.
| | 01:31 |
So I've chosen not to set a Launch Image,
but of course, you can if you'd like.
| | 01:35 |
So now, with our icon set up, we're ready
to start building our application.
| | 01:40 |
| | Collapse this transcript |
|
|
4. Creating the Data ModelUsing local data storage| 00:00 |
Let's look at our local data storage options.
| | 00:03 |
Here's what iOS offers you to store data.
User defaults, creating your own custom
| | 00:09 |
files, and CoreData and SQLite databases.
First, user defaults.
| | 00:15 |
User defaults are key value pairs that are
managed through Objective-C.
| | 00:19 |
They're stored in XML files, easily
managed through the API in an unencrypted
| | 00:24 |
format, file storage, files can be created
in red on persistent media.
| | 00:30 |
But there aren't any special file types so
you can store any type of file that you
| | 00:35 |
like.
It can be custom, it can have a custom
| | 00:39 |
extension, you can associate your app with
your own custom extension if you want, and
| | 00:42 |
you can even associate your app with your
own custom extension.
| | 00:47 |
Files can be created that run time locally
to the app or in shared file locations.
| | 00:54 |
Finally, core data and SQLite, so you can
save structured data in a SQLite database.
| | 01:00 |
iOS provides built-in support for SQLite,
which you can read about at SQLite.org.
| | 01:06 |
Classes in interfaces are in the CoreData package.
| | 01:09 |
There's an entire course on lynda.com
about CoreData and SQLite, if you're
| | 01:13 |
interested.
In CoreData, database files are stored
| | 01:17 |
local to your app.
So here's what we're going to do.
| | 01:20 |
The note-taking app stores notes.
Each note is a key-value pair.
| | 01:26 |
The key is a unique date and time stamp,
and the value is a string.
| | 01:31 |
So we're going to use iOS's user defaults.
I'll also show you how to do local file
| | 01:36 |
storage just in case you want to use that
method instead.
| | 01:40 |
For more details about storage options,
you can see this link.
| | 01:44 |
| | Collapse this transcript |
| Creating a data class| 00:00 |
Now, let's create our data model.
We'll do that in a class called data.
| | 00:04 |
I'm going to select Plain Ol' Notes on the
left side of the screen, that's the Plain
| | 00:08 |
Ol' Notes group, and then I'm going to go
to file new file.
| | 00:13 |
Note, that you can push Cmd+N.
I'm going to choose under iOS in Cocoa
| | 00:18 |
Touch, Objective-C class and click Next.
I'm going to name this class Data.
| | 00:24 |
And it's going to be a subclass of
NSObject, which is the root class for
| | 00:27 |
Objective-C.
So I'll click Next and then click Create.
| | 00:33 |
And in Data.h, I'm going to create some
constant values.
| | 00:40 |
I'll do this right under the import statement.
| | 00:42 |
So that's going to be #define, and the
first one will be a key so I call it
| | 00:46 |
kDefaultText.
And I'll set it equal to the NS string
| | 00:50 |
which has an @sybmol before it.
That is an object string, so it's not a
| | 00:58 |
primitive value, and we'll call this New Note.
| | 01:01 |
And the NS string is a string that's
inside of an object, so we'll call this
| | 01:06 |
New Note.
And on the next line, kAllNotes.
| | 01:11 |
And this'll just be notes.
And finally K detail view, and we'll call
| | 01:20 |
this showDetail.
Now, these keys represent of course the
| | 01:25 |
DefaultText, so when you create a note,
then it will have the default text of New
| | 01:29 |
Note.
And All Notes is going to be the key that
| | 01:33 |
we use to access the AllNotes dictionary,
and the DetailView key is going to be what
| | 01:38 |
we use to access the detail view, which is
the second screen of our app.
| | 01:45 |
So I'll save this file with Cmd+S.
And now, we have data.h which was our
| | 01:50 |
header file, and data.m, which is the
implementation file.
| | 01:55 |
And we're ready to start writing the code
to access our user defaults data.
| | 02:00 |
| | Collapse this transcript |
| Creating a dictionary to manage data| 00:00 |
The easiest way to store shared
preferences is through key value pairs in
| | 00:04 |
a class called NSUserDefaults.
What we're going to do for our application
| | 00:10 |
is create a dictionary of key value pairs,
the key being the date that the note was
| | 00:14 |
created and time.
And the value being the text of the note,
| | 00:18 |
as a string.
We'll save these key value pairs into a
| | 00:22 |
dictionary and save that dictionary to the
use defaults.
| | 00:27 |
So, let's look at creating the dictionary.
Start in data.h and in between the
| | 00:32 |
interface and end call outs, create a
static class method so, use a plus sign
| | 00:38 |
and in parentheses, return an NSMutableDictionary.
| | 00:46 |
And we're going to call this getAllNotes.
Let's copy and paste this line of code
| | 00:51 |
into data.m.
Make sure to save data.h when you move
| | 00:56 |
over.
And we're going to paste this right in
| | 00:58 |
here in between the implementation and end.
| | 01:02 |
Make sure to delete the semicolon and add
the curly braces.
| | 01:06 |
Now, we're going to need to return an NSMutableDictionary.
| | 01:08 |
The dictionary is going to be a static
variable, that we'll declare under the
| | 01:13 |
implementation call out.
That of course is an NSMutableDictionary.
| | 01:22 |
And then we add an asterisk in objective C
every value that's not a primitive is
| | 01:26 |
going to have an asterisk when you declare it.
| | 01:29 |
And this is going to be called allNotes.
And inside getAllNotes we want to check if
| | 01:36 |
allNotes is initialized.
So, we're going to check if it's equal to
| | 01:40 |
nil, so that's the same as null in another
language and in here, the statements area,
| | 01:45 |
we're going to initialize it.
So, all notes equals and in double square
| | 01:51 |
brackets, the inner brackets are going to
say NSMutableDictionary, alloc, which
| | 01:56 |
allocates the memory for the NSMutableDictionary.
| | 02:00 |
And the outer brackets are going to
initialize it, so we just call init.
| | 02:05 |
And this is generally how you create an
object in objective C.
| | 02:10 |
You call it's class method, alloc to
allocate the memory and then you call that
| | 02:14 |
object's init method to initialize the object.
| | 02:19 |
And then at the bottom of this method
we're going to return allNotes.
| | 02:23 |
So, we've created our dictionary and we
have a method for accessing it.
| | 02:28 |
Let's create some helper methods to be
able to modify the notes in the
| | 02:31 |
dictionary.
So, go to data.h and we're going to create
| | 02:36 |
some more class methods.
Create one that returns void called
| | 02:41 |
setCurrentKey and this will set the
current key that we're working with so,
| | 02:46 |
when someone taps on a note to create it
then, it's going to create the key for
| | 02:51 |
that note.
And this method will allow to say which
| | 02:57 |
key we're working with.
So, we can have shorthand ways of
| | 03:00 |
manipulating our dictionary.
So, the value that it receives is a key
| | 03:04 |
and we'll create a few more helper methods.
| | 03:07 |
This one will return an NSString, call
this getCurrentKey.
| | 03:13 |
Next one will be setNote for current key.
And that will receive an NSString and
| | 03:21 |
we'll call that note,and then we'll create
one called setNoteForKey and then we'll
| | 03:26 |
create one called setNoteForKey.
So, set note and then we'll receive an
| | 03:34 |
NSString that we'll call note and a space
for key and it will receive an NSString
| | 03:39 |
we'll call key.
This will allow us to manipulate or change
| | 03:46 |
any notes.
And finally, one more method that will
| | 03:50 |
remove an object.
So, removeNoteForKey.
| | 03:57 |
That will receive the NSString for the key.
| | 04:01 |
And now we'll copy and paste all of those
methods that we just wrote.
| | 04:08 |
Right in to data.m.
And for each method, we're going to delete
| | 04:11 |
the semicolon and add some curly braces
just like we did before.
| | 04:15 |
(SOUND).
And then we'll need a string value for the
| | 04:24 |
current key.
So, let's create a static variable right
| | 04:27 |
below our NDMutableDictionary.
And by the way, in objective C, there's a
| | 04:32 |
different between a regular dictionary and
a mutable dictionary.
| | 04:36 |
So, a mutable dictionary can be changed
after you create it.
| | 04:39 |
If you take out the word mutable just in
NS dictionary, cannot be changed after
| | 04:43 |
it's created.
So, this is going to be a static NSString
| | 04:49 |
that's called currentKey.
So set currentKey.
| | 04:56 |
We'll set currentKey equal to key.
Get currentKey will return current key.
| | 05:06 |
Set note for current key, and we will call
self setNote for key.
| | 05:13 |
So, pass in the note and the currentKey.
And in setNoteForKey we're actually going
| | 05:19 |
to manipulate our dictionaries, so all
notes, set object, object will be the note
| | 05:23 |
that's passed in and the key will be the
key that's passed in.
| | 05:29 |
And finally removeNoteForKey, we call
allNotesRemoveObjectForKey and use the key
| | 05:33 |
that's passed it, and that way we can
manipulate the dictionary.
| | 05:39 |
And this nearly completes our data model.
| | 05:42 |
| | Collapse this transcript |
| Working with data from user defaults| 00:00 |
Now that our dictionary is set up to
manage our data we'll look at saving data
| | 00:04 |
to user defaults, we'll look at saving
data to persistent storage using
| | 00:07 |
NSUserDefaults.
Let's go to Data.h and create a new static
| | 00:13 |
method called saveNotes.
(SOUND) I'll just copy that line.
| | 00:21 |
And paste it to the bottom of Data.m.
(SOUND) And here we're going to save our
| | 00:26 |
notes to persistent storage.
So double brackets and the inner brackets,
| | 00:31 |
it's going to use the single tin for NSUserDefaults.
| | 00:36 |
So NSUserDefaults (SOUND) standardUserDefaults.
| | 00:39 |
And then, here what we do (SOUND) is we
call a setObject, passing in allNotes
| | 00:44 |
forKey, and then we pass in the kAllNotes key.
| | 00:50 |
That's the key that we defined in Data.h.
That's all you have to do to save an
| | 00:55 |
object to persistent storage.
This will stay on your device until the
| | 00:59 |
application is deleted, unless you use iCloud.
| | 01:04 |
For more information about iCloud, you can
watch the iCloud course, on lynda.com.
| | 01:09 |
So let's scroll up, and look at how to
read data from user defaults.
| | 01:13 |
When we initialize the NSMutableDictionary.
| | 01:18 |
After the word init, type With.
And we're going to initialize with a
| | 01:23 |
dictionary.
(SOUND) And the dictionary will come from
| | 01:27 |
user defaults.
So, double brackets here.
| | 01:29 |
And just like before.
NSUserDefaults, standardUserDefaults.
| | 01:33 |
And then it's going to be dictionaryforKey.
| | 01:35 |
And the key is going to be kAllNotes.
And that's it.
| | 01:40 |
Now when we call get all notes, if it's
not initialized, it will initialize from
| | 01:43 |
the persistent storage and the dictionary
will be empty of there's no data in the
| | 01:47 |
persistent storage yet.
And whenever we call saveNotes, all the
| | 01:52 |
data that we've saved in the dictionary
will be saved to persistent storage.
| | 01:57 |
| | Collapse this transcript |
| Saving data to a file| 00:00 |
So let's say instead of saving to user
defaults, you wanted to save some data to
| | 00:04 |
a file.
Now, that's actually pretty easy to do in
| | 00:07 |
iOS.
And we'll start in data.h and we're going
| | 00:11 |
to create a method that returns an ns string.
| | 00:15 |
And we'll call that filePath.
So, let's copy this, saved at the dot H.
| | 00:23 |
Go back into data dot M and under saved
notes, I'm going to paste file path and
| | 00:27 |
just want to note, this is a second option
for saving data.
| | 00:32 |
For the rest of the course, I'm actually
going to use user defaults.
| | 00:36 |
But, I wanted to include this anyway cause
it's likely that you'll want to know how
| | 00:39 |
to save a file at some point.
Now what we're going to do is return the
| | 00:43 |
full path to the file that we want to save.
| | 00:47 |
And now to do that, we're going to create
an NSArray, and we'll call this
| | 00:52 |
directories.
I'll get it equal to NS Search Path For
| | 00:57 |
Directories In Domains.
And the first value will be an NS document
| | 01:03 |
directory and an NS user domain mask and
then expand til the yes.
| | 01:10 |
So, in the next line, we're going to grab
our documents directory.
| | 01:14 |
NS string.
We'll call this documents and we're going
| | 01:18 |
to set it equal to directories objected to
index 0.
| | 01:23 |
On the next line we're going to return the
full path by calling the method documents
| | 01:29 |
stringByAppendingPathComponent and the
path component is going to be our all
| | 01:34 |
notes key.
So k:All Notes.
| | 01:39 |
So this gives us the file path to the file
that we want to save.
| | 01:43 |
Now if we scroll up Now I'm just going to
change where we set all notes equal to
| | 01:47 |
that dictionary.
Going to delete the brackets so I have
| | 01:53 |
only one closing single bracket at the end
of that line of code.
| | 01:59 |
I'm going to create some brackets, type NS dictionary.
| | 02:02 |
Dictionary with contents of file.
And the file is going to be the full path.
| | 02:09 |
So self File path.
So we initialize a dictionary by creating
| | 02:15 |
a new dictionary from that file.
Now, this might seem like a weird way to
| | 02:20 |
do things.
What I'm doing here is I'm creating the
| | 02:23 |
NSMutableDictionary, and if I call
initWith contents of file, which is also
| | 02:26 |
an instance file that I could call here.
If the file is empty or doesn't exist,
| | 02:33 |
then what will happen is the object will
not be initialized.
| | 02:38 |
So I initWithDictionary and if the file
doesn't exist then it will create an empty
| | 02:42 |
NSMutableDictionary rather than creating nothing.
| | 02:47 |
So, let's scroll down.
And in save notes, I'm going to delete
| | 02:51 |
this line of code.
Again, this method I'm showing is an
| | 02:56 |
alternate method for saving files, and for
the rest of the course, I am going to use
| | 02:59 |
NS user defaults.
But after these changes, you shouldn't
| | 03:03 |
have to make any more to make your
application compatible.
| | 03:06 |
So if you would rather use saving to a
file for the rest of the course.
| | 03:10 |
It should work just fine with everything
else that we're doing.
| | 03:12 |
So when we save the notes, we're going to
call all notes right to file.
| | 03:18 |
And the file's going to be self file path.
And for atomically I'll put in yes.
| | 03:24 |
And that's it.
So when you want to get a full path to a
| | 03:27 |
file, you need to isolate the documents directory.
| | 03:31 |
Then you call NS String, string by
appending path component.
| | 03:35 |
Tacking on the name of the file that you
want to save.
| | 03:38 |
And once you've done that, you can use
Objective C's built in methods for many of
| | 03:43 |
its basic classes like arrays and strings
and dictionaries.
| | 03:50 |
Such as dictionary with contents of file,
string with contents of file, array with
| | 03:54 |
contents of file etcetera.
And then to save the file you use this
| | 03:59 |
writeToFile methods.
| | 04:01 |
| | Collapse this transcript |
|
|
5. Creating the User InterfaceUnderstanding screens and storyboards| 00:00 |
Storyboards allow you to lay out your app
visually, and connect visual elements to
| | 00:05 |
code.
To view the storyboard for your
| | 00:09 |
application click on
mainstoryboard.storyboard on the left side
| | 00:12 |
of your screen.
Now again if you don't see the files here
| | 00:16 |
then just click this icon here to show the
project navigator.
| | 00:20 |
So again, mainstoryboard.storyboard.
And then we can see how the app is
| | 00:25 |
organized.
Now scroll to the right, and you'll see
| | 00:29 |
the master view.
So we have master up here.
| | 00:32 |
And then a simple table cell.
That's a single item in the list of items.
| | 00:38 |
So we're going to double click Master, and
I'll change this to Plain Ol' Notes.
| | 00:44 |
And I'll continue scrolling to the right,
and for detail, I'm going to double click
| | 00:48 |
Detail and change that to Note.
And if I want to configure options for any
| | 00:53 |
of these screens, I'm going to need to
show the utilities panel.
| | 00:59 |
Which is on the right and you can click
that button, on the right side of your
| | 01:02 |
screen, at the top, to show the ulitities view.
| | 01:05 |
I'm going to scroll to this detail view here.
| | 01:09 |
The utilities area, allows you to do
several different things and one of them
| | 01:13 |
is to connect objects that are visual
objects, to parts of your code.
| | 01:17 |
So you can connect a button, to call a
method when it's tapped.
| | 01:21 |
You can connect a label, like this one here.
| | 01:25 |
To a property inside of your code file.
We'll look at how to do that in just a
| | 01:30 |
second.
Also in the utilities view you can click
| | 01:33 |
and drag objects at the bottom onto the
screen, so I can drag a round rectangle
| | 01:36 |
button or segmented control or any other
(UNKNOWN) ness that I want here...
| | 01:43 |
Now what we want to do is to create a text
field for our note that covers the whole
| | 01:47 |
screen.
So I'm going to select this label here,
| | 01:51 |
and I'm going to delete it.
And then I'm going to scroll down in the
| | 01:55 |
objects area.
If you don't see that, you can just click
| | 01:58 |
this drop down menu.
Make sure you choose objects.
| | 02:01 |
You'll have to click on the objects tab to
show the object library.
| | 02:05 |
Scroll down until you find the text view
and then just click and drag one of those
| | 02:09 |
onto the screen, and it should
automatically fill the whole area of the
| | 02:12 |
screen.
Now this is going to autosize for a four
| | 02:17 |
inch iPhone or a 3.6 inch iPhone.
If you want to you can delete all the text
| | 02:23 |
in there but we're going to do that with
code anyway so you don't need to...
| | 02:28 |
And now we'll look at how to connect this
element to our code.
| | 02:32 |
So save the file and go to DetailViewController.h.
| | 02:36 |
And here a property is declared that is of
type IBOutlet UILabel, and its name is
| | 02:41 |
detailDescriptionLabel.
So we're going to delete it in code as
| | 02:45 |
well.
We'll declare at property in parenthesis
| | 02:50 |
weak, nonatomic.
And this will be IBOutlet, UITextview.
| | 02:53 |
And then it's going to be called tView.
Now we need to find a property like this.
| | 03:04 |
You need to save the file and go into the
implementation file.
| | 03:08 |
And under the implementation declaration,
you need to call at synthesize and type
| | 03:12 |
the name of the property you want to synthesize.
| | 03:17 |
And by synthesizing that, we don't have to
create getter and setter methods for that
| | 03:20 |
value.
And we access it with dot syntax.
| | 03:24 |
And then we'll need to scroll down and
just delete any references to
| | 03:28 |
detailDescriptionLabel.
And just change them to tView.
| | 03:32 |
And the text view has the same text
property that sets the text as the UI
| | 03:36 |
label does.
So that's all we need to do is just change
| | 03:40 |
that detailDescriptionLabel to text view.
And that was the only one in the code, so
| | 03:44 |
I'll hit Save.
And then go back to our storyboard.
| | 03:49 |
And because we declared that property that
we created as an IB outlet in our code.
| | 03:56 |
We can now have access to connecting it to
something in our storyboard.
| | 04:00 |
So, on the left side of the storyboard editor.
| | 04:03 |
At the bottom, find detail view controller.
| | 04:06 |
Hyphen detail.
And this will give us some values in the
| | 04:10 |
utility area the top right, that are
specific to our detail view controller
| | 04:14 |
class.
And this is where we can connect our code
| | 04:18 |
values, properties, and methods to u i
elements on the screen.
| | 04:24 |
So if you look in the Outlets section,
you'll see tView, and there's a circle to
| | 04:27 |
the right of it.
Click and drag that circle onto the text
| | 04:31 |
view, and release the mouse button.
You'll see that it's added the text view
| | 04:36 |
there, and now they are connected.
So we can modify that with our code.
| | 04:41 |
And that is actually all we need to do to
change the storyboard.
| | 04:44 |
So we'll save this file and what we should
see is, when we create a new entry, if we
| | 04:49 |
run this in simulator, that we have Plain
Ol' Notes here.
| | 04:54 |
We click the plus button, create a new entry.
| | 04:57 |
Click the item, and then it's at the top
left, showing that it's using this UI text
| | 05:01 |
view instead of the label which was centered.
| | 05:05 |
You'll see Note up here as well.
So using storyboards, you can easily edit
| | 05:09 |
the visual layouts of your applications
and easily connect UI elements to code
| | 05:13 |
objects.
| | 05:15 |
| | Collapse this transcript |
| Creating a new note| 00:00 |
Now we'll start writing the code for our
app by creating notes.
| | 00:05 |
So go to appdelegate.n.
What we're going to do here is initialize
| | 00:09 |
our model when the app launches.
Remember that in data.m we need to call
| | 00:14 |
get all notes to make sure all notes is initialized.
| | 00:20 |
So in app delegate.m we'll import the data
class, so that's data.h, and then inside
| | 00:25 |
application did finish launching with
options we're going to call data
| | 00:30 |
getAllNotes, so that'll initialize our dictionaries.
| | 00:36 |
Save the file and go to master view controller.m.
| | 00:41 |
Here, we'll import the data class as well.
Then scroll down to insert new objects.
| | 00:49 |
All of the items in the table view are
stored in an NSMutableArray.
| | 00:55 |
Again, mutable means it can be changed
after it's created.
| | 00:58 |
So I'm going to delete the line of code
that says objects insert object, and I'm
| | 01:02 |
going to replace it, and then I'm going to
write my own codes.
| | 01:08 |
In a string, this'll be the key.
And remember the key is going to be the
| | 01:12 |
current time stamp.
So what that is is NSDate.
| | 01:19 |
Date at class method.
And in the outer brackets description.
| | 01:25 |
On the next line, data setnote, note's
going to be kDefaultText.
| | 01:32 |
ForKey, pass in key.
On the next line, Data SetCurrentKey to
| | 01:39 |
key.
So we're creating that entry in our
| | 01:42 |
dictionary through these calls to the data class.
| | 01:46 |
Now on the next line in brackets _objects.
This is an instance variable that's
| | 01:51 |
declared at the top of the code.
And then, we're going to call
| | 01:59 |
insertObject, object is going to be the
key at index 0.
| | 02:07 |
So you can save that.
So now at this point, you could test it in
| | 02:12 |
the simulator, but you're not going to see
anything particularly interesting, because
| | 02:16 |
all we're doing is taking the same data
that we had before, and we're saving it as
| | 02:19 |
a note in our dictionary through these
data class methods.
| | 02:24 |
So through the rest of this chapter, we're
going to write the code for our
| | 02:27 |
application, so when you create the notes,
then you can edit them, update them if
| | 02:31 |
you'd like and delete them as well.
| | 02:35 |
| | Collapse this transcript |
| Editing and saving notes| 00:00 |
Now, that we've created the notes, let's
look at editing and saving the notes.
| | 00:04 |
So go to detailviewcontroller.m, and then
right under the import statement that's
| | 00:07 |
already there, importdata.h.
And then scroll down a little bit to the
| | 00:10 |
Configure View method.
Now, we're going to delete all the code
| | 00:14 |
that's in Configure view.
And we're going to grab the current note,
| | 00:24 |
so in a string current note, we're going
to set it equal to double brackets, data
| | 00:31 |
get all notes, object for key...
And then in other brackets, Data
| | 00:40 |
getCurrentKey.
So we're just grabbing that not string
| | 00:44 |
that's saved for the datas current key.
So we go to the next line and we're going
| | 00:49 |
to check to see if currentNote is the
default text.
| | 00:53 |
Then we'll go to the next line and create
an if statement that checks to see if the
| | 00:57 |
current note is not the default text.
Remember we set it as the default text in
| | 01:02 |
masterviewcontroller.m.
So we want to see if it's already been
| | 01:05 |
changed.
If so we're going to put that inside of
| | 01:07 |
the text field.
And if it is the default text, we're going
| | 01:11 |
to make sure the text field is blank.
Current note Is equal to string
| | 01:15 |
KdefaultText.
And then inside the if statement,
| | 01:20 |
self.tView.text equals currentNote.
And then an else statement below the if
| | 01:27 |
statement.
Self.tView.text and I'll set that equal to
| | 01:32 |
a blank NS string.
Again, remember NS string has an @ symbol
| | 01:36 |
before it.
Below the else statement, we're going to
| | 01:39 |
make sure the keyboard shows up, so
self.tView becomeFirstResponder, we're
| | 01:44 |
going to call that method.
And then when we're doing editing this and
| | 01:49 |
we go back to the Master View.
Then we want to make sure that we save any
| | 01:53 |
data that we've edited.
So right below Configure view, we're going
| | 02:00 |
to handle the event viewWillDisappear.
That means this particular screen is about
| | 02:07 |
to go away.
And we're going to go back to the Master
| | 02:11 |
View controller.
So in view will disappear.
| | 02:14 |
I want to make sure the text field is not blank.
| | 02:17 |
And if it's not blank, we're going to save
the data.
| | 02:20 |
And if the text field is blank, then we're
going to delete the note.
| | 02:25 |
So if And exclamation point
self.tView.text is equal to string.
| | 02:33 |
That's blank nsstring.
So if it's not blank, then we're going to
| | 02:36 |
save the note.
Data set note for current key and the
| | 02:41 |
note's going to be self.tView.text.
And if not, we're going to delete that
| | 02:49 |
note.
So data, data removeNoteForKey.
| | 02:54 |
And then pass in Data getCurrentKey.
And at the bottom of this method, finally
| | 03:00 |
we'll save notes.
Data saveNotes.
| | 03:04 |
So now, we've set it up so that you can
open the app in the simulator and really
| | 03:08 |
all you're going to notice now is that the
keyboard will show up when you click on a
| | 03:12 |
note.
So we'll test that now.
| | 03:17 |
And I'll click the plus button.
And there's the note, and then I'll click
| | 03:21 |
on it.
And we come to the single note view and
| | 03:25 |
the keyboard is showing up.
And then when we come back, the notes are
| | 03:29 |
saved, but that's happening behind the scenes.
| | 03:32 |
So as we go on, we'll look at displaying
the name of the note right here,
| | 03:35 |
automatically going to create a note and
all the other things that we need to edit
| | 03:39 |
with this main table view.
| | 03:43 |
| | Collapse this transcript |
| Controlling note display in a table view| 00:00 |
Now, let's modify masterviewcontroller.m
to make sure that the Table view displays
| | 00:05 |
the values instead of the keys.
And it also shows all of the saved data.
| | 00:12 |
So back in masterviewcontroller.m note,
that the NS mutable ray, called underscore
| | 00:17 |
objects.
And this is going to hold all of our data,
| | 00:20 |
and we'll look at how to manipulate it.
So if you scroll down and find the cell
| | 00:25 |
for row at index path method.
Now, all we want to do here is where we
| | 00:30 |
have cell.textlabel.text, instead of using
the object description, we're going to use
| | 00:33 |
that as a key.
So I'm going to type a bracket.
| | 00:40 |
After that and Xcode will automatically
write the opening bracket for that.
| | 00:45 |
And then I'm going to type data, get all notes.
| | 00:49 |
Object for key.
Then we pass in object description as the
| | 00:52 |
key.
Now, if I were to test this in the
| | 00:55 |
simulator, you would see the name of the
note, or whatever text that you saved.
| | 01:01 |
As the note instead of the key, which is
that timestamp.
| | 01:04 |
Now, let's make sure the notes are
organized in the order they were created
| | 01:09 |
with the newest ones on top.
So I'm going to scroll up right below
| | 01:16 |
viewDidLoad, and I'm going to create a
method called makeObjects.
| | 01:23 |
So now I'm going to initialize objects,
and we'll start by going to insert new
| | 01:26 |
objects, and deleting that if statement.
And replacing that with a call to self
| | 01:32 |
makeObjects.
And now, we'll instantiate objects inside
| | 01:36 |
of makeObjects.
Type underscore objects, equals, and then
| | 01:40 |
we're going to create a new NSMutableArray.
| | 01:43 |
Some brackets, NSMutable Array,
arrayWithArray, and the array is going to
| | 01:49 |
come from all of the keys that are saved
inside of our data object.
| | 01:56 |
Now, we're going to take those keys and
sort them by the date that they were
| | 01:59 |
created.
So double brackets here, Data getAllNotes.
| | 02:05 |
And then in the outer brackets, all keys.
So that returns an array of all the keys
| | 02:08 |
for that dictionary.
And then I sort it with some brackets
| | 02:12 |
underscore objects.
Sort using comparitor.
| | 02:16 |
And then with this highlighted right here
I'm going to press Return on my keyboard.
| | 02:20 |
And Xcode will finish writing out that
block for me.
| | 02:24 |
And here all I'm going to do is return the
value that checks to see which date is
| | 02:30 |
more recent.
So in brackets this is going to be a
| | 02:35 |
type-casted NSDate, and just keep in mind
here that this id data-type.
| | 02:43 |
Is a dynamic datatype that can be any type
of object data.
| | 02:47 |
Basically, anything that's not a primitive value.
| | 02:50 |
So we're going to typecast an NS date by
wrapping it in parentheses with an
| | 02:53 |
asterisk.
And then object two, space, compare.
| | 02:59 |
Same thing, we're going to cast an NS date
to object one.
| | 03:04 |
And that's it.
And that will, well, will give us the
| | 03:07 |
array sorted so that the newest one are on
the top.
| | 03:12 |
And now, what happens is whenever we make
an edit to the note, we want to make sure
| | 03:15 |
that the objects are recreated and the
Table view is reloaded.
| | 03:20 |
So right above make objects, I'm going to
handle the event View will appear.
| | 03:26 |
So this is going to be called, after we're
done editing in the detail view, and we
| | 03:30 |
come back.
We want to make sure that table gets
| | 03:34 |
reloaded with fresh data.
So first I'm going to call, superview will
| | 03:39 |
appear, passing and animated.
And then self, make objects and then we're
| | 03:45 |
going to reload the table view by calling
self dot table view reload data.
| | 03:52 |
And the very last thing that we want to do
is that when we create a new object you
| | 03:56 |
want to instantly transition to the detail view.
| | 04:00 |
And we'll do that at the very bottom of InsertObject.
| | 04:03 |
So we're going to call self perform segue,
with identifier, and a sender.
| | 04:11 |
The identifier is going to be the constant
value that we set in data.h which is
| | 04:16 |
kDetailView, and the sender is going to be self.
| | 04:21 |
And that's it.
And now, we can finally test it in the
| | 04:24 |
simulator and actually see that this app
is functioning like a normal app.
| | 04:28 |
So I click the Plus button.
I'm instantly taken over to the detail
| | 04:32 |
view.
And I'll type out here's an example note.
| | 04:37 |
And then click plain old notes to go back.
And there I see it right there, so if I
| | 04:42 |
create a new note, I'll call this another
note in here, and I go back to the table
| | 04:46 |
view and you'll see that it's at the top.
So now, that they are organized in the
| | 04:51 |
order that they were created with the
newest on top and when I click the plus
| | 04:55 |
button I instantly go to the detail view.
And if I click to go back with nothing
| | 05:00 |
written in there, a new note is not added.
And that makes us near complete with this
| | 05:06 |
application.
| | 05:07 |
| | Collapse this transcript |
| Deleting notes| 00:00 |
To delete notes, all we have to do is
modify the tableView commitEditingStyle
| | 00:04 |
method MasterViewController.m.
We need to make sure that the note is
| | 00:09 |
taken out of the table view, so you don't
see it anymore, and that it's deleted from
| | 00:14 |
our data model.
So, to do that, go in if statement, right
| | 00:19 |
above _objects removeObjectAtIndex.
(SOUND) We're going to go up one line
| | 00:25 |
(SOUND) and then I'm going to call Data removeNoteForKey.
| | 00:28 |
And we can get to key by calling _objects,
objectAtIndex and then passing an
| | 00:34 |
indexPath.row.
So, then, that's deleted from the data
| | 00:38 |
model.
And we want to make sure that the data
| | 00:40 |
model is saved.
So, Data saveNotes.
| | 00:44 |
And the template file already has the
object removed from the object's array,
| | 00:48 |
and the item deleted from the table.
So that's really all we have to do.
| | 00:54 |
Save and test in the simulator, and you
should be able to delete a note by
| | 00:57 |
clicking the Edit button, clicking the
minus sign, and then clicking Delete.
| | 01:04 |
And I can click Done and the note is deleted.
| | 01:06 |
I can also click and drag across a note to
delete it.
| | 01:10 |
So, I'll just click Done and to check to
see if that's saved, I'm going to click
| | 01:13 |
the Home button.
And I've clicked the Home button.
| | 01:17 |
I exit out of the app, and then I'm going
to click Stop in Xcode.
| | 01:21 |
And the reason why I'm doing that is if I
were to click Stop in Xcode while the app
| | 01:24 |
is running, it's not guaranteed to save.
But when I click the Home button, then, we
| | 01:29 |
can save the persistent data.
So if you ever notice that data that's
| | 01:33 |
supposed to be saved to the device is not
being saved, just make sure that you hit
| | 01:36 |
the Home button first and then stop the
app from running.
| | 01:41 |
So we'll run it one more time.
And there's just our single note there.
| | 01:47 |
And that complete our application, now
that we can create, edit, update, and
| | 01:51 |
delete notes.
| | 01:52 |
| | Collapse this transcript |
|
|
ConclusionNext steps| 00:00 |
Well, that concludes this course.
You may wonder where to go from here.
| | 00:04 |
Lynda.com has a huge list of courses in
iOS app development.
| | 00:09 |
There's iOS SDK Essential Training,
Objective-C Essential Training, Building
| | 00:15 |
and Monetizing Game Apps for iOS, IOS SDK
and SQLite: Building Data-Driven Apps, and
| | 00:21 |
Core Data for iOS and OS X.
If you make anything great based on what
| | 00:27 |
you learned in this course, let me know at
Twitter, @asktodd.
| | 00:31 |
See you next time.
| | 00:32 |
| | Collapse this transcript |
|
|