1 00:00:00,000 --> 00:00:02,000 [Seminar] [iOS: Writing Apps Like a Boss] 2 00:00:02,000 --> 00:00:04,000 [Tommy MacWilliam] [Harvard University] 3 00:00:04,000 --> 00:00:08,000 [This is CS50.] [CS50.TV] 4 00:00:08,000 --> 00:00:12,000 >> All right, everyone, welcome to iOS: Writing Apps like a Boss. 5 00:00:12,000 --> 00:00:16,000 This seminar is going to focus on writing apps for the iPhone 6 00:00:16,000 --> 00:00:19,000 and in turn writing apps for the iPad, and so we're basically going to walk through 7 00:00:19,000 --> 00:00:23,000 a couple different examples of how to make an app, a simple game like Tic Tac Toe, 8 00:00:23,000 --> 00:00:26,000 or if you're more interested in making an app like some kind of news reader 9 00:00:26,000 --> 00:00:30,000 or something that interacts with the Internet I will talk about that as well. 10 00:00:30,000 --> 00:00:32,000 Here's our brief agenda. 11 00:00:32,000 --> 00:00:36,000 >> IOS apps are written in a language called Objective-C, 12 00:00:36,000 --> 00:00:39,000 and so this is a little bit similar to C but not really at all, 13 00:00:39,000 --> 00:00:41,000 so we'll be talking a little bit about the language itself 14 00:00:41,000 --> 00:00:45,000 and then how to build iPhone apps using this app program called XCode, 15 00:00:45,000 --> 00:00:48,000 which if you haven't downloaded it yet feel free to start the download now. 16 00:00:48,000 --> 00:00:50,000 It's several gigabytes. 17 00:00:50,000 --> 00:00:54,000 It should be free on the App Store, so you'll need to have a Mac 18 00:00:54,000 --> 00:00:57,000 ideally running the latest version of OS X. 19 00:00:57,000 --> 00:00:59,000 If you're not, no problem. 20 00:00:59,000 --> 00:01:02,000 We have Macs in the science center available for your use 21 00:01:02,000 --> 00:01:05,000 with XCode installed, and so feel free to use those for development. 22 00:01:05,000 --> 00:01:07,000 If you want to make an app but don't have a Mac, don't worry. 23 00:01:07,000 --> 00:01:09,000 There's plenty of campus resources for that. 24 00:01:09,000 --> 00:01:15,000 And so then we're going to cover 2 larger examples of different apps you can make. 25 00:01:15,000 --> 00:01:19,000 Objective-C is technically what's called a super set of C. 26 00:01:19,000 --> 00:01:24,000 That means that any C code is also valid Objective-C code. 27 00:01:24,000 --> 00:01:29,000 That means that we kind of duct taped on some additional features to C. 28 00:01:29,000 --> 00:01:33,000 Some of these features include not having to ever write malloc again, thank God, 29 00:01:33,000 --> 00:01:36,000 no having to worry about those stupid pointers and freeing them and all that stuff 30 00:01:36,000 --> 00:01:40,000 you hated about C, and it's kind of gone in Objective-C. 31 00:01:40,000 --> 00:01:43,000 >> Now, Objective-C also has a much larger standard library, 32 00:01:43,000 --> 00:01:47,000 so there's a lot more functionality you get for free inside of Objective-C. 33 00:01:47,000 --> 00:01:50,000 Remember when we were writing PHP we noticed that we went from 34 00:01:50,000 --> 00:01:54,000 this smallest language to the giant library of all these crazy things you can do. 35 00:01:54,000 --> 00:01:56,000 The same thing happens in iOS. 36 00:01:56,000 --> 00:01:59,000 There's objects for things like the Buddhist calendar, and really anything 37 00:01:59,000 --> 00:02:05,000 you can possibly think of probably exists already in Objective-C's implementation. 38 00:02:05,000 --> 00:02:08,000 The objective part of Objective-C is referencing something 39 00:02:08,000 --> 00:02:11,000 called Object-Oriented Programming. 40 00:02:11,000 --> 00:02:14,000 This is kind of a new concept, but we've actually mostly learned 41 00:02:14,000 --> 00:02:17,000 a lot of these concepts already. 42 00:02:17,000 --> 00:02:19,000 The idea behind Object-Oriented Programming is that you're going to structure 43 00:02:19,000 --> 00:02:23,000 a lot of your code around these things called classes, 44 00:02:23,000 --> 00:02:26,000 and these classes are really glorified structs. 45 00:02:26,000 --> 00:02:30,000 Inside of a struct we basically said here is a thing, 46 00:02:30,000 --> 00:02:32,000 and this thing can have members. 47 00:02:32,000 --> 00:02:35,000 For example, a node in a linked list could have other things 48 00:02:35,000 --> 00:02:38,000 like a value, a pointer to the next node in the list, 49 00:02:38,000 --> 00:02:44,000 and together that pointer and that value composed this one instance of a struct. 50 00:02:44,000 --> 00:02:47,000 Classes are very similar except 51 00:02:47,000 --> 00:02:50,000 classes can also have functions inside of them. 52 00:02:50,000 --> 00:02:54,000 When we declared a struct, we could only say int n or node *next. 53 00:02:54,000 --> 00:02:57,000 >> Now with Objective-C we can actually put functions 54 00:02:57,000 --> 00:02:59,000 inside of those things. 55 00:02:59,000 --> 00:03:03,000 Another thing that classes can do is they can inherit data from other classes. 56 00:03:03,000 --> 00:03:07,000 For example, we'll be looking at a bunch of built-in Objective-C classes. 57 00:03:07,000 --> 00:03:11,000 One of them could be the class that represents the view for a screen, 58 00:03:11,000 --> 00:03:14,000 and so by saying I want to implement my own view 59 00:03:14,000 --> 00:03:18,000 we basically say someone over at Apple, probably really nice people, 60 00:03:18,000 --> 00:03:21,000 wrote this class for me, and it handles things like displaying buttons 61 00:03:21,000 --> 00:03:25,000 or rendering the screen, and we would be really pained 62 00:03:25,000 --> 00:03:28,000 to implement all that functionality ourselves, and so by simply 63 00:03:28,000 --> 00:03:31,000 inheriting data we can say everything that you did in that class 64 00:03:31,000 --> 00:03:34,000 I also want inside of my class, and then I'm going to do some other stuff, 65 00:03:34,000 --> 00:03:37,000 namely implement an app. 66 00:03:37,000 --> 00:03:40,000 That's what that word inheritance means. We'll see a more concrete example. 67 00:03:40,000 --> 00:03:42,000 >> And finally, the key with Object-Oriented Programming is that 68 00:03:42,000 --> 00:03:45,000 it leads to data encapsulation. 69 00:03:45,000 --> 00:03:48,000 In some of our problem sets we could have these big, global variables 70 00:03:48,000 --> 00:03:51,000 and globals everywhere, and that's how we'd keep track of state. 71 00:03:51,000 --> 00:03:55,000 With classes we can start encapsulating information inside of an object. 72 00:03:55,000 --> 00:03:58,000 If we have one screen on our app, we don't really need to have any data from 73 00:03:58,000 --> 00:04:01,000 another screen in our app inside of that, 74 00:04:01,000 --> 00:04:04,000 and so by encapsulating things within classes this actually leads 75 00:04:04,000 --> 00:04:08,000 to much better code design, and this is possible with some of the additional features 76 00:04:08,000 --> 00:04:11,000 of Objective-C. 77 00:04:11,000 --> 00:04:15,000 Forewarning, the syntax for Objective-C is bananas. 78 00:04:15,000 --> 00:04:19,000 It is like nothing we've seen before, 79 00:04:19,000 --> 00:04:22,000 so it is a little bit of a learning curve getting used to 80 00:04:22,000 --> 00:04:24,000 what the heck does this mean? 81 00:04:24,000 --> 00:04:27,000 But once you get past that initial learning curve it's really, really smooth 82 00:04:27,000 --> 00:04:29,000 to start writing apps. 83 00:04:29,000 --> 00:04:33,000 >> To declare a class, to say here is my class, it exists, 84 00:04:33,000 --> 00:04:35,000 and maybe here are some things that I'm going to define later, 85 00:04:35,000 --> 00:04:38,000 I'm going to say @interface. 86 00:04:38,000 --> 00:04:41,000 I'm going to say @interface. I'm going to give my class a name. 87 00:04:41,000 --> 00:04:43,000 And then later somewhere else I'm going to say @end, 88 00:04:43,000 --> 00:04:46,000 and everything in between the @interface and the @end 89 00:04:46,000 --> 00:04:48,000 is going to be my class. 90 00:04:48,000 --> 00:04:51,000 That is going to be what's inside of our .h files. 91 00:04:51,000 --> 00:04:55,000 Just like in C our .h files basically said here are some things that will exist. 92 00:04:55,000 --> 00:04:57,000 I'm not necessarily telling you what they do yet, 93 00:04:57,000 --> 00:04:59,000 but the compiler needs to know that they exist. 94 00:04:59,000 --> 00:05:04,000 Later inside of our .m files—because m for Objective-C 95 00:05:04,000 --> 00:05:08,000 is where we're actually going to define what these classes do. 96 00:05:08,000 --> 00:05:11,000 Much like our .c files, we provided an implementation for functions. 97 00:05:11,000 --> 00:05:15,000 Inside of our .m file we're going to say here is what all of the functions 98 00:05:15,000 --> 00:05:18,000 inside of my class—what they all do. 99 00:05:18,000 --> 00:05:22,000 And finally, inside of a struct we could say inside of those curly braces 100 00:05:22,000 --> 00:05:25,000 int n or node *next, 101 00:05:25,000 --> 00:05:28,000 and in Objective-C we're going to use the keyword @property, 102 00:05:28,000 --> 00:05:31,000 and this is what's going to define the properties, 103 00:05:31,000 --> 00:05:36,000 or the non-functions that are a part of my classes. 104 00:05:36,000 --> 00:05:40,000 >> Calling functions in Objective-C is also bananas. 105 00:05:40,000 --> 00:05:43,000 When we declared functions in C we said something like int foo 106 00:05:43,000 --> 00:05:46,000 and open paren and then gave it a list of arguments. 107 00:05:46,000 --> 00:05:51,000 This is what declaring methods or functions looks like in Objective-C. 108 00:05:51,000 --> 00:05:54,000 If I want to declare a function or a method 109 00:05:54,000 --> 00:05:58,000 where I'm saying method is really just a function that's a member of a class, 110 00:05:58,000 --> 00:06:01,000 so kind of interchangeable, but not really, 111 00:06:01,000 --> 00:06:03,000 so inside of my method I want to create a new method. 112 00:06:03,000 --> 00:06:06,000 It's going to return nothing, so it's going to be of type void. 113 00:06:06,000 --> 00:06:08,000 This is the return type of my function. 114 00:06:08,000 --> 00:06:11,000 Now we have parens here because—I don't know. 115 00:06:11,000 --> 00:06:14,000 Next is going to be the name of my function, 116 00:06:14,000 --> 00:06:17,000 and then finally we have a semicolon just like we had in C. 117 00:06:17,000 --> 00:06:19,000 What's new here is this guy here. 118 00:06:19,000 --> 00:06:22,000 This hyphen is actually necessary, and what this says is that 119 00:06:22,000 --> 00:06:26,000 this method here must be called on an instance of a class. 120 00:06:26,000 --> 00:06:29,000 >> After we declared our structs we probably said something like 121 00:06:29,000 --> 00:06:32,000 struct node n, and that actually created 122 00:06:32,000 --> 00:06:35,000 or instantiated one of those structs so that I could actually start working 123 00:06:35,000 --> 00:06:38,000 with what's inside of it, so this dash means that we have to 124 00:06:38,000 --> 00:06:41,000 instantiate the class just like we instantiated that struct 125 00:06:41,000 --> 00:06:43,000 before we can call this method on it. 126 00:06:43,000 --> 00:06:46,000 If I want to start adding arguments to my methods 127 00:06:46,000 --> 00:06:48,000 it gets even more bananas. 128 00:06:48,000 --> 00:06:50,000 Here's my method name. 129 00:06:50,000 --> 00:06:53,000 Then I'm going to have a colon, and after this colon it says here comes some arguments. 130 00:06:53,000 --> 00:06:56,000 This method takes one argument. 131 00:06:56,000 --> 00:06:59,000 The type of its argument is an int, and the name of that argument 132 00:06:59,000 --> 00:07:03,000 or the variable that I'm going to start using inside of the method is called i. 133 00:07:03,000 --> 00:07:05,000 Again, this is method. It takes one argument. 134 00:07:05,000 --> 00:07:10,000 >> If you want to start adding more arguments it gets more bananas 135 00:07:10,000 --> 00:07:13,000 in that we have this colon that says here comes my list of arguments. 136 00:07:13,000 --> 00:07:15,000 This first argument is an integer. 137 00:07:15,000 --> 00:07:17,000 Now, this second argument is interesting. 138 00:07:17,000 --> 00:07:20,000 The variable that I'm going to be using inside of my function 139 00:07:20,000 --> 00:07:23,000 is called f, so inside of my function I could say 140 00:07:23,000 --> 00:07:25,000 f + = 1 or something. 141 00:07:25,000 --> 00:07:30,000 This thing here is basically a key for that argument or parameter. 142 00:07:30,000 --> 00:07:34,000 Just like we had key value pairs and something like JSON or associative arrays 143 00:07:34,000 --> 00:07:37,000 Objective-C made the decision to say, okay, just so that it's really clear 144 00:07:37,000 --> 00:07:39,000 when you call a method what all these parameters are 145 00:07:39,000 --> 00:07:41,000 I'm actually going to name them all. 146 00:07:41,000 --> 00:07:45,000 When you call a method, you'll actually say andFloat, 147 00:07:45,000 --> 00:07:47,000 and then you'll pass it in. 148 00:07:47,000 --> 00:07:50,000 >> Interestingly, this one is not named, but all of the other parameters are, 149 00:07:50,000 --> 00:07:53,000 so if we had a 3rd argument I could say andFloat 150 00:07:53,000 --> 00:07:56,000 and another float and so on. 151 00:07:56,000 --> 00:08:01,000 When I call these methods it is of course bananas, 152 00:08:01,000 --> 00:08:04,000 so if I have an object, and I defined a method called foo, 153 00:08:04,000 --> 00:08:09,000 and I want to call it, rather than saying foo open/close parens 154 00:08:09,000 --> 00:08:11,000 I'm going to open a bracket. 155 00:08:11,000 --> 00:08:13,000 Here's the name of my method. 156 00:08:13,000 --> 00:08:17,000 I'm going to close the bracket, and this is the object I'm calling it on. 157 00:08:17,000 --> 00:08:20,000 Remember, all of these methods exist within classes 158 00:08:20,000 --> 00:08:23,000 because classes can have methods defined inside of them. 159 00:08:23,000 --> 00:08:26,000 >> Here I've arbitrarily created an object of some class, 160 00:08:26,000 --> 00:08:28,000 and this is the syntax for doing that. 161 00:08:28,000 --> 00:08:31,000 There are 2 steps to creating an object. 162 00:08:31,000 --> 00:08:34,000 The first step is to say I want to allocate space. 163 00:08:34,000 --> 00:08:37,000 This is the equivalent of a malloc. We don't have to say malloc anymore. 164 00:08:37,000 --> 00:08:42,000 We dropped the m, made it alloc, and replaced the .c with .m. 165 00:08:42,000 --> 00:08:47,000 After we alloc something we then need to initialize it. 166 00:08:47,000 --> 00:08:49,000 Basically when you create objects you might want to have some logic 167 00:08:49,000 --> 00:08:53,000 that executes when they're created, so you can pass in some default values 168 00:08:53,000 --> 00:08:57,000 or something like that, and so this init method is what actually creates the object. 169 00:08:57,000 --> 00:08:59,000 We first allocate space, just like we did in C, 170 00:08:59,000 --> 00:09:04,000 and then we initialize it, which may or may not do a whole lot. 171 00:09:04,000 --> 00:09:07,000 Then we're returning this thing into an object o. 172 00:09:07,000 --> 00:09:09,000 There's a star here because this is technically a pointer, 173 00:09:09,000 --> 00:09:12,000 but don't worry, pointers aren't that big a deal in Objective-C anymore. 174 00:09:12,000 --> 00:09:15,000 >> Now we've instantiated this class called NSObject, 175 00:09:15,000 --> 00:09:19,000 and this is just a random class that Apple has. 176 00:09:19,000 --> 00:09:22,000 We've instantiated this, and now I have an instance of this class 177 00:09:22,000 --> 00:09:25,000 in this object o, so that means that if I defined 178 00:09:25,000 --> 00:09:29,000 these methods I could call them like this. 179 00:09:29,000 --> 00:09:31,000 Similarly, if I wanted to call a method with one argument, 180 00:09:31,000 --> 00:09:34,000 so this is the bar method, that takes one argument, 181 00:09:34,000 --> 00:09:38,000 and here is the baz: qux method, so this takes 2 arguments. 182 00:09:38,000 --> 00:09:44,000 This is calling one function on this object o. 183 00:09:44,000 --> 00:09:46,000 Make sense? 184 00:09:46,000 --> 00:09:50,000 The syntax should make sense, but you kind of get used to it. 185 00:09:50,000 --> 00:09:54,000 >> Okay, let's talk about a few things that are built into Objective-C 186 00:09:54,000 --> 00:09:56,000 that weren't necessarily built into C. 187 00:09:56,000 --> 00:09:59,000 In C we kind of had to deal with strings as these stupid character arrays, 188 00:09:59,000 --> 00:10:01,000 and it got really annoying. 189 00:10:01,000 --> 00:10:04,000 Objective-C has those all built in for us, and it's built in 190 00:10:04,000 --> 00:10:06,000 using this class called NSString. 191 00:10:06,000 --> 00:10:10,000 When I want to create an NSString we have more arcane syntax. 192 00:10:10,000 --> 00:10:15,000 Rather than saying "cs50" we say @"cs50" 193 00:10:15,000 --> 00:10:17,000 and this is just the syntax for declaring strings in Objective-C. 194 00:10:17,000 --> 00:10:21,000 This is extremely easy to forget, so don't. 195 00:10:21,000 --> 00:10:24,000 Now, once I have this, this is a string, but notice 196 00:10:24,000 --> 00:10:26,000 it's really just an object. 197 00:10:26,000 --> 00:10:30,000 I said NSString, which means I instantiated the class 198 00:10:30,000 --> 00:10:32,000 called NSString, which someone else wrote for me, 199 00:10:32,000 --> 00:10:35,000 and they were very nice about it, and so now 200 00:10:35,000 --> 00:10:37,000 I can start calling methods on it. 201 00:10:37,000 --> 00:10:40,000 If I call the method length on this object s 202 00:10:40,000 --> 00:10:43,000 it's going to return to me the length of the string. 203 00:10:43,000 --> 00:10:45,000 This is just like strlen in C. 204 00:10:45,000 --> 00:10:47,000 This would return 4. 205 00:10:47,000 --> 00:10:51,000 >> Similarly, another method I might want to care about is this characterAtIndex. 206 00:10:51,000 --> 00:10:54,000 This is a method that says on this string s 207 00:10:54,000 --> 00:10:57,000 I want you to get the zeroth character, 208 00:10:57,000 --> 00:10:59,000 and so this would return to me the character c, 209 00:10:59,000 --> 00:11:02,000 and there's a whole bunch more of these methods that you can Google really easily. 210 00:11:02,000 --> 00:11:07,000 Apple's documentation is great, and we'll take a look at that in a bit. 211 00:11:07,000 --> 00:11:09,000 Those are strings. 212 00:11:09,000 --> 00:11:11,000 We also have variable size arrays built in. 213 00:11:11,000 --> 00:11:13,000 Remember in C when we declared an array 214 00:11:13,000 --> 00:11:16,000 we had to say you have 5 elements, end of story. 215 00:11:16,000 --> 00:11:18,000 When we got to JavaScript and PHP we could start 216 00:11:18,000 --> 00:11:20,000 doing things like adding elements or moving elements. 217 00:11:20,000 --> 00:11:22,000 We can do the same in Objective-C. 218 00:11:22,000 --> 00:11:26,000 Rather than create an array in the normal C way 219 00:11:26,000 --> 00:11:30,000 we have again another class called NSMutableArray. 220 00:11:30,000 --> 00:11:33,000 There's also NSArray, 221 00:11:33,000 --> 00:11:35,000 and this is going to basically encapsulate some array. 222 00:11:35,000 --> 00:11:38,000 This says the first thing I want to do is I want to allocate 223 00:11:38,000 --> 00:11:41,000 space for a new array, and after I allocate it 224 00:11:41,000 --> 00:11:43,000 I then need to initialize it. 225 00:11:43,000 --> 00:11:45,000 Again, just calling these 2 methods. 226 00:11:45,000 --> 00:11:48,000 Now this means that inside of this object a 227 00:11:48,000 --> 00:11:50,000 I have an empty array sitting there. 228 00:11:50,000 --> 00:11:54,000 If I want to add something to this array, I can call the addObject method. 229 00:11:54,000 --> 00:11:59,000 I want to add an object to the array a, and I want to add the string CS50. 230 00:11:59,000 --> 00:12:02,000 If I wanted to conversely remove that I can say I want to 231 00:12:02,000 --> 00:12:09,000 remove the object at the first place on the array or object a. 232 00:12:09,000 --> 00:12:11,000 >> Make sense? 233 00:12:11,000 --> 00:12:14,000 Okay, you kind of get used to this square bracket thing. 234 00:12:14,000 --> 00:12:18,000 By the way, inside of a lot of Apple's libraries you'll see this NS. 235 00:12:18,000 --> 00:12:21,000 The NS actually stands for next step, which was one of Steve Jobs first companies, 236 00:12:21,000 --> 00:12:24,000 and that's where he really started writing a lot of the code 237 00:12:24,000 --> 00:12:27,000 as kind of the basis for Mac OS X and all of the other stuff, 238 00:12:27,000 --> 00:12:32,000 and so this NS is kind of this nice legacy shout out to one of the earlier companies 239 00:12:32,000 --> 00:12:34,000 back when Apple was first starting out. 240 00:12:34,000 --> 00:12:36,000 It's everywhere. 241 00:12:36,000 --> 00:12:41,000 Let's take a look at a more holistic Objective-C example. 242 00:12:41,000 --> 00:12:44,000 Here I am inside of XCode. 243 00:12:44,000 --> 00:12:47,000 To get here, I first downloaded XCode from the App Store, 244 00:12:47,000 --> 00:12:50,000 opened it up, and then I went up here to file, 245 00:12:50,000 --> 00:12:54,000 over here to new, and then project. 246 00:12:54,000 --> 00:12:57,000 After I do that I have all these options of what I want to create, 247 00:12:57,000 --> 00:12:59,000 and so we'll take a look at these options later, 248 00:12:59,000 --> 00:13:03,000 but just for this example, because we're not actually going to have an app yet, 249 00:13:03,000 --> 00:13:06,000 I came down here, and I said Command Line Tool, 250 00:13:06,000 --> 00:13:09,000 and this is an app that I could run at the command line 251 00:13:09,000 --> 00:13:12,000 just like we've been running from C. 252 00:13:12,000 --> 00:13:16,000 That's how I created this project, and so now I'm here, 253 00:13:16,000 --> 00:13:20,000 so let's first look at this file, and this should look pretty familiar. 254 00:13:20,000 --> 00:13:24,000 I have an int name. There's my friend argc, my other buddy argv. 255 00:13:24,000 --> 00:13:30,000 And so we can see that this is the entry point for my first Objective-C application. 256 00:13:30,000 --> 00:13:32,000 Here we can ignore this for now. 257 00:13:32,000 --> 00:13:35,000 This is basically some memory management stuff that you won't really 258 00:13:35,000 --> 00:13:37,000 ever have to worry about. 259 00:13:37,000 --> 00:13:39,000 >> Let's look at this first block here. 260 00:13:39,000 --> 00:13:46,000 This first line, if I say Student* alice = [[Student alloc] init] what's that doing? 261 00:13:46,000 --> 00:13:50,000 This first student here, this is probably a class. 262 00:13:50,000 --> 00:13:54,000 This isn't a class that Apple wrote, but it's a class that I wrote. 263 00:13:54,000 --> 00:13:57,000 The first thing I want to do is I want to allocate space for a new student, 264 00:13:57,000 --> 00:14:00,000 and then I want to initialize it, so this gives me back 265 00:14:00,000 --> 00:14:05,000 this new student object, and I'm storing this in a variable called Alice. 266 00:14:05,000 --> 00:14:07,000 Where did that class come from? 267 00:14:07,000 --> 00:14:12,000 Well, over here on the left side these are all of the different files inside of my project. 268 00:14:12,000 --> 00:14:16,000 We can see here I have a Student.h and a Student.m. 269 00:14:16,000 --> 00:14:20,000 The .h file, remember, is where I declare all of the things 270 00:14:20,000 --> 00:14:22,000 that will exist within the class. 271 00:14:22,000 --> 00:14:25,000 >> Let's take a look at that. 272 00:14:25,000 --> 00:14:29,000 All right, here we have this @interface, and this says that here comes 273 00:14:29,000 --> 00:14:33,000 the declarations of everything that will exist within my class. 274 00:14:33,000 --> 00:14:36,000 Then I have a colon. Then I have this NSObject thing. 275 00:14:36,000 --> 00:14:40,000 This colon signifies that inheritance bit we were discussing a little bit earlier. 276 00:14:40,000 --> 00:14:43,000 This says everything an NSObject can do 277 00:14:43,000 --> 00:14:46,000 where NSObject is this class written by somebody else, 278 00:14:46,000 --> 00:14:50,000 everything this NSObject can do I want to be able to do that. 279 00:14:50,000 --> 00:14:54,000 By saying :NSObject that means that I basically 280 00:14:54,000 --> 00:14:58,000 inherited all of the functionality of another class. 281 00:14:58,000 --> 00:15:02,000 That really gave me a whole bunch of different methods and properties that I can use. 282 00:15:02,000 --> 00:15:05,000 Down here I'm creating 2 properties. 283 00:15:05,000 --> 00:15:08,000 That means my student, if this were a struct, these would be the 2 things 284 00:15:08,000 --> 00:15:11,000 inside of my struct, so every student has a name 285 00:15:11,000 --> 00:15:14,000 that is a string, and a student also has a grade, 286 00:15:14,000 --> 00:15:17,000 which is an int. 287 00:15:17,000 --> 00:15:23,000 >> Finally, down here I'm going to create a method for my student. 288 00:15:23,000 --> 00:15:26,000 I called my method, initWithName, and it takes one argument, 289 00:15:26,000 --> 00:15:31,000 and that argument is a string, and I called it name. 290 00:15:31,000 --> 00:15:35,000 Now let's look at how we actually implemented this class. 291 00:15:35,000 --> 00:15:38,000 Here, now I'm inside of my .m file, 292 00:15:38,000 --> 00:15:40,000 m for implementation, I guess. 293 00:15:40,000 --> 00:15:44,000 I have my implementation, my end, and here is where I'm actually defining 294 00:15:44,000 --> 00:15:47,000 what initWithName does. 295 00:15:47,000 --> 00:15:50,000 I have initWithName, the name of my parameter, and then this 296 00:15:50,000 --> 00:15:53,000 is where I'm actually creating a student, 297 00:15:53,000 --> 00:15:56,000 and so this is a little bit cryptic, but this is kind of boilerplate 298 00:15:56,000 --> 00:15:58,000 that you want to include in your constructors. 299 00:15:58,000 --> 00:16:02,000 This initialization function here, initWithName, is a type of constructor. 300 00:16:02,000 --> 00:16:05,000 You're basically constructing a new student object 301 00:16:05,000 --> 00:16:07,000 and maybe sending some data inside of it. 302 00:16:07,000 --> 00:16:11,000 The first thing I want to do is I want to call init on this super thing. 303 00:16:11,000 --> 00:16:15,000 >> Remember that when I said back here in the .h file 304 00:16:15,000 --> 00:16:21,000 that everything an NSObject has a student also has. 305 00:16:21,000 --> 00:16:24,000 That means when I create a student what I also need to do is 306 00:16:24,000 --> 00:16:28,000 make sure that the NSObject that I'm inheriting all of that data from 307 00:16:28,000 --> 00:16:32,000 is also initialized properly. 308 00:16:32,000 --> 00:16:36,000 What I need to say is this super is actually going to refer to the parent class 309 00:16:36,000 --> 00:16:39,000 that I'm inheriting from, so I want to make sure I initialize 310 00:16:39,000 --> 00:16:43,000 everything that I'm depending on before I start trying to use it. 311 00:16:43,000 --> 00:16:46,000 Then if that initialized correctly this is just like saying if malloc 312 00:16:46,000 --> 00:16:50,000 did not return null then I can start setting some properties. 313 00:16:50,000 --> 00:16:54,000 >> In JavaScript and PHP we had this keyword called this, 314 00:16:54,000 --> 00:16:58,000 and this referred to the current instance of a class. 315 00:16:58,000 --> 00:17:00,000 In Objective-C we call this self. 316 00:17:00,000 --> 00:17:04,000 When I say self.name, that means that the object 317 00:17:04,000 --> 00:17:07,000 I just created when I said student alloc init, 318 00:17:07,000 --> 00:17:09,000 that's going to give me back an object. 319 00:17:09,000 --> 00:17:12,000 That means I want to set the name of that object 320 00:17:12,000 --> 00:17:15,000 to whatever I just passed in. 321 00:17:15,000 --> 00:17:18,000 Just like in C, we access members with this dot, 322 00:17:18,000 --> 00:17:21,000 so self.name says the name of the student object 323 00:17:21,000 --> 00:17:24,000 is now going to be whatever you just passed in. 324 00:17:24,000 --> 00:17:28,000 And so finally, I can return it so I actually get something back. 325 00:17:28,000 --> 00:17:30,000 >> Questions? 326 00:17:30,000 --> 00:17:34,000 Okay, so this self = super init, 327 00:17:34,000 --> 00:17:37,000 if you don't totally understand the inheritance stuff don't worry. 328 00:17:37,000 --> 00:17:40,000 Just know that if you ever want to make your own init method just 329 00:17:40,000 --> 00:17:42,000 do that, and you'll be good to go. 330 00:17:42,000 --> 00:17:44,000 Yeah.>>[Student] What does if self mean? 331 00:17:44,000 --> 00:17:49,000 This means when we malloc something we always checked if it was equal to null, 332 00:17:49,000 --> 00:17:51,000 and if it was null, then we exited. 333 00:17:51,000 --> 00:17:55,000 This is the same thing, because if this returns null, then we're probably going to seg fault 334 00:17:55,000 --> 00:17:57,000 if we start trying to manipulate it. 335 00:17:57,000 --> 00:18:01,000 That's our student class. 336 00:18:01,000 --> 00:18:03,000 That means we can initialize our students in one of two ways. 337 00:18:03,000 --> 00:18:08,000 If I say student alloc init I'm not using that method that I just wrote, 338 00:18:08,000 --> 00:18:11,000 and instead I can simply say alice.name, 339 00:18:11,000 --> 00:18:14,000 and now I'm going to set that property name. 340 00:18:14,000 --> 00:18:17,000 >> Similarly, if I want to use that initWithName method 341 00:18:17,000 --> 00:18:20,000 I can simply say alloc, and then rather than saying init 342 00:18:20,000 --> 00:18:24,000 I'm going to call that method that I just created, and I'm going to pass in Bob. 343 00:18:24,000 --> 00:18:30,000 At this point, this object Bob has a name equal to Bob. 344 00:18:30,000 --> 00:18:35,000 Okay, down here I'm using that NSMutableArray that we looked at earlier. 345 00:18:35,000 --> 00:18:38,000 I'm allocating space.Then I'm initializing a new array. 346 00:18:38,000 --> 00:18:40,000 I'm going to add 2 things to it. 347 00:18:40,000 --> 00:18:43,000 This array now holds student objects. 348 00:18:43,000 --> 00:18:46,000 Notice that nowhere did I have to say this is an array of students. 349 00:18:46,000 --> 00:18:48,000 I said it's an array, period. 350 00:18:48,000 --> 00:18:50,000 Then I can put whatever inside of it that I want. 351 00:18:50,000 --> 00:18:52,000 Here I have 2 objects. 352 00:18:52,000 --> 00:18:56,000 >> Finally, I have another object here, this TF. 353 00:18:56,000 --> 00:18:59,000 Over here in TF.h basically the same thing. 354 00:18:59,000 --> 00:19:01,000 I'm inheriting from NSObject, and by the way, 355 00:19:01,000 --> 00:19:03,000 when you create classes this is all done for you, 356 00:19:03,000 --> 00:19:06,000 this kind of interface boilerplate. 357 00:19:06,000 --> 00:19:08,000 It has a property of students. 358 00:19:08,000 --> 00:19:15,000 I have a couple methods here that don't really do a whole lot, 359 00:19:15,000 --> 00:19:18,000 and so that means after I create this TF object 360 00:19:18,000 --> 00:19:23,000 I can call this method grade on it like this. 361 00:19:23,000 --> 00:19:26,000 Any questions on Objective-C syntax before we start moving into some more 362 00:19:26,000 --> 00:19:30,000 interesting apps development stuff? 363 00:19:30,000 --> 00:19:34,000 >> Okay, so let's actually make an iPhone app. 364 00:19:34,000 --> 00:19:39,000 The core classes that you'll be using inside of your iPhone app are called view controllers, 365 00:19:39,000 --> 00:19:42,000 and a view controller basically represents a single screen 366 00:19:42,000 --> 00:19:46,000 inside of your app, so if I'm on the music app, for example, 367 00:19:46,000 --> 00:19:50,000 one view controller could represent the view in which I view all the songs on my iPhone. 368 00:19:50,000 --> 00:19:53,000 Another view controller could be when I click a song and start playing it 369 00:19:53,000 --> 00:19:55,000 or as I'm drilling down into artists. 370 00:19:55,000 --> 00:19:59,000 Each of those different screens could be represented as a different view controller, 371 00:19:59,000 --> 00:20:04,000 and a view controller is really just a class that says how this screen works. 372 00:20:04,000 --> 00:20:07,000 Things inside of a view controller, we're going to have properties, 373 00:20:07,000 --> 00:20:10,000 so things like a button is going to be a property of our view controller. 374 00:20:10,000 --> 00:20:13,000 >> We're also going to have methods, and these are basically event handlers. 375 00:20:13,000 --> 00:20:16,000 This method says when you press this button 376 00:20:16,000 --> 00:20:19,000 I want to do something, and finally, again, 377 00:20:19,000 --> 00:20:24,000 we're going to be using this self keyword to access the current instance. 378 00:20:24,000 --> 00:20:29,000 To build interfaces in iOS is actually really, really easy. 379 00:20:29,000 --> 00:20:32,000 They have this nice drag and drop interface called Interface Builder, 380 00:20:32,000 --> 00:20:37,000 and the 2 core concepts that wire up your Objective-C to Interface Builder 381 00:20:37,000 --> 00:20:40,000 are IBOutlet and IBAction. 382 00:20:40,000 --> 00:20:44,000 An IBOutlet simply says that if you declare a property that's a button, 383 00:20:44,000 --> 00:20:47,000 and you want to hook it up to something in your actual UI, 384 00:20:47,000 --> 00:20:49,000 you're going to say it's an outlet. 385 00:20:49,000 --> 00:20:51,000 Similarly, if you want to represent an event handler 386 00:20:51,000 --> 00:20:54,000 then you're going to say it's an action. 387 00:20:54,000 --> 00:20:57,000 >> To actually wire up this graphical representation 388 00:20:57,000 --> 00:21:00,000 and your code it's really, really simple. 389 00:21:00,000 --> 00:21:03,000 If you want to attach an IBOutlet, all you have to do is you control click, 390 00:21:03,000 --> 00:21:05,000 and we'll see an example of this really quick. 391 00:21:05,000 --> 00:21:07,000 You control click where it says View Controller. 392 00:21:07,000 --> 00:21:09,000 You're going to drag into the interface, or conversely, 393 00:21:09,000 --> 00:21:13,000 if you want to hook up an event handler you're going to drag from the interface 394 00:21:13,000 --> 00:21:15,000 in the other direction. 395 00:21:15,000 --> 00:21:20,000 Let's take a look at a really simple iOS example. 396 00:21:20,000 --> 00:21:23,000 >> Let's create a new project. 397 00:21:23,000 --> 00:21:25,000 I'm going to come up here to Application, 398 00:21:25,000 --> 00:21:28,000 and I'm going to click Single View Application. 399 00:21:28,000 --> 00:21:31,000 I'm going to click next. I'll give my project a name. 400 00:21:31,000 --> 00:21:33,000 I'll call it Hello. 401 00:21:33,000 --> 00:21:36,000 Interestingly, Apple assumes you're creating a product 402 00:21:36,000 --> 00:21:38,000 so that you can sell it and they can make money. 403 00:21:38,000 --> 00:21:41,000 Down here I'm going to say that this is an iPhone app. 404 00:21:41,000 --> 00:21:44,000 You can create an iPad app, or if you want to create one of those apps 405 00:21:44,000 --> 00:21:47,000 that supports both devices you can do that too. 406 00:21:47,000 --> 00:21:49,000 These are what you want your checkmarks to look like. 407 00:21:49,000 --> 00:21:51,000 >> You want to use storyboards, which we'll see later, 408 00:21:51,000 --> 00:21:54,000 and you definitely want to use automatic reference counting, 409 00:21:54,000 --> 00:21:58,000 which is a nice feature that prevents you from having to say malloc and free. 410 00:21:58,000 --> 00:22:03,000 Unless you want to call malloc and free, I would leave this checked. 411 00:22:03,000 --> 00:22:07,000 I'll click next, and finally, this is going to ask me where I want to save it. 412 00:22:07,000 --> 00:22:14,000 I'll hit create, and here we go. 413 00:22:14,000 --> 00:22:16,000 I created a new project. 414 00:22:16,000 --> 00:22:19,000 Over here on the left are all the files that are inside of my project, 415 00:22:19,000 --> 00:22:22,000 and notice that I got a whole bunch, and I didn't even do anything. 416 00:22:22,000 --> 00:22:24,000 IOS is great. 417 00:22:24,000 --> 00:22:27,000 >> For example, here this ViewController.h, 418 00:22:27,000 --> 00:22:30,000 this is going to represent my first view controller, 419 00:22:30,000 --> 00:22:32,000 so the first screen inside of my app. 420 00:22:32,000 --> 00:22:34,000 Now we know what this is saying. 421 00:22:34,000 --> 00:22:36,000 We're saying I'm calling this class ViewController, 422 00:22:36,000 --> 00:22:40,000 and a ViewController does everything that a UIViewController does, 423 00:22:40,000 --> 00:22:43,000 and this, again, is some class that Apple wrote that does a lot of handy stuff for us 424 00:22:43,000 --> 00:22:46,000 like display the screen. 425 00:22:46,000 --> 00:22:50,000 Here is where I can actually start defining what my view controller does, 426 00:22:50,000 --> 00:22:52,000 and it turns out that I really don't need any of this. 427 00:22:52,000 --> 00:22:55,000 This is boilerplate code that Apple gives me for free. 428 00:22:55,000 --> 00:22:59,000 I did need that first line, or I don't have a class, 429 00:22:59,000 --> 00:23:02,000 so we can get rid of that and get rid of this. 430 00:23:02,000 --> 00:23:05,000 Okay, so this is my empty screen. 431 00:23:05,000 --> 00:23:08,000 >> Now let's click on this MainStoryboard.storyboard, 432 00:23:08,000 --> 00:23:11,000 and this is where it starts to get interesting. 433 00:23:11,000 --> 00:23:14,000 This represents the first screen on my app. 434 00:23:14,000 --> 00:23:17,000 If I want to add a button, in HTML I had to create a button tag. 435 00:23:17,000 --> 00:23:20,000 In Android you have to create a button tag, 436 00:23:20,000 --> 00:23:23,000 but in iOS if I just come down here to the bottom right 437 00:23:23,000 --> 00:23:27,000 and if I click on this 3rd one here where it says Objects, 438 00:23:27,000 --> 00:23:31,000 and I can scroll down, or I can start searching for button. 439 00:23:31,000 --> 00:23:35,000 And look, a button, so if I actually drag and drop this right there, 440 00:23:35,000 --> 00:23:38,000 I've just added a button to this screen on my app. 441 00:23:38,000 --> 00:23:41,000 If I want to change the text, I can double click it, 442 00:23:41,000 --> 00:23:47,000 say something enticing like "Press Me." 443 00:23:47,000 --> 00:23:51,000 Okay, now if I run this app, so we compile it, 444 00:23:51,000 --> 00:23:54,000 so to run it I click the play button in the top left, and there's my app. 445 00:23:54,000 --> 00:23:58,000 I didn't do anything, and I got a sweet looking iPhone app. 446 00:23:58,000 --> 00:24:01,000 If I want to stop it, you can click the stop button 447 00:24:01,000 --> 00:24:03,000 because it's more fun. 448 00:24:03,000 --> 00:24:07,000 >> Let's say that I actually want something to happen when I press this button. 449 00:24:07,000 --> 00:24:09,000 To do that what I need to do is I need to create 450 00:24:09,000 --> 00:24:13,000 a new event handler or an action. 451 00:24:13,000 --> 00:24:16,000 That means that I need to create some method that I want to be called 452 00:24:16,000 --> 00:24:18,000 when I press the button, so let's create a new method. 453 00:24:18,000 --> 00:24:20,000 I'm inside of ViewController.h. 454 00:24:20,000 --> 00:24:22,000 I need to say that a method exists. 455 00:24:22,000 --> 00:24:26,000 I need a hyphen first because I'm going to be calling this on the view controller. 456 00:24:26,000 --> 00:24:28,000 I need to give this a type. 457 00:24:28,000 --> 00:24:31,000 The type of this is going to be that IBAction thing that we saw earlier. 458 00:24:31,000 --> 00:24:35,000 This is an event handler, so it's going to return an IBAction, 459 00:24:35,000 --> 00:24:38,000 and this is a hint to XCode to say that 460 00:24:38,000 --> 00:24:40,000 this is something I want to wire something to. 461 00:24:40,000 --> 00:24:45,000 I can give it a name, like buttonPressed, semicolon. 462 00:24:45,000 --> 00:24:48,000 >> Now I've declared a new method inside of my class. 463 00:24:48,000 --> 00:24:50,000 I've said this method has to exist. 464 00:24:50,000 --> 00:24:53,000 Now let's come into ViewController.m, 465 00:24:53,000 --> 00:24:56,000 and let's say what this method can do. 466 00:24:56,000 --> 00:25:03,000 If I start typing, for example, (void)buttonPressed 467 00:25:03,000 --> 00:25:06,000 notice XCode is really nice and autocompletes for me. 468 00:25:06,000 --> 00:25:09,000 That's really wonderful. 469 00:25:09,000 --> 00:25:12,000 Notice here that inside of the .m file I can also say void, 470 00:25:12,000 --> 00:25:15,000 and this is because that IBAction isn't actually a type. 471 00:25:15,000 --> 00:25:19,000 It's actually hashtag defined somewhere to be a void, 472 00:25:19,000 --> 00:25:22,000 and again, this is just a hint to XCode that says 473 00:25:22,000 --> 00:25:25,000 I want this to be an event handler, and we'll see why in just a second. 474 00:25:25,000 --> 00:25:28,000 When this button is pressed I'm going to do something annoying 475 00:25:28,000 --> 00:25:30,000 like display a popup. 476 00:25:30,000 --> 00:25:35,000 >> To do that I can create a new instance of this class called UIAlertView, 477 00:25:35,000 --> 00:25:39,000 and this is a class that Apple wrote that's going to display annoying popups. 478 00:25:39,000 --> 00:25:43,000 We'll call this popup alert, and I have 2 steps, remember, to creating this object. 479 00:25:43,000 --> 00:25:46,000 The first thing I need to do is allocate space. 480 00:25:46,000 --> 00:25:48,000 I want a UIAlertView. 481 00:25:48,000 --> 00:25:51,000 I want to allocate space. That's my first method. 482 00:25:51,000 --> 00:25:53,000 My next method is I want to initialize it, 483 00:25:53,000 --> 00:25:58,000 and so I have this big, long method called initWithTitle. 484 00:25:58,000 --> 00:26:01,000 That's basically going to control what this popup says. 485 00:26:01,000 --> 00:26:04,000 The title of my popup can be hello. 486 00:26:04,000 --> 00:26:08,000 The message of this popup can be "This is iOS." 487 00:26:08,000 --> 00:26:10,000 Delegate thing, I don't know what that is. 488 00:26:10,000 --> 00:26:13,000 Let's say it's nothing. 489 00:26:13,000 --> 00:26:18,000 Now the button that's going to appear can say something like "It sure is," 490 00:26:18,000 --> 00:26:24,000 and I don't really want any other buttons, so let's delete that and close the bracket. 491 00:26:24,000 --> 00:26:27,000 >> Okay, I created an extra one. There we go. 492 00:26:27,000 --> 00:26:30,000 This is how I can create a new popup. 493 00:26:30,000 --> 00:26:35,000 If I want to actually show the popup I want to call the show method. 494 00:26:35,000 --> 00:26:38,000 To do that I can say alert and show, 495 00:26:38,000 --> 00:26:40,000 and again, autocomplete was super nice. 496 00:26:40,000 --> 00:26:42,000 If I forgot what that was, if I just typed in s, 497 00:26:42,000 --> 00:26:45,000 I can scroll through here to figure out what it was, 498 00:26:45,000 --> 00:26:48,000 and it filters nicely. 499 00:26:48,000 --> 00:26:52,000 Now I created this new popup. 500 00:26:52,000 --> 00:26:55,000 We'll come back to what delegate means later, 501 00:26:55,000 --> 00:26:58,000 and now I want to say I want this method to be fired 502 00:26:58,000 --> 00:27:01,000 when I press the button, so I'm going to come back to my storyboard, 503 00:27:01,000 --> 00:27:04,000 and I want to now attach this IBAction. 504 00:27:04,000 --> 00:27:06,000 The first thing you want to do is click the button. 505 00:27:06,000 --> 00:27:08,000 When I press this button I want something to happen. 506 00:27:08,000 --> 00:27:10,000 I'm not going to hold down control. 507 00:27:10,000 --> 00:27:13,000 I'm going to click and drag from the button 508 00:27:13,000 --> 00:27:15,000 to over here where it says View Controller. 509 00:27:15,000 --> 00:27:17,000 We can see that it nicely lights up. 510 00:27:17,000 --> 00:27:22,000 >> If I let go with my mouse I now have this popup over here where I have some options. 511 00:27:22,000 --> 00:27:24,000 One of these is the events that I can register. 512 00:27:24,000 --> 00:27:28,000 These are all of those methods I declared in my h file as IBActions. 513 00:27:28,000 --> 00:27:31,000 This is how XCode knows what should appear in this little list, 514 00:27:31,000 --> 00:27:33,000 so that's just a hint. 515 00:27:33,000 --> 00:27:37,000 If I click on button pressed, I've now registered the event handler. 516 00:27:37,000 --> 00:27:41,000 In JavaScript we had to say I have some code that registered the event handler. 517 00:27:41,000 --> 00:27:43,000 In Objective-C it was really that easy. 518 00:27:43,000 --> 00:27:46,000 If I run this again 519 00:27:46,000 --> 00:27:49,000 now when I press the button my event handler is going to fire, 520 00:27:49,000 --> 00:27:51,000 and I'm going to get this popup. 521 00:27:51,000 --> 00:27:54,000 Super, super simple there. 522 00:27:54,000 --> 00:27:57,000 >> If you ever want to see all of the events that happen to be registered 523 00:27:57,000 --> 00:28:00,000 on a component if I click on this button 524 00:28:00,000 --> 00:28:02,000 and I come over here to the right side 525 00:28:02,000 --> 00:28:05,000 first you can see over here I can do things like the type of the button, 526 00:28:05,000 --> 00:28:08,000 so if you want one of those I's or the add contact button 527 00:28:08,000 --> 00:28:10,000 or whatever. 528 00:28:10,000 --> 00:28:13,000 If I want to see all of the events that are on this button 529 00:28:13,000 --> 00:28:16,000 if I come all the way over here to the right side 530 00:28:16,000 --> 00:28:19,000 we can see here at the events I have all of these different events. 531 00:28:19,000 --> 00:28:23,000 I can press the button, when I let go of the button, when I double tap or whatever, 532 00:28:23,000 --> 00:28:26,000 and the one I just registered is this event called Touch Up Inside, 533 00:28:26,000 --> 00:28:29,000 and this says that as soon as my finger comes off the button 534 00:28:29,000 --> 00:28:32,000 that event is going to fire, and that's exactly what just happened. 535 00:28:32,000 --> 00:28:36,000 This is kind of the default button pressed event. 536 00:28:36,000 --> 00:28:39,000 >> Any questions so far? 537 00:28:39,000 --> 00:28:43,000 Okay, that's how we can start to wire up things in our code 538 00:28:43,000 --> 00:28:46,000 into things inside of our interface. 539 00:28:46,000 --> 00:28:49,000 Remember the first thing we had to do was to find the code, 540 00:28:49,000 --> 00:28:54,000 and then we wired up the interface to the code, 541 00:28:54,000 --> 00:28:57,000 and there's our first app. 542 00:28:57,000 --> 00:29:00,000 Okay, that was really cool, and we created this button. 543 00:29:00,000 --> 00:29:03,000 What if we don't want to have to create a bunch of properties 544 00:29:03,000 --> 00:29:05,000 representing these buttons? 545 00:29:05,000 --> 00:29:08,000 For example, in Tic Tac Toe I have 9 buttons, 546 00:29:08,000 --> 00:29:11,000 and it'd be super, super annoying to have to drag and drop 9 times 547 00:29:11,000 --> 00:29:14,000 or if I had to make Tic Tac Toe with 81 instead of 9 548 00:29:14,000 --> 00:29:17,000 and I had to drag and drop 81 times, and that's lame. 549 00:29:17,000 --> 00:29:20,000 What we can do instead is much like an HTML 550 00:29:20,000 --> 00:29:23,000 when we had things like IDs and names and we can search for things 551 00:29:23,000 --> 00:29:27,000 by their ID, there's a similar notion in iOS called tags. 552 00:29:27,000 --> 00:29:31,000 >> A tag is simply a unique numerical identifier for a component. 553 00:29:31,000 --> 00:29:34,000 If I say this has a tag of 0, for example, 554 00:29:34,000 --> 00:29:38,000 if I create a button and give it a tag of 0, and we'll see how to do that in just a second, 555 00:29:38,000 --> 00:29:41,000 if I want to get that button I can simply say I want to call 556 00:29:41,000 --> 00:29:45,000 the viewWithTag method on the object over here, 557 00:29:45,000 --> 00:29:48,000 the self.view, which represents the current screen, for example. 558 00:29:48,000 --> 00:29:53,000 If I call that viewWithTag method, I'm going to pull back the button with tag 0. 559 00:29:53,000 --> 00:29:58,000 Let's take a look at this by building Tic Tac Toe. 560 00:29:58,000 --> 00:30:01,000 First, this is my storyboard. 561 00:30:01,000 --> 00:30:05,000 I've created these 10 UI buttons. 562 00:30:05,000 --> 00:30:07,000 Notice they're all the same size. 563 00:30:07,000 --> 00:30:11,000 If I click one of these and I come back over here on this right side 564 00:30:11,000 --> 00:30:15,000 you'll see I adjusted the font right here, so I made the font a little bit bigger, 565 00:30:15,000 --> 00:30:19,000 but what I also did was I set this tag. 566 00:30:19,000 --> 00:30:23,000 I said that this has a tag of 1, and that's the top left. 567 00:30:23,000 --> 00:30:26,000 >> Now, if I click another button, like this second one here, 568 00:30:26,000 --> 00:30:29,000 now you'll see that my tag is 2. 569 00:30:29,000 --> 00:30:32,000 Each of these buttons just has a unique tag, 570 00:30:32,000 --> 00:30:35,000 and so this is later how I'm going to start interacting 571 00:30:35,000 --> 00:30:38,000 with my app. 572 00:30:38,000 --> 00:30:40,000 This is all inside of one view controller, 573 00:30:40,000 --> 00:30:42,000 but here is what we have. 574 00:30:42,000 --> 00:30:44,000 We have 3 properties here. 575 00:30:44,000 --> 00:30:49,000 The first one and last one are going to represent the state of my board. 576 00:30:49,000 --> 00:30:53,000 Basically this first one is an array representing where the Xs and Os are. 577 00:30:53,000 --> 00:30:57,000 This other one here tells us whose turn it is. 578 00:30:57,000 --> 00:31:01,000 You'll notice that I also have these things here. 579 00:31:01,000 --> 00:31:05,000 Before when we declared properties we gave them a name and a type. 580 00:31:05,000 --> 00:31:08,000 We can also give them some additional information here. 581 00:31:08,000 --> 00:31:11,000 This first says nonatomic, and what this says 582 00:31:11,000 --> 00:31:16,000 is basically only one thing will ever be trying to access this variable at a time. 583 00:31:16,000 --> 00:31:19,000 You could do more complex applications that are multi-threaded, 584 00:31:19,000 --> 00:31:22,000 and so back in Scratch we had different threads, 585 00:31:22,000 --> 00:31:25,000 and different sprites could be doing different things at the same time. 586 00:31:25,000 --> 00:31:29,000 >> If that's not the case, which it is not in anything that we'll be looking at, 587 00:31:29,000 --> 00:31:33,000 if we say nonatomic it's actually going to make things a little bit faster. 588 00:31:33,000 --> 00:31:37,000 We also have this thing called assign, strong, or weak. 589 00:31:37,000 --> 00:31:40,000 This assign just says that this is a standard type. 590 00:31:40,000 --> 00:31:43,000 This is not an object or a pointer because this is just a bool, 591 00:31:43,000 --> 00:31:46,000 so bool is built into Objective-C. 592 00:31:46,000 --> 00:31:49,000 This says don't try to do anything fancy with pointers here. 593 00:31:49,000 --> 00:31:51,000 It's a regular old scaler. 594 00:31:51,000 --> 00:31:54,000 Strong and weak, this weak says that actually 595 00:31:54,000 --> 00:31:57,000 I want this to be pointing to something in the view controller. 596 00:31:57,000 --> 00:32:00,000 I'm not going to actually allocate or init this myself. 597 00:32:00,000 --> 00:32:04,000 The interface builder, when I run the app, is going to handle all that initialization. 598 00:32:04,000 --> 00:32:07,000 If I say weak, that says someone else is going to be creating this. 599 00:32:07,000 --> 00:32:09,000 If I say strong, this says that I'm going to be the one 600 00:32:09,000 --> 00:32:12,000 that's creating this board object, 601 00:32:12,000 --> 00:32:14,000 and so here I have some more methods here, 602 00:32:14,000 --> 00:32:18,000 for example, an action for when the new game button is pressed, 603 00:32:18,000 --> 00:32:20,000 an action for when any of the other buttons are pressed, 604 00:32:20,000 --> 00:32:23,000 and et cetera. 605 00:32:23,000 --> 00:32:26,000 >> We won't get into too much of the logic of Tic Tac Toe, 606 00:32:26,000 --> 00:32:30,000 although it's very exciting, but let's take a look at 607 00:32:30,000 --> 00:32:33,000 some of the things that we can do inside of iOS. 608 00:32:33,000 --> 00:32:35,000 This new game method is going to be fired 609 00:32:35,000 --> 00:32:37,000 whenever I press the new game button. 610 00:32:37,000 --> 00:32:41,000 To hook that up I simply come over to my storyboard. 611 00:32:41,000 --> 00:32:43,000 I clicked on new game. 612 00:32:43,000 --> 00:32:47,000 If I come over here to the right I can see that 613 00:32:47,000 --> 00:32:50,000 Touch Up Inside is wired to the newGame method. 614 00:32:50,000 --> 00:32:53,000 That's why this is going to get fired. 615 00:32:53,000 --> 00:32:56,000 The newGame method is going to do some set up. 616 00:32:56,000 --> 00:32:59,000 It's going to say I want you to clear the state of the board. 617 00:32:59,000 --> 00:33:01,000 This is a nice method on mutable arrays. 618 00:33:01,000 --> 00:33:03,000 This is going to say it's now X's turn, 619 00:33:03,000 --> 00:33:07,000 and now I'm going to take advantage of this viewWithTag thing. 620 00:33:07,000 --> 00:33:11,000 >> I know that my buttons have the tags 1-9, 621 00:33:11,000 --> 00:33:13,000 and that's something I arbitrarily picked. 622 00:33:13,000 --> 00:33:15,000 If I want to set the text of each button to be empty 623 00:33:15,000 --> 00:33:17,000 because I just started a new game and I don't want any 624 00:33:17,000 --> 00:33:20,000 Xs or Os to be left over I can do this. 625 00:33:20,000 --> 00:33:24,000 I can say I want the view with the tag, 1, 2, 3, 4 et cetera. 626 00:33:24,000 --> 00:33:27,000 This will pull a different button each time. 627 00:33:27,000 --> 00:33:30,000 Here I'm going to cast it to UIButton. 628 00:33:30,000 --> 00:33:33,000 Just like we could cast ints to floats and vice versa 629 00:33:33,000 --> 00:33:37,000 this says that I want to cast this to a UIButton. 630 00:33:37,000 --> 00:33:40,000 That means the type of this will be a UIButton* 631 00:33:40,000 --> 00:33:43,000 because of pointers, but don't worry, they're not scary anymore. 632 00:33:43,000 --> 00:33:47,000 >> Once I have this button I'm going to call a method on it. 633 00:33:47,000 --> 00:33:50,000 This method is called setTitle forState, and so this says 634 00:33:50,000 --> 00:33:53,000 I want to set the text of the button to be the empty string, 635 00:33:53,000 --> 00:33:57,000 and I want it to be the empty string when it's not pressed. 636 00:33:57,000 --> 00:34:01,000 If I'm using this method, I can change the text of the button 637 00:34:01,000 --> 00:34:04,000 as soon as someone hits it, but we want to say when the button is just sitting there 638 00:34:04,000 --> 00:34:07,000 I want the text to be blank. 639 00:34:07,000 --> 00:34:10,000 Finally, we're going to initialize my board, 640 00:34:10,000 --> 00:34:12,000 and I'm going to say that everything is currently at 0, 641 00:34:12,000 --> 00:34:15,000 so this board members immutable is a mutable array, 642 00:34:15,000 --> 00:34:21,000 which means I can call the addObject method and just a 0 inside of it. 643 00:34:21,000 --> 00:34:23,000 That's what happens when I create a new game. 644 00:34:23,000 --> 00:34:25,000 >> Let's take a look at another one. 645 00:34:25,000 --> 00:34:28,000 This method here is the IBAction that's going to be pressed 646 00:34:28,000 --> 00:34:31,000 every time one of those squares is pressed. 647 00:34:31,000 --> 00:34:33,000 Now we have some Tic Tac Toe logic here. 648 00:34:33,000 --> 00:34:36,000 We figure out whose turn it is, 649 00:34:36,000 --> 00:34:39,000 and based on that we either set an X or an O, 650 00:34:39,000 --> 00:34:43,000 but we notice that we're reusing this same event handler 651 00:34:43,000 --> 00:34:45,000 for every single one of those buttons. 652 00:34:45,000 --> 00:34:49,000 That means that I don't have a method for the top left button, 653 00:34:49,000 --> 00:34:52,000 a different method for the bottom right button, although I could have done that. 654 00:34:52,000 --> 00:34:54,000 That wouldn't really be good design. 655 00:34:54,000 --> 00:34:57,000 What I'm doing here is I'm actually going to determine 656 00:34:57,000 --> 00:35:00,000 what the tag of the button that was pressed is. 657 00:35:00,000 --> 00:35:04,000 You notice that this play method takes one argument. 658 00:35:04,000 --> 00:35:07,000 It's called sender, and what sender is is sender is going to 659 00:35:07,000 --> 00:35:10,000 represent exactly what was taken action upon. 660 00:35:10,000 --> 00:35:15,000 If I press a button, this sender is going to be that UIButton 661 00:35:15,000 --> 00:35:18,000 that I actually pressed, so that means that that UIButton 662 00:35:18,000 --> 00:35:20,000 has a tag because I created a tag. 663 00:35:20,000 --> 00:35:23,000 >> If I want to get at the tag I can simply say 664 00:35:23,000 --> 00:35:26,000 I want the tag of the sender, 665 00:35:26,000 --> 00:35:28,000 and again, I've just casted it to a UIButton. 666 00:35:28,000 --> 00:35:32,000 I happen to know that the sender will be a UIButton. 667 00:35:32,000 --> 00:35:34,000 It doesn't always have to be a UIButton. 668 00:35:34,000 --> 00:35:36,000 I could, for example, register the same event handler 669 00:35:36,000 --> 00:35:38,000 for one for a button, one for a slider. 670 00:35:38,000 --> 00:35:40,000 In this case, I know they're all buttons, so I'm going to say 671 00:35:40,000 --> 00:35:43,000 I want this to be a button, and then I can get the tag, 672 00:35:43,000 --> 00:35:48,000 and from the tag I now know where I am inside of the board. 673 00:35:48,000 --> 00:35:51,000 Then I can simply set either the X or the O, can flip the turn, 674 00:35:51,000 --> 00:35:54,000 check who has won, et cetera. 675 00:35:54,000 --> 00:35:59,000 >> Any questions on this so far? 676 00:35:59,000 --> 00:36:02,000 Okay, all the code we posted online—we don't want to get into too much 677 00:36:02,000 --> 00:36:06,000 of the Tic Tac Toe logic, but now you can see that really 678 00:36:06,000 --> 00:36:09,000 all we're doing is we're looping over this array, 679 00:36:09,000 --> 00:36:13,000 so we have a couple of for loops here, and we're just comparing to see 680 00:36:13,000 --> 00:36:18,000 do we have a match in all the rows, a match in a column or anything like that. 681 00:36:18,000 --> 00:36:21,000 To actually run this app, if I tap on one of these buttons 682 00:36:21,000 --> 00:36:24,000 that play method was fired, so that means I just set 683 00:36:24,000 --> 00:36:31,000 the button to be an X, so now this button will be an O, and so on, 684 00:36:31,000 --> 00:36:35,000 and so that's how we're starting to interact with this single page app. 685 00:36:35,000 --> 00:36:38,000 >> We'll post the code, so feel free to peruse that, 686 00:36:38,000 --> 00:36:43,000 but let's now talk about some apps that are more than just one page. 687 00:36:43,000 --> 00:36:47,000 As exciting as Tic Tac Toe was, a lot of apps inside of iOS 688 00:36:47,000 --> 00:36:50,000 are kind of these drill down things with multiple screens. 689 00:36:50,000 --> 00:36:54,000 The first concept that we'll need to talk about are protocols, 690 00:36:54,000 --> 00:36:57,000 and a protocol is simply a set of methods 691 00:36:57,000 --> 00:36:59,000 that you can promise to define. 692 00:36:59,000 --> 00:37:02,000 If I create this new protocol with 2 methods, this first one, 693 00:37:02,000 --> 00:37:05,000 if the return type is void, I called it foo. 694 00:37:05,000 --> 00:37:07,000 It takes no arguments. I have another method. 695 00:37:07,000 --> 00:37:11,000 It returns an int. I called it bar, and it takes one argument. 696 00:37:11,000 --> 00:37:14,000 All this protocol is that's called SomeProtocol up here, 697 00:37:14,000 --> 00:37:19,000 this is a set of things that someone can implement. 698 00:37:19,000 --> 00:37:22,000 I haven't inside of this protocol said what foo does. 699 00:37:22,000 --> 00:37:26,000 Instead, I'm just saying you could define foo if you want to. 700 00:37:26,000 --> 00:37:30,000 If I'm creating a view controller or creating a class 701 00:37:30,000 --> 00:37:33,000 I can inside of that class promise to implement 702 00:37:33,000 --> 00:37:40,000 some of these methods, so for example, if say 703 00:37:40,000 --> 00:37:43,000 this now says I'm making a promise to you that inside 704 00:37:43,000 --> 00:37:50,000 of this view controller class I will have definitions for both foo and bar. 705 00:37:50,000 --> 00:37:52,000 >> Why is that useful? 706 00:37:52,000 --> 00:37:55,000 A lot of components inside of iOS take advantage of this 707 00:37:55,000 --> 00:37:58,000 design pattern called delegation, and what delegation says 708 00:37:58,000 --> 00:38:01,000 is that, for example, if I have a text box 709 00:38:01,000 --> 00:38:04,000 and there are some events that could be registered inside of my text box, 710 00:38:04,000 --> 00:38:07,000 rather than creating separate events what I can do is I can say 711 00:38:07,000 --> 00:38:10,000 the delegate of this text box will be some object. 712 00:38:10,000 --> 00:38:13,000 When I say that this is a delegate now that means that 713 00:38:13,000 --> 00:38:16,000 whenever some event would have been fired in the text box 714 00:38:16,000 --> 00:38:18,000 rather than having to register it or anything like that 715 00:38:18,000 --> 00:38:21,000 it's just going to call a method on the delegate. 716 00:38:21,000 --> 00:38:24,000 For example, inside of my text box I have a method for when I press 717 00:38:24,000 --> 00:38:27,000 that done button in the bottom right, 718 00:38:27,000 --> 00:38:30,000 and so rather than registering event handler what I can say is 719 00:38:30,000 --> 00:38:34,000 text box, here is an object that I want you to call a method on 720 00:38:34,000 --> 00:38:37,000 every time someone presses the done button, 721 00:38:37,000 --> 00:38:40,000 and that means that that object has to implement some protocol 722 00:38:40,000 --> 00:38:45,000 that says I promise to define that done button action, 723 00:38:45,000 --> 00:38:47,000 because if it doesn't define that method and you press done, 724 00:38:47,000 --> 00:38:49,000 then it's going to be confusing. 725 00:38:49,000 --> 00:38:55,000 >> Let's take a look at an example. 726 00:38:55,000 --> 00:38:58,000 Here I simply have one text box, 727 00:38:58,000 --> 00:39:04,000 and one of the properties of this text box over here on this right side is the delegate. 728 00:39:04,000 --> 00:39:06,000 This is a property of the class. 729 00:39:06,000 --> 00:39:09,000 What I did here is I control clicked, and I dragged from this spot over here 730 00:39:09,000 --> 00:39:13,000 to the view controller, and that says now the delegate of this text box 731 00:39:13,000 --> 00:39:18,000 is going to be the view controller. 732 00:39:18,000 --> 00:39:20,000 That means that when some actions happen, rather than registering 733 00:39:20,000 --> 00:39:25,000 separate event handlers I want you to send them to the delegate. 734 00:39:25,000 --> 00:39:28,000 Now let's take a look at my view controller. 735 00:39:28,000 --> 00:39:32,000 Inside of the .h file I've made a promise. 736 00:39:32,000 --> 00:39:36,000 I've promised to implement some methods inside of this protocol 737 00:39:36,000 --> 00:39:38,000 called UITextFieldDelegate, and again, 738 00:39:38,000 --> 00:39:42,000 this is just some list of some things that I can choose to implement. 739 00:39:42,000 --> 00:39:46,000 >> If I come here in my .m file, I have implemented one such method. 740 00:39:46,000 --> 00:39:49,000 I've called it textFieldShouldReturn 741 00:39:49,000 --> 00:39:52,000 because that's what it was called inside of the protocol. 742 00:39:52,000 --> 00:39:57,000 And now whenever I press the done button inside of that text field 743 00:39:57,000 --> 00:40:00,000 this is what's going to get called, so I did not register an event handler. 744 00:40:00,000 --> 00:40:03,000 I connected the delegate, and whenever this event is fired 745 00:40:03,000 --> 00:40:08,000 this is the method that will get called, so if I come over here to my storyboard and run it— 746 00:40:08,000 --> 00:40:11,000 while that's loading we can see what this does. 747 00:40:11,000 --> 00:40:13,000 On my screen I have 2 things. 748 00:40:13,000 --> 00:40:16,000 I have this text field, and I have this label. 749 00:40:16,000 --> 00:40:19,000 I'm simply saying I want the text of this label 750 00:40:19,000 --> 00:40:23,000 to be equal to whatever the user typed in inside of the text field. 751 00:40:23,000 --> 00:40:26,000 This next line here is simply a method that I'm calling 752 00:40:26,000 --> 00:40:29,000 on the text field that says I want you to hide the keyboard. 753 00:40:29,000 --> 00:40:33,000 This is just the arbitrary method that Apple chose. 754 00:40:33,000 --> 00:40:38,000 >> Again, before I did anything I had to wire everything up, so I first came over here. 755 00:40:38,000 --> 00:40:42,000 From the view controller I drag over to the text box. 756 00:40:42,000 --> 00:40:46,000 I let go, and I can see here that I can make this the text field property 757 00:40:46,000 --> 00:40:49,000 since over here in the view controller I've defined a property 758 00:40:49,000 --> 00:40:52,000 that is an IBOutlet of a text field. 759 00:40:52,000 --> 00:40:55,000 This says that I can wire this property up 760 00:40:55,000 --> 00:40:59,000 to a text field in my UI. 761 00:40:59,000 --> 00:41:03,000 Now when I click this I can start typing. 762 00:41:03,000 --> 00:41:06,000 Now if I click the done button this is going to fire 763 00:41:06,000 --> 00:41:08,000 an event that I can now respond to. 764 00:41:08,000 --> 00:41:10,000 No event handlers. 765 00:41:10,000 --> 00:41:13,000 That's how I just responded to that done button. 766 00:41:13,000 --> 00:41:15,000 Make sense? 767 00:41:15,000 --> 00:41:20,000 >> This isn't a design pattern that—you might not ever find yourself 768 00:41:20,000 --> 00:41:23,000 creating your own protocol, but just know that some 769 00:41:23,000 --> 00:41:27,000 different iOS components register events in different ways. 770 00:41:27,000 --> 00:41:29,000 Buttons, for example, use those IBActions. 771 00:41:29,000 --> 00:41:32,000 Text fields, on the other hand, are going to use delegates. 772 00:41:32,000 --> 00:41:36,000 We can see and you can look all of that up inside of the documentation. 773 00:41:36,000 --> 00:41:41,000 By the way, there's actually a ton of UI stuff built into iOS for you, 774 00:41:41,000 --> 00:41:46,000 so for example, the way I made that say done at the bottom right 775 00:41:46,000 --> 00:41:48,000 is I selected this text field. 776 00:41:48,000 --> 00:41:50,000 I came over here. 777 00:41:50,000 --> 00:41:53,000 I scrolled down a bit to return key, 778 00:41:53,000 --> 00:41:56,000 and I can actually make this a whole bunch of things, like if I want that to say 779 00:41:56,000 --> 00:42:00,000 emergency call instead I can do that, which is totally random, 780 00:42:00,000 --> 00:42:02,000 and I don't know why there's a built-in emergency call button, 781 00:42:02,000 --> 00:42:06,000 but there, it says emergency call in really small letters. 782 00:42:06,000 --> 00:42:08,000 There you go. 783 00:42:08,000 --> 00:42:12,000 >> Definitely explore all of these different options in iOS. 784 00:42:12,000 --> 00:42:14,000 Any questions on delegates? 785 00:42:14,000 --> 00:42:18,000 Again, just an interesting design pattern that you should be aware of. 786 00:42:18,000 --> 00:42:22,000 Okay, let's next take a look at table views. 787 00:42:22,000 --> 00:42:26,000 A table view is basically that list of items that is all over the place in iOS. 788 00:42:26,000 --> 00:42:29,000 When you're flipping through all of your contacts, you're looking at 789 00:42:29,000 --> 00:42:34,000 the setting page, and that kind of list of things is called a table view. 790 00:42:34,000 --> 00:42:37,000 Implementing a table view in iOS is pretty simple. 791 00:42:37,000 --> 00:42:41,000 Instead of making a class that descends from that UIViewController 792 00:42:41,000 --> 00:42:44,000 like we've done before we simply need to say rather than 793 00:42:44,000 --> 00:42:46,000 everything a UIViewController does I want to do, 794 00:42:46,000 --> 00:42:50,000 I say everything a UITableViewController does I want to do, 795 00:42:50,000 --> 00:42:54,000 so that simply adds some additional things that are totally done for us. 796 00:42:54,000 --> 00:42:58,000 We need to do very little to basically fill in the blanks inside of the table. 797 00:42:58,000 --> 00:43:02,000 >> In order to display a table I need to answer some questions. 798 00:43:02,000 --> 00:43:06,000 The first question I need to answer is how many sections are in the table? 799 00:43:06,000 --> 00:43:08,000 When you're flipping through your contacts app you'll notice that it's kind of 800 00:43:08,000 --> 00:43:12,000 organized by the As, then you have the Bs, and you have that little sub header. 801 00:43:12,000 --> 00:43:14,000 Each of those is called a section. 802 00:43:14,000 --> 00:43:16,000 You may or may not need these. 803 00:43:16,000 --> 00:43:19,000 The first thing you need to do is implement a method 804 00:43:19,000 --> 00:43:22,000 called tableView:numberOfSectionsInTableView. 805 00:43:22,000 --> 00:43:25,000 That simply returns how many sections you have, 806 00:43:25,000 --> 00:43:29,000 so this could say return one if you have one big table view. 807 00:43:29,000 --> 00:43:33,000 The next question that iOS needs to know is how many rows do you have? 808 00:43:33,000 --> 00:43:36,000 For example, you're flipping through a table view. 809 00:43:36,000 --> 00:43:39,000 You have a fixed number of songs you're looking at or a fixed number of contacts. 810 00:43:39,000 --> 00:43:41,000 If you're me, of course, not that many, 811 00:43:41,000 --> 00:43:44,000 and so that's how iOS knows how many cells to display. 812 00:43:44,000 --> 00:43:46,000 >> Again, this could say something like return 3. 813 00:43:46,000 --> 00:43:49,000 My table view has 3 rows. 814 00:43:49,000 --> 00:43:52,000 Finally, iOS needs to know what each cell looks like, 815 00:43:52,000 --> 00:43:54,000 so what it's actually going to do is call this method down here, 816 00:43:54,000 --> 00:43:57,000 this tableView:cellForRowAtIndexPath. 817 00:43:57,000 --> 00:44:01,000 It's going to call this method on every single cell inside of your table. 818 00:44:01,000 --> 00:44:03,000 How does it know how many times to call it? 819 00:44:03,000 --> 00:44:06,000 Well, you told it inside of number of rows in section. 820 00:44:06,000 --> 00:44:08,000 We're going to call this on each of our cells, 821 00:44:08,000 --> 00:44:11,000 and inside of this is where you can actually do things like 822 00:44:11,000 --> 00:44:16,000 set the text or tell you what that little blue button on the right side does. 823 00:44:16,000 --> 00:44:19,000 The pattern for getting these cells, we're going to use this method 824 00:44:19,000 --> 00:44:22,000 called dequeueReusableCellWithIdentifier. 825 00:44:22,000 --> 00:44:29,000 >> Objective-C is actually very well known for the ridiculous length of their method names, 826 00:44:29,000 --> 00:44:32,000 and this is really a nice case in point example. 827 00:44:32,000 --> 00:44:37,000 What this method does is this just says I want you to give me a cell. 828 00:44:37,000 --> 00:44:39,000 Just an iOS thing. 829 00:44:39,000 --> 00:44:41,000 If you have something like 100,000 songs on your iPod 830 00:44:41,000 --> 00:44:45,000 what iOS doesn't want to do is allocate 100,000 cells, 831 00:44:45,000 --> 00:44:48,000 because if you're at the top of your list, do you really need to allocate memory 832 00:44:48,000 --> 00:44:51,000 for the cell that's 99,000 rows down? 833 00:44:51,000 --> 00:44:55,000 No, because as you're scrolling you can kind of allocate as you go along. 834 00:44:55,000 --> 00:44:57,000 This is done for you. 835 00:44:57,000 --> 00:44:59,000 You don't have to worry about all that stupid performance stuff. 836 00:44:59,000 --> 00:45:02,000 All you say is you call this method dequeueReusableCellWithIdentifier, 837 00:45:02,000 --> 00:45:06,000 and this says, okay, if you need to I'm going to create a new cell for you. 838 00:45:06,000 --> 00:45:09,000 >> But if you're at the bottom of the table and you've already allocated some cells 839 00:45:09,000 --> 00:45:12,000 at the top of the table that you're really not going to need anytime soon 840 00:45:12,000 --> 00:45:15,000 I'm going to give you one of those back instead of allocating a new one, 841 00:45:15,000 --> 00:45:17,000 and so this is a nice performance concern. 842 00:45:17,000 --> 00:45:21,000 You do not have to allocate the cells yourself. 843 00:45:21,000 --> 00:45:23,000 That's going to give you back a cell. 844 00:45:23,000 --> 00:45:25,000 It's going to return to you a cell object. 845 00:45:25,000 --> 00:45:28,000 Once you have the cell object you can do stuff to it. 846 00:45:28,000 --> 00:45:32,000 You can set the text of the cell with this property called text label. 847 00:45:32,000 --> 00:45:36,000 You can add that arrow on the right or some other random stuff 848 00:45:36,000 --> 00:45:41,000 with this other property called accessoryType, and so on and so on. 849 00:45:41,000 --> 00:45:46,000 >> Let's take a look at actually implementing a table view now. 850 00:45:46,000 --> 00:45:49,000 When I created this project 851 00:45:49,000 --> 00:45:53,000 rather than saying single view application I actually came over here 852 00:45:53,000 --> 00:45:57,000 to master-detail application, and so basically this corresponds to the mail app 853 00:45:57,000 --> 00:46:01,000 on the iPad with the table view on the left and then the contents on the right. 854 00:46:01,000 --> 00:46:07,000 On the iPod or iPhone this is going to correspond to a single table view. 855 00:46:07,000 --> 00:46:10,000 That's where I got my starter code. 856 00:46:10,000 --> 00:46:13,000 >> Let's first take a look at the storyboard. 857 00:46:13,000 --> 00:46:15,000 All of this was done for me, basically created. 858 00:46:15,000 --> 00:46:18,000 This navigation bar showed me what an example cell could look like, 859 00:46:18,000 --> 00:46:22,000 and I can double click this, change the title. 860 00:46:22,000 --> 00:46:25,000 Any other UI concerns I can handle there. 861 00:46:25,000 --> 00:46:27,000 The header file looks really simple. 862 00:46:27,000 --> 00:46:30,000 Rather than saying this is UIViewController we're now saying this is a 863 00:46:30,000 --> 00:46:35,000 TableViewController, so we know that we want to call all those table methods. 864 00:46:35,000 --> 00:46:38,000 Next I want to create a property that's going to represent 865 00:46:38,000 --> 00:46:40,000 the things inside of my table. 866 00:46:40,000 --> 00:46:43,000 This table is going to arbitrarily display 867 00:46:43,000 --> 00:46:46,000 a list of fruit, and so I need to create some array 868 00:46:46,000 --> 00:46:49,000 in which I can insert fruit. 869 00:46:49,000 --> 00:46:52,000 Inside of my implementation file the first thing I want to do 870 00:46:52,000 --> 00:46:55,000 is I want to make sure I initialize this array. 871 00:46:55,000 --> 00:46:58,000 >> I said alloc init, created my fruit array, 872 00:46:58,000 --> 00:47:03,000 and I'm adding 4 things to it, one of which is much more controversial than the other 3. 873 00:47:03,000 --> 00:47:06,000 And now I have an array of size 4. 874 00:47:06,000 --> 00:47:08,000 We're shaking it up in CS50. 875 00:47:08,000 --> 00:47:11,000 I now have an array of size 4. 876 00:47:11,000 --> 00:47:13,000 Now I'm going to start answering these questions, and actually, 877 00:47:13,000 --> 00:47:16,000 when I created this app all of this was already done for me. 878 00:47:16,000 --> 00:47:19,000 I didn't have to type out the number of sections in table view. 879 00:47:19,000 --> 00:47:22,000 It was already there, and I'm filling in the blanks. 880 00:47:22,000 --> 00:47:24,000 How many sections do I have? 881 00:47:24,000 --> 00:47:26,000 One. All done. 882 00:47:26,000 --> 00:47:28,000 How many rows do I have? 883 00:47:28,000 --> 00:47:31,000 Well, I have one row for every fruit, so this count 884 00:47:31,000 --> 00:47:34,000 is a property of any array that says how big is it? 885 00:47:34,000 --> 00:47:36,000 That's how many rows I have. 886 00:47:36,000 --> 00:47:42,000 Finally, I need to say what does each cell look like? 887 00:47:42,000 --> 00:47:46,000 I'm going to say dequeueReusableCellWithIdentifier. 888 00:47:46,000 --> 00:47:48,000 >> Again, this was already written for me. 889 00:47:48,000 --> 00:47:51,000 I didn't have to do this myself, and I want to get back 890 00:47:51,000 --> 00:47:54,000 this cell at this location. 891 00:47:54,000 --> 00:47:57,000 Remember that we're calling this same method on every single cell, 892 00:47:57,000 --> 00:48:01,000 and this argument here, this indexPath argument, 893 00:48:01,000 --> 00:48:03,000 says what row I'm in. 894 00:48:03,000 --> 00:48:05,000 If I say indexPath.row down here 895 00:48:05,000 --> 00:48:09,000 this will be 0, then it will be 1, then it will be 2, and this is so I know 896 00:48:09,000 --> 00:48:11,000 what cell I'm currently displaying. 897 00:48:11,000 --> 00:48:15,000 I want to set the text of the cell using this textLabel property 898 00:48:15,000 --> 00:48:19,000 to go inside my fruit array and get 899 00:48:19,000 --> 00:48:21,000 the object corresponding to each row. 900 00:48:21,000 --> 00:48:24,000 If this is a string, I'm now setting 901 00:48:24,000 --> 00:48:28,000 the text property to a string. 902 00:48:28,000 --> 00:48:30,000 I can do one other thing. 903 00:48:30,000 --> 00:48:32,000 I can also register an event handler on each of the cells, 904 00:48:32,000 --> 00:48:35,000 so when I tap each of these cells 905 00:48:35,000 --> 00:48:39,000 this didSelectRowAtIndexPath, this is going to be called for me, 906 00:48:39,000 --> 00:48:42,000 so simply by defining this I can now handle what happens 907 00:48:42,000 --> 00:48:46,000 when you tap a cell, and again, we're passing in which cell was tapped 908 00:48:46,000 --> 00:48:50,000 so that we can reuse this same event handler for all of our cells. 909 00:48:50,000 --> 00:48:53,000 >> Again, this is something iOS is doing for me. 910 00:48:53,000 --> 00:48:55,000 Let's display another annoying popup 911 00:48:55,000 --> 00:48:59,000 that simply says you picked something where that something 912 00:48:59,000 --> 00:49:04,000 is going to be the row object. 913 00:49:04,000 --> 00:49:10,000 When I run this, I'm going to have this nice table view 914 00:49:10,000 --> 00:49:14,000 with one row for each of these fruit, and if I tap one 915 00:49:14,000 --> 00:49:16,000 it tells me what happened. 916 00:49:16,000 --> 00:49:21,000 Make sense? 917 00:49:21,000 --> 00:49:24,000 Let's build a little bit more complex of an app, 918 00:49:24,000 --> 00:49:28,000 as much as clicking you picked tomato is. 919 00:49:28,000 --> 00:49:31,000 The nice part about the storyboarding 920 00:49:31,000 --> 00:49:35,000 is it's not only going to help us design screens individually, 921 00:49:35,000 --> 00:49:38,000 it's also going to help us tie together our entire app, 922 00:49:38,000 --> 00:49:42,000 so the final app we'll be building is this nice sports news reader, 923 00:49:42,000 --> 00:49:45,000 and so it's going to have multiple screens, and so I can actually represent 924 00:49:45,000 --> 00:49:48,000 each of these multiple screens on the storyboard, 925 00:49:48,000 --> 00:49:52,000 and I can zoom out and see my app from a high level. 926 00:49:52,000 --> 00:49:55,000 >> In order to create a new element inside of my storyboard 927 00:49:55,000 --> 00:49:59,000 it's really simple inside of Interface Builder. 928 00:49:59,000 --> 00:50:01,000 If I want to add another screen to this, for example, 929 00:50:01,000 --> 00:50:06,000 I can first zoom out with the pinch zoom that Apple loves so much, 930 00:50:06,000 --> 00:50:09,000 and down here before I search for a button 931 00:50:09,000 --> 00:50:12,000 and I drag and drop a button 932 00:50:12,000 --> 00:50:15,000 if I want to create a new screen I can actually just drag and drop 933 00:50:15,000 --> 00:50:19,000 an entire view controller, so if I pick this, pull it over here, 934 00:50:19,000 --> 00:50:23,000 hey, there's another screen, and so now using this same storyboard file 935 00:50:23,000 --> 00:50:26,000 I can have all of the screens inside of my app, and I can zoom out 936 00:50:26,000 --> 00:50:28,000 and see how they interact. 937 00:50:28,000 --> 00:50:32,000 These won't interact yet. 938 00:50:32,000 --> 00:50:36,000 The way in which these 2 screens interact is you define relationships. 939 00:50:36,000 --> 00:50:39,000 You can basically say this screen, when you press this button, 940 00:50:39,000 --> 00:50:42,000 I want you to slide over to this new screen. 941 00:50:42,000 --> 00:50:44,000 That means there's this kind of relationship between 942 00:50:44,000 --> 00:50:46,000 the first screen and the second screen. 943 00:50:46,000 --> 00:50:49,000 You'll have basically an arrow from that button to the second screen 944 00:50:49,000 --> 00:50:53,000 saying that when you press this button that's where I want to go. 945 00:50:53,000 --> 00:50:57,000 Just like we control clicked and dragged to define those outlets 946 00:50:57,000 --> 00:51:01,000 we're going to do the same thing to define these segues. 947 00:51:01,000 --> 00:51:05,000 >> We'll see an example for that, and before we actually transition 948 00:51:05,000 --> 00:51:08,000 from one screen to another iOS is nice enough to call this method 949 00:51:08,000 --> 00:51:11,000 called prepareForSegue, and this is where we can start 950 00:51:11,000 --> 00:51:14,000 sending data from one app to another. 951 00:51:14,000 --> 00:51:17,000 In the example we're about to look at it will basically allow us 952 00:51:17,000 --> 00:51:20,000 to filter baseball teams by leagues and divisions. 953 00:51:20,000 --> 00:51:23,000 When I select a league, for example, I want to transition 954 00:51:23,000 --> 00:51:25,000 to my next screen where I can display all of the divisions 955 00:51:25,000 --> 00:51:27,000 in that league or all the different teams. 956 00:51:27,000 --> 00:51:31,000 I need to send to that screen what teams you should display. 957 00:51:31,000 --> 00:51:35,000 To do that I'm going to take advantage of this method here. 958 00:51:35,000 --> 00:51:39,000 >> Finally, just a random point on iOS. 959 00:51:39,000 --> 00:51:41,000 If you want to store data there's this thing called core data, 960 00:51:41,000 --> 00:51:44,000 which is actually kind of complicated to work with. 961 00:51:44,000 --> 00:51:47,000 You can also use SQL to store data, 962 00:51:47,000 --> 00:51:51,000 which, again, is nice but kind of on the more complicated side to work with, 963 00:51:51,000 --> 00:51:55,000 but iOS also supports these really cool things called property lists, 964 00:51:55,000 --> 00:52:00,000 and a property list is just a file that represents key value pairs. 965 00:52:00,000 --> 00:52:03,000 You define a list of keys, and you say this key is going to be an array. 966 00:52:03,000 --> 00:52:06,000 This key is going to be a string, and basically anything you can do 967 00:52:06,000 --> 00:52:10,000 in JSON you can do inside of a property list, 968 00:52:10,000 --> 00:52:14,000 and so this really nicely allows us to work with some data. 969 00:52:14,000 --> 00:52:18,000 For example, I have this Teams.plist that I created. 970 00:52:18,000 --> 00:52:22,000 I created a new plist file, and I can drill down. 971 00:52:22,000 --> 00:52:26,000 This is a dictionary, this is a dictionary, these are strings, 972 00:52:26,000 --> 00:52:30,000 and so this is a nice graphical representation of a JSON document 973 00:52:30,000 --> 00:52:33,000 or just a set of key value pairs, 974 00:52:33,000 --> 00:52:37,000 and so this is the data that I'll be working with inside of my app. 975 00:52:37,000 --> 00:52:40,000 >> Let's first come over here. We have a lot more files now. 976 00:52:40,000 --> 00:52:44,000 But let's first come over here to the storyboard. 977 00:52:44,000 --> 00:52:48,000 The storyboard here—if I can zoom out— 978 00:52:48,000 --> 00:52:51,000 we can now see that this is the flow of my app. 979 00:52:51,000 --> 00:52:53,000 I'm first going to start on this screen. 980 00:52:53,000 --> 00:52:55,000 I'm going to drill down to this screen, 981 00:52:55,000 --> 00:52:58,000 and I'm going to drill down to this screen, and we can see here that if I kind of 982 00:52:58,000 --> 00:53:04,000 move one of these around we have these arrows going from here to here, 983 00:53:04,000 --> 00:53:08,000 and the way I define that arrow was if I zoom in a little bit, 984 00:53:08,000 --> 00:53:12,000 and if I come over to this view controller, 985 00:53:12,000 --> 00:53:16,000 and here's a cell, and I want to say that when you tap a cell 986 00:53:16,000 --> 00:53:18,000 I want you to slide over to another screen. 987 00:53:18,000 --> 00:53:21,000 I can simply hold down control, 988 00:53:21,000 --> 00:53:26,000 scroll over a little bit, hold down control, drag this cell over and let go. 989 00:53:26,000 --> 00:53:30,000 >> And over here we say what's the transition that you want to use? 990 00:53:30,000 --> 00:53:32,000 Do you want to use that slide thing that's called push? 991 00:53:32,000 --> 00:53:34,000 Do you want to slide up from the bottom? 992 00:53:34,000 --> 00:53:36,000 That's called modal. 993 00:53:36,000 --> 00:53:40,000 And once I click one of these, it's going to draw this arrow for me, 994 00:53:40,000 --> 00:53:44,000 and that means that I've actually handled what happens when I press this button graphically. 995 00:53:44,000 --> 00:53:48,000 I didn't write any code to actually slide from one screen to the next one. 996 00:53:48,000 --> 00:53:51,000 I defined this visually inside of Interface Builder. 997 00:53:51,000 --> 00:53:55,000 If I click on this arrow, we can see that I gave this thing a name. 998 00:53:55,000 --> 00:53:59,000 I called it showDivisions, and this is so that I can know 999 00:53:59,000 --> 00:54:03,000 what transition is about to occur, and we'll see why in just a sec. 1000 00:54:03,000 --> 00:54:06,000 That's how I've wired up the different screens in my app. 1001 00:54:06,000 --> 00:54:09,000 If this were a button, for example, rather than a table view, 1002 00:54:09,000 --> 00:54:11,000 I could control click on the button, drag over to the next screen, 1003 00:54:11,000 --> 00:54:16,000 and that's how I can do navigation that way. 1004 00:54:16,000 --> 00:54:19,000 >> Really quickly, if we come into the MasterViewController, 1005 00:54:19,000 --> 00:54:22,000 again, we're simply going to define a list of things 1006 00:54:22,000 --> 00:54:26,000 that will be displayed in the table view. 1007 00:54:26,000 --> 00:54:29,000 Here I'm saying I want you to take that plist file, 1008 00:54:29,000 --> 00:54:32,000 and I want you to load it up into a dictionary, 1009 00:54:32,000 --> 00:54:35,000 and once you have that dictionary, I'm going to answer those same questions again. 1010 00:54:35,000 --> 00:54:37,000 Here is the number of sections. 1011 00:54:37,000 --> 00:54:41,000 One, there is one row for every league, and the text of each cell 1012 00:54:41,000 --> 00:54:46,000 should be either the first one, the first league, the second league, and so on. 1013 00:54:46,000 --> 00:54:51,000 Finally, I'm going to use this method that we just saw called prepareForSegue, 1014 00:54:51,000 --> 00:54:54,000 and this is the method that's going to be fired when I click 1015 00:54:54,000 --> 00:54:57,000 on one of those rows and therefore activating that transition 1016 00:54:57,000 --> 00:54:59,000 that I set up with the arrows. 1017 00:54:59,000 --> 00:55:02,000 This is saying that I can have multiple 1018 00:55:02,000 --> 00:55:05,000 relationships from one screen to another. 1019 00:55:05,000 --> 00:55:08,000 If I have 2 buttons and each button takes you to a different screen 1020 00:55:08,000 --> 00:55:10,000 I'm going to have 2 segues, 1 for each button. 1021 00:55:10,000 --> 00:55:14,000 But this prepareForSegue is, again, going to be reused 1022 00:55:14,000 --> 00:55:17,000 for each of the different relationships, so that means that I need a way 1023 00:55:17,000 --> 00:55:21,000 of identifying if you press the first button or you press the second button. 1024 00:55:21,000 --> 00:55:25,000 >> Remember when I gave that segue a name, this showDivisions, 1025 00:55:25,000 --> 00:55:29,000 that's how I now know that this is the segue that was activated. 1026 00:55:29,000 --> 00:55:32,000 All I want to do is I want to say 1027 00:55:32,000 --> 00:55:35,000 I want to figure out what I just hit, 1028 00:55:35,000 --> 00:55:38,000 and so to get that, I can say I want the indexPath for the selected row, 1029 00:55:38,000 --> 00:55:42,000 remember the indexPath just says where I just clicked, 1030 00:55:42,000 --> 00:55:47,000 and then I want to say I want to figure out where I'm going. 1031 00:55:47,000 --> 00:55:50,000 This destinationViewController, that's a property of the segue. 1032 00:55:50,000 --> 00:55:53,000 That's the screen I'm going to, 1033 00:55:53,000 --> 00:55:56,000 so I know that the screen I'm going to is called DivisionsViewController 1034 00:55:56,000 --> 00:55:59,000 because I created that class, 1035 00:55:59,000 --> 00:56:02,000 and so now if I say d.divisions 1036 00:56:02,000 --> 00:56:06,000 I'm now setting a property of the view controller I'm about to go to. 1037 00:56:06,000 --> 00:56:10,000 This is how I'm sending data from one screen to another screen. 1038 00:56:10,000 --> 00:56:13,000 >> Just looking at this DivisionsViewController 1039 00:56:13,000 --> 00:56:16,000 you can see here that in the .h file 1040 00:56:16,000 --> 00:56:20,000 there is that property divisions, and that's what I'm basically populating, 1041 00:56:20,000 --> 00:56:25,000 so that's how I know that I'm displaying the divisions corresponding to 1042 00:56:25,000 --> 00:56:28,000 the league that I clicked, and again, 1043 00:56:28,000 --> 00:56:31,000 the actual table view looks pretty much the same, just answering those 1044 00:56:31,000 --> 00:56:34,000 3 simple questions as well as identifying 1045 00:56:34,000 --> 00:56:37,000 what happens when you move to the next screen. 1046 00:56:37,000 --> 00:56:40,000 Just a couple of other things here. 1047 00:56:40,000 --> 00:56:43,000 You notice at the top here that rather than saying #include 1048 00:56:43,000 --> 00:56:45,000 I'm now saying #import. 1049 00:56:45,000 --> 00:56:47,000 This is just an Objective-C thing. 1050 00:56:47,000 --> 00:56:52,000 The import is basically a nicer version of include, 1051 00:56:52,000 --> 00:56:57,000 and for example, we need to know what this class is, 1052 00:56:57,000 --> 00:57:00,000 so I can't just say DivisionsViewController. 1053 00:57:00,000 --> 00:57:05,000 If we didn't hashtag standardio.c inside of our .c file 1054 00:57:05,000 --> 00:57:07,000 the compiler had no idea what printf was. 1055 00:57:07,000 --> 00:57:12,000 Similarly, if I don't import the DivisionsViewController 1056 00:57:12,000 --> 00:57:16,000 the compiler really has no idea what a DivisionsViewController is. 1057 00:57:16,000 --> 00:57:19,000 Just make sure that inside of your different .m files you make sure to import 1058 00:57:19,000 --> 00:57:25,000 the corresponding .h files so that the compiler knows what's going on. 1059 00:57:25,000 --> 00:57:31,000 >> Finally, what Apple ultimately does is display some data using a web view, 1060 00:57:31,000 --> 00:57:35,000 and so a web view is an object in which you can embed 1061 00:57:35,000 --> 00:57:37,000 a little web browser inside of your app. 1062 00:57:37,000 --> 00:57:40,000 All you need to do is supply a URL to your web browser, 1063 00:57:40,000 --> 00:57:43,000 so I want to go to mlb.mlb.com, 1064 00:57:43,000 --> 00:57:46,000 and this is how I can access the home page for each team, 1065 00:57:46,000 --> 00:57:49,000 and so by passing in this URL 1066 00:57:49,000 --> 00:57:52,000 the web view can display this for me, and I can browse around, 1067 00:57:52,000 --> 00:58:01,000 and simulator is in use by that one. 1068 00:58:01,000 --> 00:58:03,000 Now this came from my plist. 1069 00:58:03,000 --> 00:58:07,000 If I click this this also came from my plist, and this sliding was handled 1070 00:58:07,000 --> 00:58:09,000 by defining those segues. 1071 00:58:09,000 --> 00:58:12,000 I click this and one more, 1072 00:58:12,000 --> 00:58:15,000 and now here is my UIWebView, so just like that 1073 00:58:15,000 --> 00:58:19,000 here's the website for the URL that I just embedded, 1074 00:58:19,000 --> 00:58:21,000 and I didn't have to handle anything crazy. 1075 00:58:21,000 --> 00:58:24,000 This is how to display a web page. 1076 00:58:24,000 --> 00:58:27,000 Things like this back button here are also given to me 1077 00:58:27,000 --> 00:58:33,000 totally for free because I've defined these relationships using segues. 1078 00:58:33,000 --> 00:58:37,000 >> Any questions? Yeah. 1079 00:58:37,000 --> 00:58:40,000 [Student] So when you use alloc, you never have to free anything? 1080 00:58:40,000 --> 00:58:43,000 Exactly, when you call alloc and init you do not have to free it. 1081 00:58:43,000 --> 00:58:46,000 IOS is going to handle all of that for you. 1082 00:58:46,000 --> 00:58:51,000 It's wonderful, and you're not breaking any rules. Yeah. 1083 00:58:51,000 --> 00:58:54,000 [Student] If you were to include more teams that could fit on the screen, 1084 00:58:54,000 --> 00:58:58,000 would it automatically have a scroll option, or is that something you need to add? 1085 00:58:58,000 --> 00:59:01,000 Exactly, if I had more teams, for example, it would automatically handle 1086 00:59:01,000 --> 00:59:04,000 the scrolling for me, and all the performance concerns 1087 00:59:04,000 --> 00:59:08,000 with the huge table are also handled totally for me. 1088 00:59:08,000 --> 00:59:11,000 >> Other questions? 1089 00:59:11,000 --> 00:59:13,000 All of this code is going to be posted. 1090 00:59:13,000 --> 00:59:16,000 We kind of glossed over a little bit of the more minor details, 1091 00:59:16,000 --> 00:59:19,000 but things like setting some properties to the web view 1092 00:59:19,000 --> 00:59:22,000 are just things that you can get by browsing Apple's documentation, 1093 00:59:22,000 --> 00:59:24,000 which is really, really nicely laid out. 1094 00:59:24,000 --> 00:59:27,000 They have a lot of sample apps and example usages 1095 00:59:27,000 --> 00:59:34,000 of different APIs, so definitely peruse those if you can. 1096 00:59:34,000 --> 00:59:36,000 Just some helpful links you might want to take a look at. 1097 00:59:36,000 --> 00:59:38,000 These are some handy documentation guides. 1098 00:59:38,000 --> 00:59:41,000 The URLs are massive, so they're shortened. 1099 00:59:41,000 --> 00:59:44,000 This first one is the entire library of documentation. 1100 00:59:44,000 --> 00:59:46,000 There's little search bars, so if you start typing button 1101 00:59:46,000 --> 00:59:50,000 it will start giving you all the information about all the things you can do with a button. 1102 00:59:50,000 --> 00:59:53,000 I've also included the Table View Programming Guide. 1103 00:59:53,000 --> 00:59:56,000 It handles table views in much more detail, 1104 00:59:56,000 --> 01:00:00,000 how to do things like dynamically add cells or edit cells or remove them. 1105 01:00:00,000 --> 01:00:02,000 >> There's lots of sample apps from Apple that will show you how to do that, 1106 01:00:02,000 --> 01:00:05,000 and finally, this last one is the Human Interface Guidelines, 1107 01:00:05,000 --> 01:00:09,000 and this is basically some discussion of UI components, 1108 01:00:09,000 --> 01:00:12,000 things like don't make a button that's 4 pixels by 4 pixels. 1109 01:00:12,000 --> 01:00:15,000 That's a bad idea, and other things that Apple wants you to do 1110 01:00:15,000 --> 01:00:18,000 to encourage good design. 1111 01:00:18,000 --> 01:00:23,000 >> Any last questions before we finish up? 1112 01:00:23,000 --> 01:00:27,000 All right, definitely feel free to—we're going to have special labels on Discuss. 1113 01:00:27,000 --> 01:00:30,000 We'll have an iOS one, so definitely feel free to utilize that. 1114 01:00:30,000 --> 01:00:34,000 If you want to work with your classmates on projects 1115 01:00:34,000 --> 01:00:37,000 or help figure out some random iOS stuff 1116 01:00:37,000 --> 01:00:40,000 feel free to email me, and definitely peruse all the code online because 1117 01:00:40,000 --> 01:00:43,000 just in the interest of time we kind of glossed over the more 1118 01:00:43,000 --> 01:00:45,000 finer grained details of things. 1119 01:00:45,000 --> 01:00:47,000 But if not, then good luck on your iOS projects, and I hope we have 1120 01:00:47,000 --> 01:00:53,000 a big influx of apps in the App Store. 1121 01:00:53,000 --> 01:00:56,000 [CS50.TV]