Re: Retrieving Multiple Anchor Tags.
Re: Retrieving Multiple Anchor Tags.
- Subject: Re: Retrieving Multiple Anchor Tags.
- From: Kyle Sluder <email@hidden>
- Date: Fri, 15 May 2009 04:35:39 -0400
On Fri, May 15, 2009 at 3:20 AM, Philip Juel Borges
<email@hidden> wrote:
> My problem is that I don't know how to write the code that retrieves any
> anchor tag. Any ideas?
That's not really helpful. You've basically just restated the problem here.
Ladies and gentlemen it's time to play BREAK! IT! DOWN! For those
of you keeping score at home, here are the rules: start with a problem
statement, figure out smaller subproblems to divide it into, and keep
doing that until you get to pieces you can handle. Then get those
pieces done and work it back into a solution to the big problem.
Alright, let's play!
Your overall task is to use the contents of a textfield to control
what gets loaded in a WebView. Break it down, step 1: you need to
write a method. Break it down, step 2: your method needs to get the
contents of the text field. Break it down, step 3: you need to use
these contents to provide to the WebView the fragment identifier (part
after the hash mark). Keep breaking it down and we wind up with a
list of small learning-focused tasks that looks like this:
1. Getting a method called when things happen in the UI.
2. Communicating values from the UI objects to this method.
3. Ensuring these values are valid input, or coercing them to be so.
4. Telling the WebView to load something.
5. Creating the URL fragment from the input we validated/coerced in step 3.
You seem to have a handle on tasks 1 and 4, based on your description.
Step 2 is pretty simple: NSControl has a bunch of methods for
setting/retrieving the value it displays. [1] The one you pick is
influenced by step 5, however. Since you're building a string, you
probably want to go with -stringValue. Based on your implementation
of step 4, you know how to wire up an outlet in IB, so you can just
wire up another one for your text field and call -stringValue: on it
in your action method.
But wait, we're using -stringValue, but we need a number because we
know that all the valid anchors in our document are numeric. At first
this seems to make step 3 more difficult, but it doesn't because Cocoa
has a class called NSNumberFormatter. You can drag one from the IB
Library on top of your text field and force it to only accept numbers.
-stringValue is still going to return an NSString, of course, but at
least the number formatter will ensure that this string contains only
digits.
And finally, step 5. Rather than what you've been doing with
converting numbers to strings, we already have a string, but the basic
URL construction is the same.
Hopefully that's enough information to get you on the right track
towards thinking about how to program using Cocoa. Breaking things
down is an important skill; don't get too hung up on your ultimate
goal. Thanks for playing Break It Down! I'm Chuck Woolery, and I'll
see you next week.
--Kyle Sluder
[1] http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html#//apple_ref/doc/uid/20000073-SW8
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden