Recording
How to disable and enable a button in Xcode 7 macbook pro
2015-11-05 · 10 min
Machine transcript · wording, timing and speaker attribution may be wrong
Wording, speaker labels, and timestamps can still be wrong. A transcript does not establish that statements in the recording are factual. Use the source links and surrounding context.
Source information & transcript revisions
Transcript revisions
- rev_0038354098a56c23e06cfd9634389992Machine-generated and unreviewed; may be wrong; not a verified quotation.enraw_asractive150 segments
Transcript
Hello and welcome to another Xcode
Segment details
0:00–0:03 · rev_0038354098a56c23e06cfd9634389992_0tutorial. So in this lesson, we'll be—
Segment details
0:04–0:06 · rev_0038354098a56c23e06cfd9634389992_1I'll be showing you how to make a button
Segment details
0:07–0:09 · rev_0038354098a56c23e06cfd9634389992_2and enable the button and disable the
Segment details
0:10–0:13 · rev_0038354098a56c23e06cfd9634389992_3button. So let's go ahead and create a new
Segment details
0:13–0:16 · rev_0038354098a56c23e06cfd9634389992_4project in Xcode by pressing File, then
Segment details
0:16–0:18 · rev_0038354098a56c23e06cfd9634389992_5New, and then Project, and then choose
Segment details
0:18–0:21 · rev_0038354098a56c23e06cfd9634389992_6Single View Application and just call it
Segment details
0:22–0:25 · rev_0038354098a56c23e06cfd9634389992_7whatever. I don't know, button. Whoops,
Segment details
0:25–0:28 · rev_0038354098a56c23e06cfd9634389992_8we're not typing in Chinese today. And
Segment details
0:28–0:31 · rev_0038354098a56c23e06cfd9634389992_9Objective-C. What else? Yeah,
Segment details
0:32–0:35 · rev_0038354098a56c23e06cfd9634389992_10just press Next and then Create.
Segment details
0:36–0:38 · rev_0038354098a56c23e06cfd9634389992_11Okay, so what do we have to do? First,
Segment details
0:41–0:45 · rev_0038354098a56c23e06cfd9634389992_12go to Main Storyboard, and this is our
Segment details
0:46–0:49 · rev_0038354098a56c23e06cfd9634389992_13ViewController. At the moment it's set
Segment details
0:50–0:53 · rev_0038354098a56c23e06cfd9634389992_14to this like funny size. I don't really
Segment details
0:54–0:55 · rev_0038354098a56c23e06cfd9634389992_15like it, so click on the top and what I
Segment details
0:56–0:58 · rev_0038354098a56c23e06cfd9634389992_16usually do is I disable Use Auto
Segment details
0:58–1:02 · rev_0038354098a56c23e06cfd9634389992_17Layout and disable Size Classes.
Segment details
1:02–1:04 · rev_0038354098a56c23e06cfd9634389992_18So now we have the iPhone size.
Segment details
1:07–1:08 · rev_0038354098a56c23e06cfd9634389992_19We also need to change this bit on Custom
Segment details
1:12–1:15 · rev_0038354098a56c23e06cfd9634389992_20Class. Oh no, not yet. Okay, first
Segment details
1:15–1:18 · rev_0038354098a56c23e06cfd9634389992_21right-click on your button and do New File
Segment details
1:18–1:21 · rev_0038354098a56c23e06cfd9634389992_22and do Cocoa Touch Class and just
Segment details
1:23–1:27 · rev_0038354098a56c23e06cfd9634389992_23leave it like this. You can name it— name
Segment details
1:28–1:31 · rev_0038354098a56c23e06cfd9634389992_24this bit to like Button or something,
Segment details
1:31–1:33 · rev_0038354098a56c23e06cfd9634389992_25it's up to you. And then
Segment details
1:33–1:36 · rev_0038354098a56c23e06cfd9634389992_26do Create, and then you'll see them
Segment details
1:36–1:40 · rev_0038354098a56c23e06cfd9634389992_27up here. You can move them down if you
Segment details
1:40–1:42 · rev_0038354098a56c23e06cfd9634389992_28want. But it's okay if you just leave
Segment details
1:43–1:44 · rev_0038354098a56c23e06cfd9634389992_29them. But I like to
Segment details
1:44–1:48 · rev_0038354098a56c23e06cfd9634389992_30move them down next to the main
Segment details
1:48–1:50 · rev_0038354098a56c23e06cfd9634389992_31storyboard. Now go to Custom Class on
Segment details
1:50–1:54 · rev_0038354098a56c23e06cfd9634389992_32the first view controller and change this
Segment details
1:54–1:57 · rev_0038354098a56c23e06cfd9634389992_33to Button. Okay,
Segment details
1:57–2:00 · rev_0038354098a56c23e06cfd9634389992_34so we're all set up.
Segment details
2:01–2:01 · rev_0038354098a56c23e06cfd9634389992_35Next we need is a button.
Segment details
2:10–2:11 · rev_0038354098a56c23e06cfd9634389992_36And what, what can we make the button do?
Segment details
2:13–2:15 · rev_0038354098a56c23e06cfd9634389992_37We can make the button make an image go
Segment details
2:17–2:19 · rev_0038354098a56c23e06cfd9634389992_38up. So first we need an image view. So
Segment details
2:19–2:22 · rev_0038354098a56c23e06cfd9634389992_39look for an image view in the bottom
Segment details
2:23–2:23 · rev_0038354098a56c23e06cfd9634389992_40right, drag it onto the screen. Now we
Segment details
2:24–2:28 · rev_0038354098a56c23e06cfd9634389992_41need an image. Okay, it doesn't
Segment details
2:28–2:32 · rev_0038354098a56c23e06cfd9634389992_42have to be an image view, we can do a
Segment details
2:32–2:33 · rev_0038354098a56c23e06cfd9634389992_43Whoops, label. Get rid of this.
Segment details
2:37–2:40 · rev_0038354098a56c23e06cfd9634389992_44Okay, so we'll make the label move up the
Segment details
2:42–2:43 · rev_0038354098a56c23e06cfd9634389992_45screen, and then we need a button.
Segment details
2:43–2:45 · rev_0038354098a56c23e06cfd9634389992_46There we go. And then just bring it down
Segment details
2:49–2:51 · rev_0038354098a56c23e06cfd9634389992_47here. We can name it to up.
Segment details
2:51–2:54 · rev_0038354098a56c23e06cfd9634389992_48Okay, first we have to declare, so go down
Segment details
2:56–3:00 · rev_0038354098a56c23e06cfd9634389992_49here, and then do open curly brackets and
Segment details
3:00–3:04 · rev_0038354098a56c23e06cfd9634389992_50close. And then we do IBOutlet UIButton,
Segment details
3:05–3:08 · rev_0038354098a56c23e06cfd9634389992_51and we'll just call it up. And we need
Segment details
3:10–3:12 · rev_0038354098a56c23e06cfd9634389992_52IBOutlet UILabel for our label.
Segment details
3:12–3:16 · rev_0038354098a56c23e06cfd9634389992_53We'll just call it label.
Segment details
3:16–3:17 · rev_0038354098a56c23e06cfd9634389992_54And then we need an IBAction. So that
Segment details
3:25–3:29 · rev_0038354098a56c23e06cfd9634389992_55when you press on the button, something
Segment details
3:29–3:30 · rev_0038354098a56c23e06cfd9634389992_56happens. And we'll call it Up— whoops,
Segment details
3:30–3:33 · rev_0038354098a56c23e06cfd9634389992_57no, just call it UpButton, and then
Segment details
3:38–3:41 · rev_0038354098a56c23e06cfd9634389992_58semicolon on the end. And then
Segment details
3:42–3:45 · rev_0038354098a56c23e06cfd9634389992_59we need to link it up. So go back to Main
Segment details
3:46–3:48 · rev_0038354098a56c23e06cfd9634389992_60Storyboard, click on the top, and then 6th
Segment details
3:49–3:51 · rev_0038354098a56c23e06cfd9634389992_61tab along Go to your label outlets and
Segment details
3:51–3:55 · rev_0038354098a56c23e06cfd9634389992_62then link it, and your button,
Segment details
3:56–3:58 · rev_0038354098a56c23e06cfd9634389992_63and your— where's the IBAction?
Segment details
4:02–4:04 · rev_0038354098a56c23e06cfd9634389992_64Oh, here it is, Received Actions, and then
Segment details
4:08–4:10 · rev_0038354098a56c23e06cfd9634389992_65on the button and do Touch Up Inside.
Segment details
4:10–4:12 · rev_0038354098a56c23e06cfd9634389992_66Okay, now we need to write the code. So
Segment details
4:18–4:19 · rev_0038354098a56c23e06cfd9634389992_67the button makes the label move.
Segment details
4:19–4:21 · rev_0038354098a56c23e06cfd9634389992_68So go into here, and under
Segment details
4:23–4:27 · rev_0038354098a56c23e06cfd9634389992_69implementation, I think we need a
Segment details
4:27–4:31 · rev_0038354098a56c23e06cfd9634389992_70semicolon here. Okay, so we go.
Segment details
4:31–4:34 · rev_0038354098a56c23e06cfd9634389992_71Let's do the label first.
Segment details
4:34–4:37 · rev_0038354098a56c23e06cfd9634389992_72So label, and we want the label to move
Segment details
4:38–4:42 · rev_0038354098a56c23e06cfd9634389992_73up every time we press the button. So do
Segment details
4:42–4:44 · rev_0038354098a56c23e06cfd9634389992_74label.center— was it label?
Segment details
4:45–4:48 · rev_0038354098a56c23e06cfd9634389992_75Yeah, label.center equals CGPoint.make
Segment details
4:51–4:55 · rev_0038354098a56c23e06cfd9634389992_76label.center— whoops— label.center.x,
Segment details
4:56–5:04 · rev_0038354098a56c23e06cfd9634389992_77label.center.y, and -25
Segment details
5:05–5:10 · rev_0038354098a56c23e06cfd9634389992_78to make it go up. And we
Segment details
5:10–5:14 · rev_0038354098a56c23e06cfd9634389992_79need brackets and then semicolon.
Segment details
5:14–5:17 · rev_0038354098a56c23e06cfd9634389992_80This is the wrong one. CGPoint make,
Segment details
5:19–5:21 · rev_0038354098a56c23e06cfd9634389992_81and then just get rid of all this.
Segment details
5:24–5:25 · rev_0038354098a56c23e06cfd9634389992_82Label.center CGPoint make. Make sure you
Segment details
5:31–5:34 · rev_0038354098a56c23e06cfd9634389992_83have the bracket there. Okay,
Segment details
5:35–5:38 · rev_0038354098a56c23e06cfd9634389992_84make sure you spell it right, so it's
Segment details
5:41–5:43 · rev_0038354098a56c23e06cfd9634389992_85capital L. Okay, so that's okay. Now we
Segment details
5:44–5:46 · rev_0038354098a56c23e06cfd9634389992_86need the button, so we do IBAction and
Segment details
5:47–5:49 · rev_0038354098a56c23e06cfd9634389992_87then upButton,
Segment details
5:50–5:51 · rev_0038354098a56c23e06cfd9634389992_88and then get rid of the semicolon, do open
Segment details
6:01–6:04 · rev_0038354098a56c23e06cfd9634389992_89curly brackets, close curly brackets.
Segment details
6:04–6:05 · rev_0038354098a56c23e06cfd9634389992_90Now I
Segment details
6:09–6:17 · rev_0038354098a56c23e06cfd9634389992_91got this wrong. You don't actually need
Segment details
6:17–6:19 · rev_0038354098a56c23e06cfd9634389992_92this void label, you need this bit inside
Segment details
6:19–6:21 · rev_0038354098a56c23e06cfd9634389992_93the button, and we can get rid of void
Segment details
6:22–6:23 · rev_0038354098a56c23e06cfd9634389992_94label. And when we do run,
Segment details
6:24–6:25 · rev_0038354098a56c23e06cfd9634389992_95press up and label goes up. Okay,
Segment details
6:30–6:33 · rev_0038354098a56c23e06cfd9634389992_96so that was easy. We only needed what this
Segment details
6:33–6:36 · rev_0038354098a56c23e06cfd9634389992_97IBAction. Okay, so now I'll show you how
Segment details
6:36–6:39 · rev_0038354098a56c23e06cfd9634389992_98to disable the button.
Segment details
6:39–6:40 · rev_0038354098a56c23e06cfd9634389992_99How should we do it? We could make a
Segment details
6:45–6:48 · rev_0038354098a56c23e06cfd9634389992_100second button to disable the first button.
Segment details
6:48–6:51 · rev_0038354098a56c23e06cfd9634389992_101Okay, so copy and paste the button. And we
Segment details
6:55–6:59 · rev_0038354098a56c23e06cfd9634389992_102can just call it disable,
Segment details
6:59–7:03 · rev_0038354098a56c23e06cfd9634389992_103disable button.
Segment details
7:05–7:06 · rev_0038354098a56c23e06cfd9634389992_104And then, or we can also have enable
Segment details
7:10–7:14 · rev_0038354098a56c23e06cfd9634389992_105button.
Segment details
7:14–7:14 · rev_0038354098a56c23e06cfd9634389992_106Then go into button.h and then
Segment details
7:21–7:24 · rev_0038354098a56c23e06cfd9634389992_107IBOutlet UIButton,
Segment details
7:25–7:26 · rev_0038354098a56c23e06cfd9634389992_108EnableButton, and just copy
Segment details
7:29–7:32 · rev_0038354098a56c23e06cfd9634389992_109this and paste it and change this to
Segment details
7:33–7:35 · rev_0038354098a56c23e06cfd9634389992_110Disable.
Segment details
7:35–7:35 · rev_0038354098a56c23e06cfd9634389992_111Okay, then we need 2 more IBActions,
Segment details
7:39–7:41 · rev_0038354098a56c23e06cfd9634389992_112EnableButton.
Segment details
7:46–7:46 · rev_0038354098a56c23e06cfd9634389992_113Disable button.
Segment details
7:55–7:56 · rev_0038354098a56c23e06cfd9634389992_114And now we have to link those up.
Segment details
8:00–8:01 · rev_0038354098a56c23e06cfd9634389992_115Where is it? Disable button.
Segment details
8:05–8:07 · rev_0038354098a56c23e06cfd9634389992_116Let's move them a bit further apart.
Segment details
8:07–8:11 · rev_0038354098a56c23e06cfd9634389992_117And enable button. And then
Segment details
8:13–8:16 · rev_0038354098a56c23e06cfd9634389992_118disable button received actions, touch up
Segment details
8:16–8:20 · rev_0038354098a56c23e06cfd9634389992_119inside.
Segment details
8:20–8:20 · rev_0038354098a56c23e06cfd9634389992_120Is that up? Get rid of these. I don't know
Segment details
8:25–8:28 · rev_0038354098a56c23e06cfd9634389992_121why it's done this.
Segment details
8:28–8:29 · rev_0038354098a56c23e06cfd9634389992_122This is up. And then enable button on the
Segment details
8:32–8:35 · rev_0038354098a56c23e06cfd9634389992_123enable button, touch up inside.
Segment details
8:35–8:37 · rev_0038354098a56c23e06cfd9634389992_124Okay, so So we need IBAction
Segment details
8:40–8:44 · rev_0038354098a56c23e06cfd9634389992_125here, disableButton,
Segment details
8:44–8:47 · rev_0038354098a56c23e06cfd9634389992_126and what we do is up— this
Segment details
8:52–8:56 · rev_0038354098a56c23e06cfd9634389992_127is the name of our up button.
Segment details
8:57–8:58 · rev_0038354098a56c23e06cfd9634389992_128We do up.enabled and
Segment details
9:07–9:12 · rev_0038354098a56c23e06cfd9634389992_129equals yes— no, equals no.
Segment details
9:12–9:14 · rev_0038354098a56c23e06cfd9634389992_130So that should stop the up button from
Segment details
9:16–9:18 · rev_0038354098a56c23e06cfd9634389992_131working. Let's test it out.
Segment details
9:18–9:19 · rev_0038354098a56c23e06cfd9634389992_132So it's working now. Disable, it's
Segment details
9:26–9:28 · rev_0038354098a56c23e06cfd9634389992_133disabled, so it doesn't work now. It's
Segment details
9:29–9:30 · rev_0038354098a56c23e06cfd9634389992_134grayed out. Okay, let's enable
Segment details
9:30–9:33 · rev_0038354098a56c23e06cfd9634389992_135it. IBAction,
Segment details
9:34–9:36 · rev_0038354098a56c23e06cfd9634389992_136you want EnableButton,
Segment details
9:37–9:38 · rev_0038354098a56c23e06cfd9634389992_137and then
Segment details
9:42–9:48 · rev_0038354098a56c23e06cfd9634389992_138we do up.enabled equals
Segment details
9:49–9:52 · rev_0038354098a56c23e06cfd9634389992_139yes.
Segment details
9:53–9:53 · rev_0038354098a56c23e06cfd9634389992_140So it's moving up. We disable the button,
Segment details
9:59–10:01 · rev_0038354098a56c23e06cfd9634389992_141it's not moving up anymore. Then we enable
Segment details
10:01–10:04 · rev_0038354098a56c23e06cfd9634389992_142the button, and now we can continue moving
Segment details
10:04–10:06 · rev_0038354098a56c23e06cfd9634389992_143up. So that was all for the tutorial.
Segment details
10:06–10:09 · rev_0038354098a56c23e06cfd9634389992_144It's quite simple really, but it actually
Segment details
10:09–10:12 · rev_0038354098a56c23e06cfd9634389992_145took me a long time to figure it out. So
Segment details
10:13–10:15 · rev_0038354098a56c23e06cfd9634389992_146hopefully you can use this for something
Segment details
10:16–10:18 · rev_0038354098a56c23e06cfd9634389992_147in your app, and I'll see you next
Segment details
10:18–10:22 · rev_0038354098a56c23e06cfd9634389992_148time. Thanks for watching!
Segment details
10:22–10:24 · rev_0038354098a56c23e06cfd9634389992_149