Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 21, 2010, 05:49 PM   #1
jjshammas
macrumors newbie
 
Join Date: Oct 2010
Objective C - setURL not working?

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?
jjshammas is offline   0 Reply With Quote
Old Oct 21, 2010, 05:57 PM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by jjshammas View Post
Code:
    [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.
__________________
dejo is offline   0 Reply With Quote
Old Oct 21, 2010, 06:00 PM   #3
jjshammas
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Quote:
Originally Posted by dejo View Post
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 is offline   0 Reply With Quote
Old Oct 21, 2010, 06:06 PM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by jjshammas View Post
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?
__________________
dejo is offline   0 Reply With Quote
Old Oct 21, 2010, 06:07 PM   #5
jjshammas
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Quote:
Originally Posted by dejo View Post
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.
jjshammas is offline   0 Reply With Quote
Old Oct 21, 2010, 06:10 PM   #6
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by jjshammas View Post
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?
__________________
dejo is offline   0 Reply With Quote
Old Nov 10, 2010, 02:51 PM   #7
jjshammas
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Quote:
Originally Posted by dejo View Post
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?
jjshammas is offline   0 Reply With Quote
Old Nov 10, 2010, 03:12 PM   #8
PatrickCocoa
macrumors 6502a
 
Join Date: Dec 2008
Tough Love

Quote:
Originally Posted by jjshammas View Post
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.
__________________
iMac 21.5", 3.06GHz, 4 GB, 2 TB HD.
iPod Touch 3G.
PatrickCocoa is offline   0 Reply With Quote
Old Nov 10, 2010, 03:23 PM   #9
Krevnik
macrumors 68020
 
Krevnik's Avatar
 
Join Date: Sep 2003
Quote:
Originally Posted by jjshammas View Post
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:@"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.
__________________
iMac 2013 27", 13" rMBP, iPad 4, iPhone 5s
Krevnik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Objects in Objective C luke71933 Mac Programming 6 Apr 8, 2014 08:20 AM
Resolved: Learning objective-C? Curax iPhone/iPad Programming 4 Sep 7, 2013 11:28 AM
Objective C question farmerdoug Mac Programming 4 Sep 16, 2012 02:44 PM
Problems with Objective-C++? frank100 Mac Programming 5 Aug 8, 2012 04:49 PM
Testing objective-c Evre iPhone/iPad Programming 21 Jul 12, 2012 07:22 AM

Forum Jump

All times are GMT -5. The time now is 09:04 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC