Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
In iPhone SDK: Developing iPad Applications, author Simon Allardice shows developers how to apply their existing knowledge of Xcode, Objective-C and the iPhone SDK to create applications for the iPad. The course focuses on what's different on the iPad, including design principles for the larger screen real estate, detecting device capabilities, and working with new Xcode project types. Exercise files accompany the course.
Well, the next thing we want to do with our popovers is actually have them do something when we select one of these buttons. To do this, we are going to work with delegation, because we'll be communicating between objects here. Now, a UIPopoverController is capable of delegation by itself, but that's not exactly what we're talking about. We've got this custom content in our popover. We want it to do something. We wanted to do something back on our main view controller. So we are going to delegate some behavior to the main view controller and this is how we do it.
Going back to the code, I am going to go into the interface for my FontChooserViewController. This is the view controller file that provides content to my popover. And I am going to put an entry here called protocol entry. What this is going to do is saying, I am capable of delegating behavior to other objects and the entry here with the method signature that returns void, says that's all right. Somebody can volunteer to be a delegate for me and if they do volunteer, they need to provide a method called fontSelected, the text appointed to a string called fontname.
Well, so there is something to connect to, we're going to declare an actual delegate object itself here called delegate and wrap that in a property. Now if you are not used to creating your own delegate protocols, this is certainly going to seem a little odd, a little strange the first time through. Particularly, if you're coming from other programming languages. But in Cocoa Touch and in Cocoa itself, this idea of delegation between objects is very, very common and something you should be getting used to.
Now, because this is the view controller providing content for our pop-up, I'm also going to declare an IBAction here for when the buttons are tapped. And this is just a normal one that's going to take ID called sender. Okay, so that's step one. Step two is we are going to jump over into the implementation file. I am going to synthesize the delegate there and provide the implementation for that buttonTapped Method. So the code that I'm writing here will happen when somebody taps a button inside this popover.
The first line is going to actually take the generic id sender and cast it as a button because I know it's going to be a button that's causing this to happen. I want to treat it as a button so that on the next line I can ask, what's the title for the button? Now, buttons have multiple states. So I am asking for the title for the normal UI control state and just storing that in the NSString called font. Then I am checking to see if the delegate exists, and if it does, I am calling it. I am saying, hey delegate, do your fontSelected method. Here is the argument called font.
This is really where we're handing off that delegate method. So I am going to save that, and just compile, see how we are going. We have succeeded so far. Well, the next thing that we have to do is actually start working with the code in the other parts of our application. Specifically our PopoverDemoViewController. The one thing that I need to do in this file is actually say yes, I'm going to be a delegate for the FontChooserViewController. And the way that I am going to volunteer to be a delegate is by putting the words FontChooserDelegate within angle brackets after the name of the class I am inheriting from.
Now because it won't know what this means unless I do the import statement, I better add that too. But the real key here, it needs to be in my implementation file for this ViewController. After all I am volunteering. I am saying, I am going to be the delegate for you. Well, that means I need to provide that delegate method. Now, this doesn't need to be very complex. I do have to follow the signature of it, which was void fontSelected, there we go. It's picking it up. So I should have been passed through a string from that delegate popover object.
What I can do is target my text view, which I called allText, and set its font equal to, well to create a font I use the UI font object, fontWithName. We'll take the argument that gets passed in and I'll just make it 18 points for the font size here. Now, there is a couple of things left to do. One is expecting that we are actually being passed through a font name and I want to take that from the text on the button. But if I open up my FontChooserViewController, I currently have those buttons as a, b, c, and d. I am just going to change their names to Arial, Courier, Verdana, and Georgia.
Fonts that I know exist. Now, I could save this and compile it just to see if it succeeds, and run this again. I am not actually expecting this to work and there is a reason for that. So if I say select font, I can click these buttons. Arial, Courier, Verdana, and Georgia, and the popover seems to pop up and disappear but it's not changing anything and that's because of one of the last pieces that we really need to do. Back in my main view controller implementation file, this is the code that we wrote to cause our popover to appear, and on this line here is where we're saying create a new instance of the FontChooserViewController, and that's what fuels the popover.
Well, before we do that, we want to make sure that we are naming ourselves as delegates for it. The Delegate property becomes available and we can just say self. Create a new instance of that object, name ourselves the delegate. We're volunteering. We are saying, yes, we can respond to that method if you choose to send it to us. So it's definitely a lot of connections back-and-forth and again if it's your first time working with delegates, you're probably finding it a bit tough keeping track of them.
But, it's really all about kind of standing up and saying, yes, I'm going to do this. I can be a delegate for you. So we'll save that, build it, run this application, open up the popover, Arial, Courier, Verdana, Georgia. We are still not having it work yet. Well, there is a pretty good reason for that. Over in my NIB file, I want to take a look at my document window and just take a look and see if these buttons are hooked up to anything. And they're certainly not. Well, that would help.
So I am going to select the Arial button, hook Touch Up Inside to File's Owner. Yes, there is a method called buttonTapped. That's the one that we want. We're using the same one for all of them. So select Courier. Touch Up Inside to buttonTapped. Select Verdana, Touch Up Inside to buttonTapped and you know where I'm going with this, select Georgia. Touch Up Inside to buttonTapped. If you ever need to look at them from the other perspective, I could right-click File's Owner and I would say there is multiple actions being received by the same method.
We can always expand those to change them if we need to. I am going to save this, return to Xcode and Run. Open the popover, select Arial, select Courier, select Verdana, select Georgia. That seems to be working just fine. It might seem like an awful lot of code to write for something that's at the end of the day quite simple, but really what we are trying to build here is a very flexible framework, so that we can construct objects that don't have to know a lot about each other, and can still work together very well.
That's the power of delegation in Cocoa Touch.
Find answers to the most frequently asked questions about iPhone SDK: Developing iPad Applications.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
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.