Objective C - setURL not working?

Discussion in 'Mac Programming' started by jjshammas, Oct 21, 2010.

  1. macrumors newbie

    Joined:
    Oct 21, 2010
    #1
    Hey guys, excuse me for being uneducated, I've just started learning Objective-C.

    Basically when I click a button, I want a URL to be opened in a webview element. I have an object set up with an outlet "changeWebSite" and an action "startNow:". changeWebSite is linked up to the webview, and the action linked up to the button. After Interface Builder wrote the .m and .h files, I tested this out:

    Code:
    #import "StartNow.h"
    
    @implementation StartNow
    - (IBAction)startNow:(id)sender {
        [changeWebSite setURL:(NSURL *)@"http://www.google.com/hi"];
    }
    @end
    There's no errors, but nothing happens when I click the button. I tested it out by linking the output to a textbox and use a setStringValue method (which worked), but I just can't get the setURL to work. Any suggestions? :confused:
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You cannot typecast an NSString into an NSURL. NSURL has methods for creating an instance from an NSString. You should use them.
     
  3. thread starter macrumors newbie

    Joined:
    Oct 21, 2010
    #3
    Do you mind explaining a bit on how to methodize it to change to an NSURL?
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Have you looked at the NSURL Class Reference yet?
     
  5. thread starter macrumors newbie

    Joined:
    Oct 21, 2010
    #5
    I'm looking at information about NSURL on the Xcode Developer Documentation, but it's kind of difficult to grasp.
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Do you understand the Objective-C fundamental of calling a class or instance method, and the difference between the two?
     
  7. thread starter macrumors newbie

    Joined:
    Oct 21, 2010
    #7
    I guess a little bit; could you maybe just give an example snippet of code on how to change the URL of a webview?
     
  8. macrumors 6502a

    Joined:
    Dec 2, 2008
    #8
    Tough Love

    I'm not trying to be obnoxious (it just comes naturally), but you'll need to be able to swim in the sea of Apple documentation. You'll have thousands of questions like the one you've just asked. Being able to dive into the docs and get what you want will help you tremendously in the long run.

    That said, you'll need to:
    1. Create an NSURL (using alloc and some kind of init);
    2. Create an NSURLRequest (using the NSURL you created in #1);
    3. Send the NSURLRequest to the UIWebView you've mentioned that you've already created.
     
  9. macrumors 68020

    Krevnik

    Joined:
    Sep 8, 2003
    #9
    Part of trying to push you this way is that once you understand the core concepts and can read the docs, it becomes fairly quick and easy to figure this stuff out on your own. Teaching a man to fish versus giving him a fish, so to speak.

    The problem is that NSString != NSURL. They are two different objects, and you can't just typecast objects between each other and expect it to come out right. You can really only typecast "up" an inheritance tree (say, from NSData * to NSObject *, or id).

    The documentation shows you the actual declaration of the code as it would appear in the header.

    In this case, say we look at:

    + URLWithString:
    – initWithString:

    They both give you a NSURL with a string. If you know the difference between "+" and "-" when declaring a method in a header, then it's clear what they mean here.

    But you still have to create an object just like you do any other object in Objective-C, by either using a class method constructor, or allocating and initializing it.

    [NSURL URLWithString:mad:"Foo"];
    [[NSURL alloc] initWithString:mad:"Foo"];

    These both return an NSURL object from the string @"Foo". But the normal rules of Objective-C apply. So one will return an autoreleased one, and the other won't (exercise left up to the reader to figure out which is which). This is core Objective-C syntax and structure here, and in the future, you will be better off reading up on this before jumping in whole hog. As you understand the language and ask less for code snippets, all this will become easier for you.

    The beginning of the learning curve is steep (like learning a foreign language) until you get used to it. But it is 100% worth getting there. This type of question asking for the snippet isn't going to get you there, which is why dejo answers the way he did.
     

Share This Page