Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Constructing the view controller

From: iOS SDK and SQLite: Building Data-Driven Apps

Video: Constructing the view controller

At this point, we have a working FeedsTableViewController for viewing feeds and a working ItemsTableViewController for viewing items. In this movie, we will create the AddViewController for adding feeds to the database. This will be accomplished by creating a modal dialog view for getting the feed URL from the user. We'll start by making a working copy of BWRSS-addView-start, and we'll call it BWRSS-addView-02. I'm just going to expand this a little bit. And there is our addView, and that's our start, and I make a copy, and I rename it with 02. And I'm just going to open the Xcode by double-clicking on this Xcode project file.

Constructing the view controller

At this point, we have a working FeedsTableViewController for viewing feeds and a working ItemsTableViewController for viewing items. In this movie, we will create the AddViewController for adding feeds to the database. This will be accomplished by creating a modal dialog view for getting the feed URL from the user. We'll start by making a working copy of BWRSS-addView-start, and we'll call it BWRSS-addView-02. I'm just going to expand this a little bit. And there is our addView, and that's our start, and I make a copy, and I rename it with 02. And I'm just going to open the Xcode by double-clicking on this Xcode project file.

And now, before we can create the screen in storyboard, we need to create a new class to support the AddViewModal dialogs. So, I'm going to select our BWRSS folder up here. You could just right click on this and say New File, or you can go up to the File menu and say New > File; either one does exactly the same thing. And we're going to select a Cocoa Touch Objective-C class, press Next, and this is BWRSSAddViewController, like that.

And it is a subclass of UIViewController, so you want to select UIViewController and not UITableViewController, if that's the last one that you used. It was apparently the last one that I used. And leave both of these unchecked. It is not going to be targeted for the iPad. We're not using NIBs. And I'll select Next, and it will get stored right there in the BWRSS folder with all the other m and h files. And you want to make it in the BWRSS group and make sure that the BWRSS application is targeted so that check box needs to be checked there.

I'll press Create, and there we have our new class files. I'm just going to drag them up above that Supporting Files folder. And I'm going to open the .h file, and we'll get started in there. And switching back to my Finder, you see here that I have an AddView02-header.txt file. So, I'm going to open that up, and I'm just going to select all of these and drop it in this Xcode file. I'm just going to select these there and make sure our ends are matched up; they are.

And so there are a couple of interesting things going on here. You will notice that we have a protocol declared. This is the RSSAddViewControllerDelegate protocol. This has three methods defined in it. And we're going to be implementing that towards the end of the chapter, but that allows the AddViewModalViewController to communicate with the FeedsTableViewController. The FeedsTableViewController is going to be the parent of this ModalViewController, but because this is a ModalViewController, we don't want to do things like dealing with the database directly; we want to communicate back to the parent ViewController, which is not modal, and allow it to do those time- and resource-intensive things.

Otherwise, we end up affecting the performance of the rest of our device. And in fact, iOS even prevents us from doing certain things from a ModalDialogueViewController. So, we had this protocol. And we'll see that it adds a layer of complexity, but it's not that bad. You will see it is relatively easy to implement. Our AddViewController itself will be using the NSURLConnectionDelegate and NSXMLParserDelegate protocols. We have this delegate property, which actually gets populated from the FeedsTableViewController, and we'll see how to do that later in the chapter.

And then we have a TextField, a Label, and a couple of Actions. We'll be hooking those up to our ModalViewController. So, I'm going ahead and press Command+S to Save, and I'm going to switch to the .m file. I just did that by pressing Ctrl+Command+Up Arrow here on my keyboard. You'd also just select it in the Project Navigator. Of course, you may want to be using the Assistant Editor. I'm not using the Assistant Editor because I have such limited screen real estate here. So, over here in the .m file, or methods file, I'm going to go back to the Finder and I'm going to bring up the stuff that we don't want to have to type a lot of.

You can see there is quite a bit of that. Now, first, we have this import BWUtilities because we used some of those utilities. So, I'll go ahead and I'll paste that in right there. And we have a whole lot of constants, and we'll be using these throughout this chapter and throughout this class. So, I'm just going to go ahead and select all the way to the end here, and I'm going paste this in, in Xcode. I'm going to select these and replace that. Notice we have this warning here. That is probably for an incomplete implementation because there are things that we had declared in the header file that we haven't implemented in here yet.

So, we have a number of constants that we are using for various different purposes. It's always a good idea to use a constant rather than typing in a literal string or a literal number, especially if you're going to be using that literal more than once. We are also declaring a number of instance variables here. We have an enum that we use for a mode switch later on. The xmlData is used for accumulating data as we read it from the network connection, currentElement is used during parsing, feedRecord is used to accumulate data for the feedRecord before we pass it back to the parent controller, and a number of flags here, and some local properties as well.

We also have a number of methods that we're going to go ahead and just drop these in right now. And so I'm going to copy that and come down in here and paste them just before this @end. We have a duplicate here of the viewDidLoad, so I'll go ahead and I'll delete that one. So, viewDidLoad, it sends a becomeFirstResponder message to the URL text feed. We'll see what that does in a little while. It makes that TextField the focus as soon as the view is loaded.

And we have actions for our Cancel button and our Add button, and we also have this textFieldShouldReturn, which intercepts the Return button, or the Return key from the keyboard, and allows us to take an action based on it. We'll see how that works as well. So, I'm going to go ahead and press Command+S to save this, and now we're going to create the interface in our storyboard. So, I'm going zoom out here a little bit because this is just too big to work with in this small amount of real estate I have here on the screen.

And I'm going to bring out our Utilities pane. And the first thing I'm going to do is I'm going to come over here to the FeedsTableViewController and I'm going to add a button. And so I'll come down here into Windows and Bars. There it is, Bar button. I'm going to drop that on to--yeah, it looks like I need to zoom in in order to do that. I'm going to drop that on there and under Style, Bordered is fine, Identifier, Add, and that will give it a little plus sign.

And I'm going to zoom out again, and I'm going to create a new Controller. Under Controllers & Objects, I just want a Plain View Controller. Now, I want us to get under this TableViewController here, and I'm going to select both of them and scroll them up a little bit. And I'm going to create a new segue from this plus button that I've just created.

So I'm holding down the Control key on my keyboard while I drag a line from here to our new ViewController, and I'm going to select modal for the segue. Now, we have our new segue here, and I'm going to select that. Its identifier will be SegueToAddView. Its style is Modal, and its Transition will be Cover Vertical. I'm going to press the Enter key there and press Command+S to save.

Now, we're going to zoom in to our new ViewController here, and I want to select the orange circle there which identifies our controller and come over here to show the Identity Inspector, and we're going to select our new BWRSSAddViewController for that. And so now we can see it's the AddViewController. And I want to set some properties on this controller--make sure I've got the Controller selected. I'm going to get a Scroll View and drop that on here so that we can add some items to that.

So, there's our Scroll View, get that nicely centered in there. And I'm going to select the Background and make it the Scroll View Textured Background Color. Now, the thing about the Scroll View Textured Background Color is that it doesn't actually show up here on this screen, but it is a dark color. So, I'm going to be putting items on it. I'm going to setting their color to white so that we can actually see them. And this will make sense when we go to test it, and you'll see what it looks like. But in the meantime, it's going to make things a little bit difficult to work with, and that's just a quirk of how Interface Builder works and how storyboards work.

So, I'm pressing Command+S to save. I do that a lot. And I'm going to grab a text Label here, under Controls, and Label. I will bring it up to the top here and center, or you can bring it up to the top and left, and you can just drag its right side out all the way to the other line. So, I'm using these guides to help to line things up. And it makes it so that everything looks like nice on the screen when we're done laying it out. I'm going to select a Center Alignment and the System Bold Font.

The Size of 17 is good, and it is going to say Add RSS Feed, and you can see that it says Add RSS Feed. If I take my cursor away and click over here, you can see how nicely centered that is and everything. But now I'm going to set its color to white, and we won't be able to see it very well anymore. Here's the Text Color and make it White Color. And so now it becomes much more difficult to see, but it is there.

I'm going make sure this is properly centered. That is just necessary because this is actually a darker background than what it looks like. So, we going to grab one more Text Label here, and we're going line this up so that it's centered under this other Text Label that we have got there. And I'm going to grab the ends of it and drag them out to the sides. This one is going to say, "Enter a URL," and that will serve as a prompt. It is also going to serve as a status line, and this is going to be System Font and its Size is going to be 12.

And again, we're going to set the color to white so it shows up really nicely. So, I can either select it down here or I can select it from the Recently Used Colors. So, that will be White also, and we'll have trouble seeing it until we run it. Now, we're going to grab a Text Field, and we're going to drop that in right under the Text Label. You see these little guides. They really help with this a lot, for lining things up. Our Text Field is going to start with http://. And I'm not using that as a placeholder text because we don't want it to disappear when we start to type in there.

We want to be able to actually use that as a prefix or select it and get rid of it if we want to. So that works really well. We're going to set some options down here: Capitalization, None, Correction, No, the Keyboard will be the URL keyboard, its Appearance is the Default, and the Return key will say Done. And we're not going to change the Text Color on this one because it's going to show up like this with the white background, and it will actually look really nice with that textured background. Now, we're going to add a couple of buttons, round rectangle buttons. And I'm going to start with them over here to the left.

And I'm just going to kind of put them close to each other like that, and then I'm going select both of them and drag them to the center so that they're centered but they're spaced just nicely. And the one on the left is going to say Cancel, and the one on the right is going to say Add. Now, I'm just going to scroll a little bit so we can see our Controller Delegate--it's really difficult with the amount of space I have here on the screen--because we want to be able to hook up a few of these things here. Firstly, I'm going to control drag from the text field all the way down here to our controller, and I'm going to select Delegate.

All right, and that allows this Text Field to delegate its protocol to our AddViewController, and I'm going to press Command+S here. That allows us to do this thing down here. Text Field should return, that allows us to get this message when the Done key is pressed on the keyboard. And the Cancel and Add buttons, again, I'm going to control drag all the way down here. So, that's the Cancel action.

It looks like it worked. Let's just do it this way. Check and make sure that worked. Yes, there is our Cancel action, that's great. And our Add action, I can just drag right from there to our Add button, and it's Touch Up Inside. You'll notice that the Default Action is Touch Up Inside, so you can drag it this way. It's just a little bit more complicated to do it that way. If I didn't do that, I can drag it from here, and I have to drag it all the way down here to the Add action there.

And then when I select this, we can see that we get that Touch Up Inside action. So, if we select the Controller and our Connections Inspector, we can see all of these connections. Our statusLabel will go here to our white label there. Our URL Text Field will come here. And now, it looks like we've hooked up everything that we need to hook up. So, I'm going to press Command+S again to make sure this is saved.

And now let's go ahead and run it on the iPhone Simulator. I'm going to select our Run button here. Now, we're just going to press this Plus button, and there's our modal dialog. And you'll notice that it gave the focus to our Text Field, and so here's our little keyboard. Our label RSS Feed or Enter URL in white, those are very nicely visible. If I press the Cancel button, it dismisses it. If I press the Add button, of course dismissing it is all that anything it does. And if I type in a URL and press the Done button, you see that that dismisses it as well.

We now have a working storyboard interface that we can use for the modal dialog box. The next step will be to implement the methods in the AddViewController so we can start adding feeds to our database.

Show transcript

This video is part of

Image for iOS SDK and SQLite: Building Data-Driven Apps
iOS SDK and SQLite: Building Data-Driven Apps

41 video lessons · 6417 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 8m 29s
    1. Welcome
      57s
    2. Exercise files
      2m 17s
    3. Course overview
      3m 4s
    4. Application overview
      2m 11s
  2. 14m 49s
    1. Prototyping in a testbed
      1m 27s
    2. Building the view controller
      3m 45s
    3. Coding the testbed
      7m 56s
    4. Using the testbed
      1m 41s
  3. 37m 49s
    1. Understanding SQLite in iOS
      1m 41s
    2. Creating an Objective-C interface for SQLite
      9m 57s
    3. Testing the BWDB interface in the sandbox
      7m 1s
    4. Designing a database schema
      8m 7s
    5. Supporting the application with a specific interface
      7m 7s
    6. Using C pointers with automatic reference counting (ARC)
      3m 56s
  4. 21m 18s
    1. Understanding the table view
      1m 33s
    2. Creating the view controller
      6m 39s
    3. Reading from the database
      13m 6s
  5. 33m 50s
    1. Understanding the parsing process
      1m 57s
    2. Creating the item view controller
      12m 25s
    3. Reading data from the internet
      5m 30s
    4. Parsing the feed with NSXMLParser
      8m 2s
    5. Updating the item view with the feed items
      5m 56s
  6. 40m 14s
    1. Understanding the modal view
      1m 47s
    2. Constructing the view controller
      15m 5s
    3. Finding a feed link in a web page
      8m 55s
    4. Parsing the feed with NSXMLParser
      5m 4s
    5. Delegating back to the parent view
      6m 11s
    6. Deleting feeds
      3m 12s
  7. 21m 5s
    1. Creating the web view class
      12m 33s
    2. Coding the web view
      5m 25s
    3. Viewing pages in Safari
      3m 7s
  8. 14m 3s
    1. Understanding the iOS preferences system
      1m 23s
    2. Creating the preferences plist in Xcode
      7m 20s
    3. Reading preferences in your application
      5m 20s
  9. 6m 15s
    1. Adding pull-to-refresh functionality
      2m 34s
    2. Implementing the pull-to-refresh gesture for iOS 6
      3m 41s
  10. 27m 1s
    1. Understanding split view
      1m 4s
    2. Coding the table views
      11m 24s
    3. Implementing the iPad detail view
      6m 35s
    4. Implementing the iPad modal view
      7m 58s
  11. 35s
    1. Goodbye
      35s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed iOS SDK and SQLite: Building Data-Driven Apps.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.