Updating a text field from the URL from WebKit - cocoa

First off let me say that I am VERY new to COCOA but I am learning. I have a simple web browser built. It loads a default page, takes URLs from a text field, even has google search. The problem I'm running in to is updating the textfeild with the current URL. I have the code as follows...
In the .h is...
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface MustangAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSTextField *googleSearch;
WebView *webView;
NSTextField *addressBar;
}
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic, retain) IBOutlet NSTextField *googleSearch;
#property (nonatomic, retain) IBOutlet WebView *webView;
#property (nonatomic, retain) IBOutlet NSTextField *addressBar;
- (IBAction)googleSearch:(id)sender;
- (IBAction)homeAction:(id)sender;
- (IBAction)addressBar:(id)sender;
#end
In the .m I have a bunch so I'll put in what I think is relevant here...
#import "MustangAppDelegate.h"
#implementation MustangAppDelegate
#synthesize window;
#synthesize googleSearch;
#synthesize webView;
#synthesize addressBar;
- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
NSURLRequest *currentRequest = [webView request];
NSURL *currentURL = [currentRequest URL];
addressBar.stringValue = currentURL.absoluteString;
}
The code for the above method never get's called. Like I said, I'm very new to OBC & Cocoa but have been programming in things like Perl for many years. Seems like I'm missing some connectivity because I get a warning with the above code that states WebView may not respond to the request. I dug around to find that code but it still seems like I'm missing something...
Thanks
Mike

Just CTRL + drag the -frameLoadDelegate from the IB to the App Delegate of your application, Mustang
use -didFinishLoadForFrame for the URL
and if you need the title use, -didRecieveTitle.
Hope it helped !

Related

Parse Issue, #end must appear in context

I am trying to build this app and every time I try to build it, it always gives me a parse error.
#import "XYZFlipsideViewController.h"
#end <===This is the error. It says: '#end' must appear in an Objective-C context
#interface XYZMainViewController : UIViewController <XYZFlipsideViewControllerDelegate,
UIPopoverControllerDelegate>
#property (strong, nonatomic) IBOutlet UIWebView *viewWeb;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (strong, nonatomic) UIPopoverController *flipsidePopoverController;
#end
I have tried to delete the first
#end
but it always gives me a lot more errors.

Arc Error ?? Existing ivar delegate for unsafe_unretained property

I am currently following the tutorials in a book that was written before the use of arc in Xcode. I have the exact same problem like what is stated here:
'Existing ivar 'delegate' for unsafe_unretained property 'delegate' must be __unsafe_unretained
The question has been answered, but is has not been stated exactly why this is the case.
The Code in the book states that I use this code to declare member variables:
the.h File
___________
#import <Cocoa/Cocoa.h>
#interface TestProgramDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSTextField *message;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSTextField *message;
#property (assign) IBOutlet NSTextField *inputText;
#end
the.m file
__________
#import "the.h" //File
#implementation theHFileAppDelegate
#synthesize window
#synthesize message
#synthesize inputText
When I synthesize the variables in my .m file, I get the following error message:
Existing ivar 'window' for property 'window' with assign attribute must be __unsafe_unretaied.
But when I remove the declaration of the member variables like its stated in the book, everything works fine and I don't get an error.
the.h File
___________
#import <Cocoa/Cocoa.h>
#interface TestProgramDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSTextField *message;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSTextField *message;
#property (assign) IBOutlet NSTextField *inputText;
#end
the.m file
__________
#import "the.h" //File
#implementation theHFileAppDelegate
#synthesize window
#synthesize message
#synthesize inputText
My program works, but I want to understand why this causes trouble. Thanks a lot.

Trying to use KVC in Xcode to update a TextLabel

I've got a TextField that I'm trying to update the values as it progresses through running my code using KVC. Unfortunately I cannot seem to get anything to update past the initial value.
I've used the bindings on the button that launches the code, the TextField that I want to update and it just doesn't want to update. Please forgive me for the n00bish question but I've been searching online all day, going through tutorials, rewriting the code different ways and can't seem to figure out why this very simple tasks won't work.
Here is my KVC.h file:
#import <Foundation/Foundation.h>
#interface KVC : NSObject{
NSString *_progressString;
}
#property (nonatomic, retain) NSString *progressString;
#end
Here is my App header file:
#import <Cocoa/Cocoa.h>
#import "KVC.h"
//UI Controls
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
NSWindow *window;
NSPersistentStoreCoordinator *__persistentStoreCoordinator;
NSManagedObjectModel *__managedObjectModel;
NSManagedObjectContext *__managedObjectContext;
NSButton *_loadingExtracts;
NSButton *_processStuff;
NSProgressIndicator *_progressBar;
KVC *myProgressString;
}
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (assign) IBOutlet NSButton *loadingExtracts;
#property (assign) IBOutlet NSButton *processStuff;
#property (assign) IBOutlet NSProgressIndicator *progressBar;
- (IBAction)saveAction:(id)sender;
- (IBAction)loadingExtracts:(id)sender;
- (IBAction)processStuff:(id)sender;
#end
And finally, here is the function inside the code that I cannot get to update.
- (IBAction)processStuff:(id)sender
{
KVC *frickenHeck = [[KVC alloc] init];
NSLog(#"Button Pressed - Processing Information");
[myProgressString setValue:#"Testing" forKey:#"_progressString"];
[_progressBar setUsesThreadedAnimation:YES];
[_progressBar startAnimation:self];
//Turn off Progress Bar
[_progressBar stopAnimation:self];
[frickenHeck setValue:#"Completed" forKey:#"_progressString"];
//[_progressText setStringValue:#""];
}
(As you can see, I've tried updating 2 different ways and neither work. The allocation seems to set up the initial variable just fine, the Log shows I'm in the method, just can't get my label to update past the allocation).
Any thoughts or ideas would be greatly appreciated.
I am more of a iOS developer, so I am not 100% certain how some of the standards cross-over to the OSX side of things. Nevertheless,
You are saying that you are using KVC, like it is a framework providing a easy to use key-value coding scheme, which doesn't necessarily make sence. This appears to be a custom class you created called KVC.
In your KVC.m file did you #synthesize this variable?
#implementation KVC
#synthesize progressString = _progressString;
You have defined your object frickenHeck as class KVC which holds a property called progressString, that you are making available to other classes via this call.
#property (nonatomic, retain) NSString *progressString;
Assuming you have synthesized the variable in your #implementation file, why don't you just call:
frickenHeck.progressString = #"Testing";
or
frickenHeck.progressString = #"Completed";
Sure you can set the variable via Key Value Coding, but not by setting to the private form of your class variable. Try:
[frickenHeck setValue:#"Completed" forKey:#"progressString"];
So, have you synthesized this variable? Or at least set a accessor setter/getter for the variable? You typically only want to do one or the other.
Header:
- (void)progressString;
- (NSString *)setProgressString:(NSString *)_string;
Implementation:
- (void)progressString {
return _progressString;
}
- (NSString *)setProgressString:(NSString *)_string {
_progressString=_string;
}
Also to note that if you are going to be changing this variable a lot, you may want to use the NSMutableString form of the class, and set the #property declaration to copy. #property (nonatomic, copy) NSMutableString *progressString;
I hope some of this information assists you on your journey..
Mark

Xcode Storyboard - Transfer of data

I'm new to storyboarding so the answer to this may be simplistic,
I've created a viewController in which a UITextField is present. My test is to transfer the data (text) from that text field into a viewController that is pushed onto the screen.
The coding that i have is as follows:
ViewController1.h -
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#interface ViewController1 : UIViewController
#property (nonatomic, retain) IBOutlet UITextField *inputText;
#end
ViewController1.m
#import "ViewController1.h"
#implementation ViewController
#synthesize inputText;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showTextController"]) {
ViewController2 *vc2 = [segue destinationViewController];
vc2.selectedText.text = self.inputText.text;
}
}
#end
ViewController2.h -
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
#property (nonatomic, retain) IBOutlet UILabel *selectedText;
#end
ViewController2.m
#import "ViewController2.h"
#implementation ViewController2
#synthesize selectedText;
#end
The segue between viewController1 and 2 in storyboard is referred to as 'showTextController'.
Is this the correct coding for something so simple? Do i need to be using 'ViewDidLoad' method along with the prepareForSegue:sender method?
It looks correct to me. In fact, this is simpler than we have had in the past since the storyboard takes care instantiating our view controller objects. One thing to note is that if you're using ARC then you shouldn't be retaining your UILabel.
Hope this helps.

objective C: may not respond to (issue)

I have two UINavigationController in the appdelegate.h
{
UINavigationController *leftView;
UINavigationController *rightView;
UIWindow *window;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *leftView;
#property (nonatomic, retain) IBOutlet UINavigationController *rightView;
appdelegate.m
#synthesize leftView;
#synthesize rightView;
then in a different class
test.m
#import "appdelegate.h"
if I do:
[self leftView] pushViewController...; //(Everything is ok)
but if I change it to:
[self rightView] pushViewControll...; //it complains about ViewController may not respond to -rightView
You code seems right; what doesn't seem right is that you're including AppDelegate. If the code was in AppDelegate.m, it would probably work without any complaints by the compiler.
Instead, you've defined #property lines for each, but that is in AppDelegate.h, not test.h - but you're using them in test.m. That is probably the source of the problem.
Interestingly, does the code actually run?
I'm going to hazard a guess: Have you declared and synthesized a property for rightView, or at least provided your own manual getter?
It sounds like you declared a #property or method called leftView that returns the leftView ivar, but you forgot to do the same for rightView. The error is telling you that self doesn't respond to the selector rightView.

Resources