PDA

View Full Version : Objective C - setURL not working?




jjshammas
Oct 21, 2010, 05:49 PM
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:


#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:



dejo
Oct 21, 2010, 05:57 PM
[changeWebSite setURL:(NSURL *)@"http://www.google.com/hi"];

You cannot typecast an NSString into an NSURL. NSURL has methods for creating an instance from an NSString. You should use them.

jjshammas
Oct 21, 2010, 06:00 PM
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?

dejo
Oct 21, 2010, 06:06 PM
Do you mind explaining a bit on how to methodize it to change to an NSURL?
Have you looked at the NSURL Class Reference yet?

jjshammas
Oct 21, 2010, 06:07 PM
Have you looked at the NSURL Class Reference yet?

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

dejo
Oct 21, 2010, 06:10 PM
I'm looking at information about NSURL on the Xcode Developer Documentation, but it's kind of difficult to grasp.
Do you understand the Objective-C fundamental of calling a class or instance method, and the difference between the two?

jjshammas
Nov 10, 2010, 02:51 PM
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
Nov 10, 2010, 03:12 PM
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
Nov 10, 2010, 03:23 PM
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: (http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/URLWithString:)
– initWithString: (http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURL/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:@"Foo"];
[[NSURL alloc] initWithString:@"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.