Automatically adding 'http://' into NSTextField

Discussion in 'Mac Programming' started by tominated, Dec 4, 2006.

  1. tominated macrumors 68000

    tominated

    Joined:
    Jul 7, 2006
    Location:
    Queensland, Australia
    #1
    What and where would i put into

    Code:
    - (IBAction)connectURL:(id)sender{
    [urlString setStringValue:[sender stringValue]];
    [[webView mainFrame] loadRequest:
    [NSURLRequest requestWithURL:
    [NSURL URLWithString:
    [sender stringValue]]]];
    }
    to make it automatically insert the 'http://' infort of the url if it isn't already there.

    BTW: this is what happens to my url text field when enter is pressed.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Assuming sender is the NSTextField:

    Code:
    - (IBAction)connectURL:(id)sender
    {
        NSString *url = [sender stringValue];
        if (![url hasPrefix:@"http://"])
            url = [NSString stringWithFormat:@"http://%@", url];
        [urlString setStringValue:url];
        [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    }
     
  3. bronxbomber92 macrumors regular

    Joined:
    Nov 23, 2006
    #3
    Are you by chance following the tutorial at macevcenter.com (O'reilly)?
     
  4. tominated thread starter macrumors 68000

    tominated

    Joined:
    Jul 7, 2006
    Location:
    Queensland, Australia
    #4
    isn't it obvious?
     
  5. tominated thread starter macrumors 68000

    tominated

    Joined:
    Jul 7, 2006
    Location:
    Queensland, Australia
    #5
    Awesome! thanks! w00t!
     
  6. bronxbomber92 macrumors regular

    Joined:
    Nov 23, 2006
    #6
    Well, no. There are several Cocoa Web Browser tutorials on the web :p
     
  7. tominated thread starter macrumors 68000

    tominated

    Joined:
    Jul 7, 2006
    Location:
    Queensland, Australia
    #7
    there are? what are they?
     
  8. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #8
    I see you're ignoring my advice about indenting your code and not breaking your lines into non-sensical separate lines that make your code harder to read.

    Notice the difference between the way your code is written and Kainjow's. His is easy to read....
     
  9. tominated thread starter macrumors 68000

    tominated

    Joined:
    Jul 7, 2006
    Location:
    Queensland, Australia
    #9
    sorry. it's just that i have no clue on where to indent it, i just copyed it straight from a tutorial
     

Share This Page