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

adcx64

macrumors 65816
Original poster
Nov 17, 2008
1,270
124
Philadelphia
Hi,
I am using Xcode to make a simple browser. How can I link a button up so that when it is pressed it changes the web view to "example.com" thank you!
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
The BYOB tutorial at http://terabook.net/byob-tutorial.html will show you what you need. There also video tutorials on doing creating simple web browsers on YouTube. Google is your friend.

But before you go making a web browser, have you learned the at least the basics of Cocoa and Objective-C programming? While you can create a simple web browser without a single line of code using just Interface Builder, you inevitably will want to go beyond that, and for that you'll need a reasonable foundation in Cocoa and Objective-C.
 

adcx64

macrumors 65816
Original poster
Nov 17, 2008
1,270
124
Philadelphia
Bump. I've searches google endlessly, but I cannot find an answer. All I want to do is when I click a button on my web browser I made, it will change the web view to say "google"................ How can I do this? I basicly, want to map a URL to a button...
 
Last edited:

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
You probably won't find an example matching your specific need. But you should have found many examples where a URL is typed into a text field and a Go button causes that URL to be loaded into the web view. Surely you could have adapted that?

Assuming your WebView is linked to an IBOutlet called webView, in an IBAction linked to your button, this is the code to load Google into a WebView.
Code:
NSURL* url = [NSURL URLWithString:"http://www.google.com/"];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
[webView.mainFrame loadRequest:req];
 

adcx64

macrumors 65816
Original poster
Nov 17, 2008
1,270
124
Philadelphia
Can you post step by step instructions please? i just copied and pasted the code u wrote into my "google.m" file and it gave me some errors on build. I am a newbie to programming. sorry for the hassle....
 

chown33

Moderator
Staff member
Aug 9, 2009
10,743
8,417
A sea of green
Assuming your WebView is linked to an IBOutlet called webView, in an IBAction linked to your button, this is the code to load Google into a WebView.
Code:
NSURL* url = [NSURL URLWithString:"http://www.google.com/"];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
[webView.mainFrame loadRequest:req];

That code sequence can be simplified to:
Code:
[webView setMainFrameURL:@"http://www.google.com/"];
According to the WebView class reference doc:
http://developer.apple.com/library/...lasses/WebView_Class/Reference/Reference.html
This method is functionally equivalent to invoking [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:URLString]]].

Also, you have a bug here:
Code:
NSURL* url = [NSURL URLWithString:"http://www.google.com/"];
It's missing an @ before "http...".


Can you post step by step instructions please? i just copied and pasted the code u wrote into my "google.m" file and it gave me some errors on build. I am a newbie to programming. sorry for the hassle....
Post the actual error messages.
Don't paraphrase or retype them. Copy and paste them into a post.

I can probably guess what the error is, based on what I wrote above. However, you should get in the habit of posting error messages, so please do that.

Finally, I'm guessing that you're not following a tutorial, but are instead making things up by yourself. I don't recommend this. You should follow a book or tutorial, so you understand what you're doing. Copying and pasting code isn't programming. And as much as one might admire learning stuff entirely on one's own, think of it this way: Would you trust a teacher who has only rudimentally skill and no actual experience in the technical field they're teaching?
 
Last edited:

adcx64

macrumors 65816
Original poster
Nov 17, 2008
1,270
124
Philadelphia
how do i implement the code that you provided into xcode? i was selecting the button in interface builder and clicking " write class files" then copying and pasting the code into the nsbutton.h file.........
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
That code sequence can be simplified to:
Code:
[webView setMainFrameURL:@"http://www.google.com/"];

Also, you have a bug here:
Code:
NSURL* url = [NSURL URLWithString:"http://www.google.com/"
It's missing an @ before "http...".

The code was copy'n'pasted from my own SimpleBrowser project. I used the expanded sequence because I do some URL corrections using NSURL, which is missing from the above code.

I get the URL from text field and I just replaced that with the string. I'm surprised I forgot the @, I thought that habit had become sufficiently entrenched. :D


EDIT: Like chown33 you need to do some leg work. Get a good book. At the very least, following through the Cocoa Application Tutorial. And don't use XCode 4 at this stage. There are many more resources, including the Cocoa Application Tutorial, that have instructions only from XCode 3.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,743
8,417
A sea of green
how do i implement the code that you provided into xcode? i was selecting the button in interface builder and clicking " write class files" then copying and pasting the code into the nsbutton.h file.........

This is why you should follow a book or tutorial. It will tell you exactly what to do.

First, you haven't said which version of Xcode you're using. The exact step-by-step procedure may well be different for different versions of Xcode.

Second, you haven't said whether you're following some sort of tutorial, possibly an old one, which doesn't match your version of Xcode.

Third, it's clear you don't understand the difference between a header (.h) file and an implementation (.m) file. This important distinction is something that a book or tutorial would point out very early on.

Overall, you're already in over your head, you don't seem to be following any guide or tutorial, you don't seem to have read (or be referring to) any Xcode documentation or guides, and you are essentially asking for someone to make you a customized tutorial to do a specific task.

This is why tutorials and books exist: to provide well-grounded and consistent guidance on the fundamentals, without having to create (and test and revise) a custom tutorial for every single person who wants to learn the fundamentals.

Short answer: get a book and follow it.
 

adcx64

macrumors 65816
Original poster
Nov 17, 2008
1,270
124
Philadelphia
Thanks for the help guys, any suggestions for a book to get? Also, so I can finish this project, do I just add the code you provided Into My nsbutton .m file?
Thanks for all of your help!:)
Also, I'm using Xcode 3.1.4
 

chown33

Moderator
Staff member
Aug 9, 2009
10,743
8,417
A sea of green
Thanks for the help guys, any suggestions for a book to get?

See the Mac Guides at the top of the forum page:
https://forums.macrumors.com/forums/73/

If you're using the G4 iBook listed in your sig, you may have difficulty finding a recent book to use. You should look at previews of any book before buying it, to make sure it matches your computer and your OS and Xcode versions.

Also, so I can finish this project, ...
Simply adding the posted may not be sufficient to finish the project.

... do I just add the code you provided Into My nsbutton .m file?
Impossible to say, because you haven't shown us the contents of any of your files.

Post your code and we may be able to improve our answers.

Also, I'm using Xcode 3.1.4
That's a fairly old release, and contains a number of bugs which have been fixed in later versions.

You should be using the latest of the Xcode 3.2 releases, if they run on your computer.

Do you know where to get Xcode 3.2 downloads, or do you need specific directions on where to go and what to do?
 

adcx64

macrumors 65816
Original poster
Nov 17, 2008
1,270
124
Philadelphia
3.1.4 is the newest one for my mac.

in my .m file it says

implementation @nsbutton

your code you gave me

@end
 

chown33

Moderator
Staff member
Aug 9, 2009
10,743
8,417
A sea of green
in my .m file it says

implementation @nsbutton

your code you gave me

@end

That's wrong, but I can't tell if it's because your actual code is wrong, or because you're paraphrasing it inaccurately.

Always copy and paste complete actual code, exactly as it appears in your .m file. Also surround your posted code with CODE tags:
https://forums.macrumors.com/threads/747660/

Accuracy is critical in programming. Small differences can have large consequences. Paraphrasing code or retyping "shortened" versions of code is inherently inaccurate.
 

adcx64

macrumors 65816
Original poster
Nov 17, 2008
1,270
124
Philadelphia
Code:
 #import "NSButton.h"

@implementation NSButton
[webView setMainFrameURL:@"http://www.google.com/"];

@end
 

chown33

Moderator
Staff member
Aug 9, 2009
10,743
8,417
A sea of green
Code:
 #import "NSButton.h"

@implementation NSButton
[webView setMainFrameURL:@"http://www.google.com/"];

@end

There are so many problems with this, I'm just going to list the problems. If you don't understand why it's a problem, that's just more reason why you need to use a book and start with the fundamentals.

Problems:

1. You haven't posted the code for your "NSButton.h". A .m file is only half of the total picture of any class.

2. You can't use the same name as a standard Apple-supplied class: NSButton is already used, and by reusing that name you are guaranteed to fail. Every class name must be unique.

3. You haven't put the code into a method or function definition. All executable code must be defined in the body of a method or function, between { and } that delimit the body of the method or function. If you don't know what a method or function definition is, then you won't know what that means. If I were to try explaining what it means, I would be repeating what a book or tutorial provides. Hence, the reason for following a book or tutorial.

4. You haven't shown the declaration for a webView variable. It might be in your "NSButton.h" file, or it might not. Impossible to say without seeing your .h file. If webView isn't declared in that file, then you don't have an actual variable of that name, so you'd need one before you can use it. Again, if you don't know what that means, you don't have the fundamentals, and any explanation I could give would repeat what a book or tutorial provides.

5. If you have a declared webView variable in your .h file, it will need to be an IBOutlet, otherwise it won't be connected to anything in your nib/xib file. If you don't know what an IBOutlet is, you're missing an important and fundamental part of the relationship between nib/xib files and the code residing in .h and .m files. And again, this would be explained by any book or tutorial that steps through fundamentals.

6. It turns out you paraphrased the code inaccurately:
Code:
@implementation NSButton
is completely different from:
Code:
implementation @nsbutton
for the following reasons:
a. The placement of @ is different, and very important.
b. Class names (indeed, all names) in Objective-C are case-sensitive. NSButton, NSbutton, nsButton, and nsbutton are 4 completely different names. One of these is an Apple-defined name; all the others are not.


None of this is to discourage you from learning how to program. Rather, it is to encourage you to follow a guided approach using a book or tutorial that starts with the fundamentals. You wouldn't expect to hop in the cockpit of an F-15 (or even a basic Cessna) and fly it without any guidance or training. Learning to program is at least as complex a task as learning to fly a real aircraft. One difference is that the consequences of a crash are substantially less fatal in programming.
 
Last edited:

adcx64

macrumors 65816
Original poster
Nov 17, 2008
1,270
124
Philadelphia
i really need to get that book ahahahah i had no idea what you stated.
my .h file was same as my .m file.... thank you for all of your help and i will definately get a book! i never thought it would be so hard to map a button to a URL for webview!
Anyways,
Thanks for all the help!
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
i really need to get that book ahahahah i had no idea what you stated.
my .h file was same as my .m file.... thank you for all of your help and i will definately get a book! i never thought it would be so hard to map a button to a URL for webview!
Anyways,
Thanks for all the help!

It's not hard. Actually it's very easy. It seems hard to you because you've not got any of the foundation to understand what's required.

I gather you have no previous programming experience. If that's the case, let me recommend Wang (2011) Mac Programming for Absolute Beginners, Apress, followed by Kochan (3e) (when it comes out) and Hillegass (3e).
 
Last edited:

adcx64

macrumors 65816
Original poster
Nov 17, 2008
1,270
124
Philadelphia
Thanks a lot I will definatly get that book!
However, I am still curious about the question I had.
Why isn't Xcode like Visual Basic where you can just tell the button what you want it to do without those .h and .m files?
Can I copy and paste the code you provides into the .h and .m files of my NSButton and expect it to work?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,743
8,417
A sea of green
Why isn't Xcode like Visual Basic where you can just tell the button what you want it to do without those .h and .m files?
Because Objective-C isn't Visual Basic. There is no language supported by Xcode that is "like" Visual Basic in that sense.

There are other languages that are more like Visual Basic, such as REALBasic, but you don't get 'em for free.


Can I copy and paste the code you provides into the .h and .m files of my NSButton and expect it to work?
No, for two reasons:
1. You can't name your class NSButton. It needs a different name that isn't the same as an existing class.
2. You weren't given complete copy-and-pastable code. You were given code fragments, on the assumption that you had sufficient skill to put them in the proper place in your code. You didn't even have proper code to paste into, because of the NSButton naming issue, and it's now clear you don't yet have the skill to paste it in at the proper place.

I suggest learning the fundamentals well enough that you can come back to this task at a later time, after you know more about the language, target-action for buttons, and about WebView itself. If solving the problem is still difficult or not obvious, then you haven't yet learned enough to attempt it. After you've learned enough, I'm pretty sure the solution will become obvious, even trivial. But it will only become so when the fundamental knowledge is in place.
 

adcx64

macrumors 65816
Original poster
Nov 17, 2008
1,270
124
Philadelphia
how about filling me in on how to do so instead of just telling me that i do not know the language, which i dont, but until i get a book can't you just tell me how to implement the said code? lol. I never knew it would be this complicated to just make a button change a URL. Would it work if I wrote the button class files as "google" so the .m and .h fies would be google.m and .h and then added the code, since i cannot keep it as NSbutton?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
how about filling me in on how to do so instead of just telling me that i do not know the language, which i dont, but until i get a book can't you just tell me how to implement the said code? lol. I never knew it would be this complicated to just make a button change a URL. Would it work if I wrote the button class files as "google" so the .m and .h fies would be google.m and .h and then added the code, since i cannot keep it as NSbutton?

Why don't you put this project aside for now, follow through the Cocoa Application Tutorial that Apple provides, then you'll have a better idea where to put the code and how to link it up in Interface Builder.

[HINT: You don't need to create a new class, new .m file or new .h file. The codes goes into your application delegate.]
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.