Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In order to display pages in the WebView, we need to create a class for the WebView delegate methods, and we need to create the view in the storyboard. So, we'll start by making a working copy of the BWRSS-webView-start. That's in chapter 6 in our exercise files. And I'm just going to drag it here with the Option key pressed and rename it to BWRSS-webView-01. And we'll go ahead and open this in Xcode by double- clicking on the Xcode project file. And I'm just going to do a couple things here to get situated.
I'm going to make sure there are iPhone Simulator selected. I'm going to open a couple of these disclosure triangles, and we're going to start by creating a new class. So, I can either select File > New > File here, or I can just right-click on the BWRSS group and say New File. This is going to be a Cocoa Touch Objective-C class. Press Next. And it's going to be called BWRSSWebViewController. And it's a subclass of ViewController, so if you have TableViewController or something else selected, you want to make it a subclass of ViewController. And leave both of these unchecked.
It's not targeted for iPad, and it's not using a NIB file, so we say Next. And it will be in the BWRSS group and with the BWRSS Target checked, and this is the correct folder for it. So, that's all good. We select Create, and there it is. You notice it drops it in down here at the end because it's kind of alphabetical. I'm just going to move it up in there. And I'm going to open up the .h file and come back out here to our finder, and you see here we have the 01-header.text file there.
I'm going to open that up in my text editor. I'm just going to select all of that and come back in here to Xcode, and if I select this and replace this line with all of that, it actually comes out right. You'll notice--I'm going to minimize this Utilities bar over here. You'll notice we're implementing the WebViewDelegate protocol, and we have an IBAction for something called an actionButton. We'll see what that is in a little bit here. And we have IBOutlets for the webView and a backButton and a forwardButton, and a property for feedItem that gets passed from the Items View Controller when this view is launched.
So, I'm going to press Command+S here to safe. I'm going to move over here to the .m file. And all we really need to do in here, and you see because this is just based on the View Controller, it's pretty lightweight here. It doesn't have a lot of stuff in the template. Also, you notice this little exclamation point. It says, "Incomplete implementation," That's because in our .h file, we have this IBAction method, and it's not defined in here, so we're going to go ahead and define that. We're just going to put that down here. And it looks like that, and we're just going to put a little NSLog in here for now.
And what's that's going to do is when this button is pressed, it's going to display in the log the name or signature of it's function, which will look kind of like that, and the URL that's been passed from the Items view, and that just will let us know that this button is working when we press this particular button. So, I'm going to press Command+S to save this, and we're going to come over here now to our iPhone storyboard--that's this one here-- and we're going to create our WebView.
Now, I'm going to bring this Utilities panel back out. I'm going to scroll back over. This is a little bit complicated to do with the limited real estate here. Zoom out, I'm going to grab us a new View Controller, and that's going to go over here in some place. Try and align that up a little bit. And it's going to insist that I scroll in to do this next part. I'm going to create a segue. Now, when I create a segue, I'm going to hold down the Control key and I'm going to drag from this prototype cell, and that will create the segue.
I'm going to select Push. Now you'll notice that one of the things that it does here is it adds that disclosure arrow back into the prototype cell, and it sets this accessory property here to Disclosure Indicator, so I have to set it back to None if I don't want that. So now, we have this new view. And in this new view, we want to have a WebView and we want to have a toolbar. So, we're going to start with the toolbar because it actually make sense, because then the WebView will snap into the right place. So, I'm going to grab this toolbar here and I'm just going to drop it into the bottom, and I'm going to grab a Web View now, and that's in Data Views.
And you'll notice that it just kind of fills that remaining space just nicely. And then I'm going to come down here and I'm going to set the class of the View Controller. So, I'm selecting the identity inspector, and I'm going to select our Web View Controller for that. Now, whenever I hover over that, it says Web View Controller. I'm going to make sure that our segue is named properly, so I'm going to select the segue here and come back over to the Attributes Inspector. And I'm going to type in SegueToWebView and press Enter.
Now, our segue is named properly. Now, I want to add some buttons to our button bar down here, but before I do that I want to show you the buttons. A couple of these buttons I've had to create them myself, and I used PhotoShop. You can use whatever application you want to. I'm going to press Command+S to save here and come back out to the Finder. And here, in this Assets folder, you'll see four PNG files. And if I bring these up in Preview, see there's one called back-arrow.png, and it looks like that, and there's firstname.lastname@example.org, and it's the same thing, just a little larger, forward-arrow and forward-arrow@2x.
The thing that these images is these are going to go on a toolbar, and the way that these work is that the only thing that's actually used in the toolbar is the transparency mask of the PNG file. So, what color it is, what image is in there, any of that is completely discarded, and all that's used is the transparency mask. So, I just make them black on transparent, and that works out just fine. So, I'm going to take these images and I'm going to drag them into our Supporting Files folder here, and you'll notice that we get this nice little dialog box.
I'll make sure that copy items is checked. Create groups, there aren't any groups so that's fine, and Add to target this BWRSS. So, I select Finish, and there is our four graphics there. Now, when we come over here to our toolbar-- and I'm going to just select this item. You notice the toolbar starts with one item on it. And I'm going to say that it's plain. It doesn't have a border, and I'm going to select an image for it. You'll notice that our graphics are here. I don't want to select one of the @2X ones. That happens automatically.
If I select the back-arrow, for the retina displays, the @2x, the larger-size one, will automatically be used. So now, we have a back-arrow.png there, and you'll notice that it displays just as you would expect it to. It's just using the transparency mask to find the boundaries of the image. So, that's pretty nice, and that works great. I'm going to come back over here to Windows & Bars, and I'm going to grab the Flexible Space. What we're going to do is we're going to put five buttons out here and flexible space in between all of them. So, that's four flexible spaces.
I'm just going to drop all of those in place right now because it's convenient and easy to do it this way. And then I'm going to come back up here and I'm going to get Bar button Item, and I'm going to get four more of those. We've got one already. And I'm going to drop all of those in between the flexible spaces. So, that lays it out just perfectly, like that. And then for each of these, I'm going to make them Plain. And this first one, this will be the right-arrow image, the forward-arrow image, and this next one, Plain. This next one is going to be the Refresh image.
That's one of the stock ones. This one is going to be the stop image, so Stop. That's got that X to it. And you see how they just resize and that flexible space in between just works to hold them all exactly the same space apart. It works really nicely. And this last one is one that is called Action, and that's the one that's going to get that action method. That's for, when you press that it will load the web page up in Safari. It will move away from this app and open it up in Safari for those times when you want to load a web page in Safari.
Of course, these flexible spaces won't actually show up on the app, but it's all going to look really nice like that. And now we're going to hook things up. We can come over here to our Web View Controller, and we're going to select the Connection Inspector here, and you'll notice that we have this Web View Property. Remember in our WebViewController.h, we had this WebView IBOutlet, and that's what that is. And so we're going to grab that, and we're going to hook it up to our WebView. And these Back button and Forward button items, these are the IBOutlets that we declared for those. And that actually allows us, not so much to intercept these, but to refer to them so that we can gray them out and inactivate them when they're not valid.
And we'll see how to do that later in this chapter. This Action button, that's what we can intercept that action. And then we're going to select the WebView, and the WebView itself has some actions that we can hook up to these buttons so that all of these buttons automatically work with the WebView. We've got the Go Back button, which go to our left arrow; and our Go Forward button, which will go to our forward arrow; reload, which will go to the Refresh button; and Stop Loading, which will go to the Stop button.
So that's all based on the WebView itself. Okay, so I can save this. And in order for this to work, we need to go back here into the ItemsViewController and hook it up to the segue. So, in order to use this class from here, we need to import its header file. So, I'm going to come back out here to the Finder now, and I'm going to grab this ItemsTableViewController-01methods.txt file, and that has our segue delegate for calling up our WebView.
So, I'm going to come back into Xcode, and I'll just drop that in right here before the Table view data source part. And it looks for the Segue identifier, SegueToWebView, and I'm going to press Command+S to save. We'll go back into our iPhone storyboard, and we'll take a look at this segue, and we see that's SequeToWebView. So, that's right. It creates a WebViewController, a pointer to that object. From our destinationViewController, it grabs the IndexPath for the selected row, and it uses that to find their correct item record from the database, and it passes that item record into the feedItem property in the WebViewController.
So, this should all be familiar at this point. We've done this several times before in this course. Now, if we come back in here to our WebViewController, we can see we don't have any of the view methods set up yet, so we'll just get a blank WebView. But what will happen is when we press that Action button, we should get a little NSlog here that shows our feedItem URL, and that will tell us that everything is working properly. So, I'll make sure the iPhone Simulator is selected and press Run, and it builds with no issues. Now, it will launch the iPhone Simulator.
And there it is. And if I bring up one of these items from the blog for example, there is our WebView, and there is all our buttons. They look nice and they're perfectly spaced. And if I clicked on this one on the right, we should get a log item down here, and we do. And there is that function signature, and there's the URL. So, these are the steps you will follow to create most any view over the storyboard. Now that we have the WebView working and we're calling it successfully from the Items View, now it's time to code the rest of the class, and we'll start with that in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98081 Viewers
61 Video lessons · 85345 Viewers
71 Video lessons · 69366 Viewers
56 Video lessons · 101730 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.