Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jjshammas

macrumors newbie
Original poster
Oct 21, 2010
6
0
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:
 

jjshammas

macrumors newbie
Original poster
Oct 21, 2010
6
0
You cannot typecast an NSString into an NSURL. NSURL has methods for creating an instance from an NSString. You should use them.

Do you mind explaining a bit on how to methodize it to change to an NSURL?
 

jjshammas

macrumors newbie
Original poster
Oct 21, 2010
6
0
Do you understand the Objective-C fundamental of calling a class or instance method, and the difference between the two?

I guess a little bit; could you maybe just give an example snippet of code on how to change the URL of a webview?
 

PatrickCocoa

macrumors 6502a
Dec 2, 2008
751
149
Tough Love

I'm looking at information about NSURL on the Xcode Developer Documentation, but it's kind of difficult to grasp.

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.
 

Krevnik

macrumors 601
Sep 8, 2003
4,100
1,309
I guess a little bit; could you maybe just give an example snippet of code on how to change the URL of a webview?

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.