Working with two windows - objective-c

I am new to programming specially objective c.
I have a two windows in MainMenu.xib.
1st window (with webview) is launching when i run my application.
I am able to launch 2nd window (with a text field and save button) from top menu.
I have specified a default URL as "www.google.com" for first launch.
Now i want to configure URL by putting it in textfield on 2nd window. Whenever i click "SAVE" 1st window should refresh with newly given URL.
How is it possible and how i can connect action button "SAVE" that it impact on 1st window.
Here is my code
AppDelegate.h contains
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet WebView *webview;
#property (assign) IBOutlet NSTextField *urlString;
- (IBAction)save:(id)sender;
#end
**And my AppDelegate.m contains**
#implementation AppDelegate
#synthesize webview;
#synthesize urlString;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *urlString = #"http://www.google.com";
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
-(IBAction)save{
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString.stringValue]]];
}
#end
Thanks

You need to open the Save Menu and Ctrl+Drag to your class.
Set the action to your save or create a new one.
(Either call your method from IBAction method created by XCode or put your code there.)

Related

How to load specific url in Webview programmatically?

I am trying to make a basic search tool in cocoa. I am trying to take the string from a textfield, edit it to make a link that searches the string in google, and then feed that into the WebView. However, Xcode is not letting me use the variable I made for the Web View. I'm getting errors along the lines of "Unknown type name 'WebView'" and "Bad Receiver type '*int'". Any help would be appreciated. Here's my code for
NewWindowController.h:
#import <Cocoa/Cocoa.h>
#interface NewWindowController : NSWindowController
#property (weak) IBOutlet NSComboBox *searchselector;
- (IBAction)onclickGO:(id)sender;
#property (weak) IBOutlet WebView *webv;
#end
NewWindowController.m:
#import "NewWindowController.h"
#interface NewWindowController ()
#end
#implementation NewWindowController
#synthesize searchselector, webv;
- (void)windowDidLoad {
[super windowDidLoad];
NSLog(#"Window did load");
}
- (IBAction)onclickGO:(id)sender {
NSInteger *indexofsearch = [searchselector indexOfSelectedItem];
NSLog(#"%d",indexofsearch);
//test for web view to load page
[[webv mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: #"https://google.com"]]];
}
#end
WebView is part of WebKit.framework, which is not included in the default projects.
Add #import WebKit; to the top of whatever file you're referencing the WebView object in.
Add WebKit.framework to "Link Binary with Libraries" (in Build Phases)

mac status bar application not working

I'm following this: http://lepture.com/en/2012/create-a-statusbar-app simple tutorial to get a Status Bar based Mac app working, I've referenced Apple's NSStatusItem class reference as well - And cannot figure out what I'm doing wrong?
It's just not working. My project uses ARC.
Here's FPAppDelete.h:
#import <Cocoa/Cocoa.h>
#interface FPAppDelegate : NSObject <NSApplicationDelegate>
#property (weak) IBOutlet NSMenu *statusMenu;
#property (strong, nonatomic) NSStatusItem *statusBar;
#end
Here's FPAppDelegate.m:
#import "FPAppDelegate.h"
#implementation FPAppDelegate
#synthesize statusBar = _statusBar;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (void) awakeFromNib {
self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
self.statusBar.title = #"G";
self.statusBar.menu = self.statusMenu;
self.statusBar.highlightMode = YES;
}
#end
I'm not expecting this at all, but I get this when I run the app, with nothing in my Status Bar
It looks like you are willing to listen. So I'll show you quickly how to run a status application.
(1) Add NSMenu to the side bar (or whatever you call). (See the screenshot below.) It's up to you to keep or remove 3 generic menu items.
(2) Add the following instance variables under AppDelegate.h.
#interface AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
NSImage *statusImage;
}
#property (assign) IBOutlet NSWindow *window;
#property (strong) NSMenuItem *menuItem1; // show application
I'll also add a property as an example.
The following code is for AppDelegate.m
#implementation AppDelegate
#synthesize menuItem1;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self setMainStatus];
}
- (void)setMainStatus {
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
statusImage = [NSImage imageNamed:#"statusImage"];
[statusItem setImage:statusImage];
[statusItem setMenu:statusMenu];
NSMutableString *menuname1 = [[NSMutableString alloc] initWithString:NSLocalizedString(#"statusMenuShowApplication", #"Show Application Window")];
menuItem1 = [[NSMenuItem alloc] initWithTitle:menuname1 action:#selector(statusApplicatinClicked:) keyEquivalent:#""];
[statusMenu addItem:menuItem1];
}
- (void)statusApplicatinClicked:(id)sender {
[self.window setIsVisible:YES];
}
#end
(3) Go back to Interface Builder and connect Status Menu to the NSMenu control you've added.
The setMainStatus method (or whatever you want to name) adds menu items to the status menu programatically. First, you need to create NSStatusbar, which takes NSImage. This NSImage is used to show an icon on the status menu. Next, add menu items and separators to the status menu (status Menu in my case). I have menuItem1 as a property so that the application could enable/disable it. That's just a quick example. You can add NSView to the status menu. If you want to add a separator, it can go as follows.
[statusMenu addItem:[NSMenuItem separatorItem]];
You don't need an application window to run a status menu application. You have to show the main application window for the first time if you are going to submit your status application to Apple's Mac App Store, though.
The status bar is the top-right part of your entire screen. It's where the date and time, Spotlight, etc live in the Menu Bar.
The screenshot you posted is of the title bar of your primary NSWindow.

How to load URL on launch in a WebView (OSX project)?

I am just starting to develop mac apps and I want that the WebView a URL when the app start.Here's my code:
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate> {
WebView *myWebView;
//other instance variables
}
#property
(retain, nonatomic) IBOutlet WebView *myWebView;
//other properties and methods
#end
AppDelegate.m:
#import "AppDelegate.h"
#import <WebKit/WebKit.h>
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *urlText = #"http://google.com";
[[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
return;
// Insert code here to initialize your application
}
#end
How to load URL on launch in a WebView (OSX project)?
I think that the code work but when I try to connect the code with the WebView in Interface Builder,I can't find the "web view" in the list of outlets.
Thanks
I'have update my code following the last post but is still doesn't work.
Thanks again for you replies.
You need to add WebKit framework.
#import <WebKit/WebKit.h>
Hard to be sure what the issue is here, so a guess...
Which way are you dragging the connection in IB?
To connect your outlet you want to drag from the outlet shown in the Inspector to the web view:
If you drag in the other way, from the web view to the App Delegate in the outline you are trying to connect an action.
You also have an issue in your code, your instance variable:
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
WebView *myWebView;
//other instance variables
}
will not be used by your property:
#property (retain, nonatomic) IBOutlet WebView *myWebView;
as your property is automatically synthesised and so will create an instance variable _myWebView. You should see a compiler warning to this effect.
This in turn means the statement:
[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
will not do what you expect, as myWebView will be nil and not refer to your WebView. You should refer to the property as self.myWebView:
[[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
With those changes you should see Google in your web view.
HTH

Xcode 4.5.2 - OS X Application - Link webview to code, load hand-coded homepage

I am using Xcode 4.5.2.
I created a new project (OS X > Application > Cocoa Application).
Under linked Frameworks and Libraries I added Webkit.framework.
Under MainMenu.xib I added a Web View object to the Window
Under MainMenu.xib I added 2 Push Buttons and named them Back and Forward.
I control clicked on the buttons and linked them to the Web View and selected goBack: and goForward:
Under MainMenu.xib I added a Text Field object
I control clicked on the text field and linked it to the Web View and selected takeStringURLFrom:
All of that worked. I can enter a web address into the text field and press return and use the back and forward buttons.
I want to hand code a default website (http://www.google.com/) to load and I'm having trouble figuring out what to do from here?
What do I link my web view to (when I control click it)?
What do I need to add to my AppDelgate.h and AppDelgate.m files to load Google into the web view when the app opens?
I did some research and got this far. I was able to find a working example but I am trying to understand how it works step-by-step and am stuck on this step.
If you want your delegate to load the Google homepage after the application launches, you can simply add the following to your applicationDidFinishLaunching::
NSURL* url = [NSURL URLWithString:#"http://google.com"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[[self.webView mainFrame] loadRequest:request];
This assumes you have the following in your AppDelegate.h:
#property (weak) IBOutlet WebView *webView;
which you placed by CTRL-dragging from the WebView control in Interface Builder into your AppDelegate.h.
Final code:
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet WebView *webView;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURL* url = [NSURL URLWithString:#"http://google.com"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[[self.webView mainFrame] loadRequest:request];
}
#end

OS X app WebView not loading through code

I have spent about a whole day of grief trying to solve this question, and this is my last resort. I have a web view in my OS X app. I made a web browser and it is working great with navigation arrows and all, but when I try to make a home page (google) It does not load on the WebView (called webber in the code). I made sure all the outlets are connected. here is my code for AppDelegate.h:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>{
NSWindow *window;
NSTextField *urlBox;
IBOutlet WebView *webber;
IBOutlet NSTextField *googleSearchField;
}
-(IBAction)search;
#property (assign) IBOutlet NSWindow *window;
#end
and here is the code for AppDelegate.m:
#import "AppDelegate.h"
#implementation AppDelegate
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController{
NSLog(#"Nib loaded");
NSString *urlText = #"http://www.google.com";
[[webber mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
}
-(IBAction)search
{
NSString *searchString = [googleSearchField stringValue];
NSString *searchURL = [NSString stringWithFormat:#"http://www.google.com/search?q=%#", searchString];
[[webber mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: searchURL]]];
}
#end
I would very much appreciate ANY form of help and I am very desperate. Please remember that I am only in 6th grade, and a step by step guide or just plain code is what I am looking for. Thank you for your time! download the Xcode project here: http://www.cadenfarley.com/cobra/Download.html Scroll down until you see "Xcode project here"
Your windowControllerDidLoadNib: method is never called because you don't have an NSWindowController anywhere. Rename that method to - (void)awakeFromNib and it should work.