Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Start a free Basic trial
to watch this video
Create an iOS app that uses notifications to drive a simple user interace.
-
0:00
Now we're going to create an iOS application that uses notifications
-
0:04
to drive a simple user interface.
-
0:07
We'll start with an existing storyboard containing a view
-
0:10
with two buttons and a label.
-
0:12
Two buttons, red and
-
0:14
blue, will drive changes to the UI label using notifications.
-
0:19
Obviously this is not a typical way you set up a GUI to work.
-
0:23
But this is a good educational exercise for learning more about notifications.
-
0:30
So we've already established through Interface Builder IB
-
0:34
actions and an IB outlet for Motif label.
-
0:39
So this is the IB action.
-
0:41
These two are the IB action implementations for
-
0:45
the two buttons and we're going to drive everything on our user interface
-
0:50
through this view controller file.
-
0:53
So, the first thing we do is create
-
0:58
Constance for our notification names.
-
1:04
This is always a good idea when you're doing with NSStrings because
-
1:10
they are prone to typos and Xcode can't help you auto correct a string.
-
1:21
So we have RedButtonNotif and
-
1:26
[SOUND] the blue, BlueButtonNotif.
-
1:31
[SOUND] So the next thing we'll
-
1:37
do is post notifications within
-
1:44
our IB action methods when
-
1:49
the buttons are pressed.
-
1:54
So NS notification Center,
-
1:58
default center, post notification name.
-
2:06
And for the blue button, this will be blue notif.
-
2:13
And object will be nil.
-
2:18
And, similarly, for the red button,
-
2:22
NSNotificationCenter, postNotification.
-
2:29
This time it's RED_NOTIF Then within viewDidLoad,
-
2:38
we'll set up the blocks that will be listening for
-
2:43
those notification names.
-
2:46
So first [SOUND] we'll get a pointer to default center.
-
2:54
[SOUND] And then, we'll send a message to the center to addObserver and
-
3:03
we want the version of addObserver that takes block as a parameter.
-
3:11
And we are looking for, first of all, the Red_Notif.
-
3:16
And we're not concerned with the objects.
-
3:20
Don't really care about the queue.
-
3:23
And In the block, we'll be setting a text for the UI label.
-
3:29
So self notiflable.text
-
3:36
equals Got Red Notif.
-
3:42
Then we'll do the same thing for blue.
-
3:46
Send a message to the notification center, this time for the blue notification.
-
3:56
Object is nil, queue is nil and then for the block,
-
4:01
we'll be making the change to the notification
-
4:07
label text to indicate that we've got blue notification.
-
4:16
So the mechanics of how this is supposed to work is that when a button is pressed
-
4:23
a notification is posted, the name of which depends on the button.
-
4:29
And these blocks that have been registered as
-
4:36
observers for those notifications will make the change to b y label.
-
4:43
So, can run this and see how things go.
-
4:50
So here we have red and blue buttons.
-
4:53
Push blue, it says we got a blue notif, push red, red notif, blue notif.
-
5:01
And we look down here in the log, we see in addition to our red and
-
5:07
blue notifications, there's all these other notifications coming through.
-
5:12
And this is because in the AppDelegate,
-
5:14
we left the block that is listening for all notifications.
-
5:20
So, you see that, not only are there many notifications posted when an application
-
5:25
first starts but iOS continues to use notifications to communicate internally.
You need to sign up for Treehouse in order to download course files.
Sign up