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 Apr 29, 2011, 04:59 PM   #1
adcx64
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
Button to change page in web view?

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!
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty
adcx64 is offline   0 Reply With Quote
Old Apr 29, 2011, 05:40 PM   #2
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: 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.
jiminaus is offline   0 Reply With Quote
Old May 1, 2011, 03:57 PM   #3
adcx64
Thread Starter
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
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...
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty

Last edited by adcx64; May 1, 2011 at 04:02 PM.
adcx64 is offline   0 Reply With Quote
Old May 1, 2011, 04:15 PM   #4
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: 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];
jiminaus is offline   0 Reply With Quote
Old May 1, 2011, 04:36 PM   #5
adcx64
Thread Starter
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
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....
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty
adcx64 is offline   0 Reply With Quote
Old May 1, 2011, 04:37 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by jiminaus View Post
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/m...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...".


Quote:
Originally Posted by adcx64 View Post
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 by chown33; May 1, 2011 at 04:45 PM.
chown33 is offline   0 Reply With Quote
Old May 1, 2011, 04:48 PM   #7
adcx64
Thread Starter
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
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.........
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty
adcx64 is offline   0 Reply With Quote
Old May 1, 2011, 05:17 PM   #8
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by chown33 View Post
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.


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.
jiminaus is offline   0 Reply With Quote
Old May 1, 2011, 05:19 PM   #9
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by adcx64 View Post
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.
chown33 is offline   0 Reply With Quote
Old May 1, 2011, 06:00 PM   #10
adcx64
Thread Starter
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
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
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty
adcx64 is offline   0 Reply With Quote
Old May 1, 2011, 06:12 PM   #11
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by adcx64 View Post
Thanks for the help guys, any suggestions for a book to get?
See the Mac Guides at the top of the forum page:
http://forums.macrumors.com/forumdisplay.php?f=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.

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

Quote:
... 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.

Quote:
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?
chown33 is offline   0 Reply With Quote
Old May 1, 2011, 06:20 PM   #12
adcx64
Thread Starter
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
3.1.4 is the newest one for my mac.

in my .m file it says

implementation @nsbutton

your code you gave me

@end
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty
adcx64 is offline   0 Reply With Quote
Old May 1, 2011, 06:55 PM   #13
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by adcx64 View Post
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:
http://forums.macrumors.com/showthread.php?t=747660

Accuracy is critical in programming. Small differences can have large consequences. Paraphrasing code or retyping "shortened" versions of code is inherently inaccurate.
chown33 is offline   0 Reply With Quote
Old May 1, 2011, 07:12 PM   #14
adcx64
Thread Starter
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
Code:
 #import "NSButton.h"

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

@end
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty
adcx64 is offline   0 Reply With Quote
Old May 1, 2011, 07:27 PM   #15
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by adcx64 View Post
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 by chown33; May 1, 2011 at 07:36 PM.
chown33 is offline   0 Reply With Quote
Old May 1, 2011, 07:51 PM   #16
adcx64
Thread Starter
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
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!
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty
adcx64 is offline   0 Reply With Quote
Old May 1, 2011, 09:35 PM   #17
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by adcx64 View Post
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 by jiminaus; May 1, 2011 at 09:46 PM.
jiminaus is offline   0 Reply With Quote
Old May 2, 2011, 01:51 PM   #18
adcx64
Thread Starter
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
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?
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty
adcx64 is offline   0 Reply With Quote
Old May 2, 2011, 02:17 PM   #19
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by adcx64 View Post
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.


Quote:
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.
chown33 is offline   0 Reply With Quote
Old May 2, 2011, 03:00 PM   #20
adcx64
Thread Starter
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
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?
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty
adcx64 is offline   0 Reply With Quote
Old May 2, 2011, 05:42 PM   #21
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by adcx64 View Post
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.]
jiminaus is offline   0 Reply With Quote
Old May 2, 2011, 07:21 PM   #22
adcx64
Thread Starter
macrumors 65816
 
adcx64's Avatar
 
Join Date: Nov 2008
Location: Cudi Zone
I guess I'll just have to do that! thanks for all the help.
__________________
iPhone 5S, iOS 7, Verizon LTE
Xbox One (Titanfall Alpha!)
Competitive Call of Duty
adcx64 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
How do you view the web view of twitter on the iPhone? Tsunam Apple, Industry and Internet Discussion 3 May 1, 2013 08:36 PM
Pages '09/iWork '09: Web View mode / view all pages as one page in Pages boyscoutslayer Mac Applications and Mac App Store 0 May 1, 2013 07:48 AM
Change page view in Pages htg Mac Basics and Help 2 Nov 28, 2012 03:00 PM
Web Express! Offline Web Page Reader for iOS ProcypherApps iPhone and iPod touch Apps 2 Jul 20, 2012 10:00 AM
is there a way to view podcasts directly from a web page? bobright iPad 1 Jun 7, 2012 02:41 PM

Forum Jump

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

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

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