Segmented control to change pages in UIWebView - objective-c

I've been going through example after example trying to compare my project with others to see what I'm doing incorrectly however I have yet to find the answer. In short what I'm attempting to do for class is have a three button segmented control that will change the url that a UIWebView displays.
However, when the app is ran it crashes on launch with the following console output
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x7f969ab2dd80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key appleWebView.'
*** First throw call stack:
The following is my code:
ViewController.M
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIScrollView *theScroller;
#property (weak, nonatomic) IBOutlet UISegmentedControl *appleSite1;
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.theScroller.contentSize = CGSizeMake(280, 1000.0);
}
- (IBAction)appleSite1:(UIButton *)sender
{
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://www.google.com"]];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
//{
// IBOutlet UIWebView myWebView;
//}
//
#end
I'm only testing with the one site (ebay) until I can figure out exactly what I'm missing. Obviously I'm a student and am learning so please bear with me.
Thanks in advance.

I don't know if you already solve your problem...
The crash is maybe caused by Interface Builder which is linking your UIWebView to a property (IBOutlet) named appleWebView. I think that you renamed appleWebView to myWebView.
To fixe this open the controller's xib (or storyboard), select the UIWebView, in the right panel open the connections inspector and remove the link to appleWebView.
Hope it helps!

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)

Why am I getting an error say this class is not KVC compliant for the key listingDescription? [duplicate]

This question already has answers here:
Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?
(79 answers)
Closed 7 years ago.
I am getting the following errors:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<AddListingViewController 0xe603940> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key listingDescription.'
by the way, listingDescription is in another class and it looks like this:
#property (nonatomic, strong) NSString *listingDescription;
Here is my AddListingViewController.h
#import <UIKit/UIKit.h>
#import "ListingTableViewController.h"
#import "ListingManager.h"
#interface AddListingViewController : UIViewController
#property (nonatomic) ListingManager *manager;
#end
Here is the AddListingViewController.m
#import "AddListingViewController.h"
#interface AddListingViewController ()
#property (weak, nonatomic) IBOutlet UITextField *title;
#property (weak, nonatomic) IBOutlet UITextView *description;
#property (weak, nonatomic) IBOutlet UITextField *price;
#end
#implementation AddListingViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//cancel posting on tap
- (IBAction)cancelListing:(UIBarButtonItem *)sender {
NSLog(#"cancel tapped thpugh");
[self dismissViewControllerAnimated:YES completion:nil];
}
//add item on tap
- (IBAction)addListing:(UIBarButtonItem *)sender {
NSLog(#"Add button tapped");
}
#end
Not sure why I am getting the error. Whenever I click the bar button "add" to go to the "AddListingViewController" view controller, I get the error. Looked around but their answers didn't satisfy my needs.
Not likely a related problem, but you have conflict between NSObject protocol's 'description' method and this:
#property (weak, nonatomic) IBOutlet UITextView *description;
This may lead to problems while debugging/logging out class objects.
Other than that I would track when you call 'listingDescription', or use any KVO-related mechanisms using that as a key.
Looked around as per nhgrif's comment above. The textboxes were referencing 2 other things (previously set and forgot to remove). Silly me. Thanks everyone for answering

Receiver type 'WebFrame' for instance message is a forward declaration

I'm learning ObjC and cocoa dev and have come across a real 'stumper'. Having exhausted Google, I respectfully adorn my desperation hat and present to you:
A class and a view controller:
The class 'Content Window' imports a viewcontroller instance and places it in a window:
ContentWindow.h
#import <Foundation/Foundation.h>
#import <WebKit/WebView.h>
#import "ContentViewController.h"
#interface ContentWindow : NSWindow{
ContentViewController* viewController;
}
#property IBOutlet ContentViewController* viewController;
-(NSWindow *) newWindow;
#end
ContentWindow.m
#import "ContentWindow.h"
#implementation ContentWindow
#synthesize viewController;
-(NSWindow *) newWindow{
//Builds the window as 'window' and displays it successfully here
//... [code redacted for brevity]
// Build view
viewController = [[ContentViewController alloc] initWithNibName:#"ContentViewController" bundle:nil];
[window setContentView: viewController.view];
NSString *urlString = #"http://www.google.com";
[[viewController.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
[viewController.title setStringValue:#"my title"];
}
#end
I am attempting to do two things with the interface:
[viewController.title setStringValue:#"my title"];
This successfully sets the view element 'title' to "my title".
[[viewController.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
This, however, throws the error:
Receiver type 'WebFrame' for instance message is a forward declaration.
and underlines in red the section of the line:
viewController.webView mainFrame
My view controller is as follows:
ContentViewController.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>
#interface ContentViewController : NSViewController {
IBOutlet NSTextField *title;
IBOutlet WebView *webView;
}
#property IBOutlet WebView *webView;
#property IBOutlet NSTextField *title;
#end
ContentViewController.m
#import "ContentViewController.h"
#interface ContentViewController ()
#end
#implementation ContentViewController
#synthesize title, webView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
#end
Finally to use this class, I am instantiating a content window from my AppDelegate class with
contentWindow = [[ContentWindow new] newWindow];
Having imported ContentWindow.h into AppDelegate.h and having set:
__strong NSWindow * contentWindow
as an AppDelegate synthesised instance variable.
I have linked both items in IB (definitely!) I have also added Webkit foundation to my project, which was suggested in another thread.
I can't for the life of me understand what is going on. I know that the logical answer is to put down Xcode and pick up the 'Learn Xcode and Objective c' book (with a bookmark about half way through where I was arrogant enough to think I'd learned enough to try something out), but before I do that, on the off-chance:
Could anyone help?
Thanks as always, AtFPt.
Usually this error message means, that the type of a class is not know (since declared by #class).
Make sure, that your code can see a declaration of WebFrame.
If so, maybe you add it later and XCode works with older meta data. In this case, a clean before build usually helps.

Obejct-C / Xcode Not able to add/change text in UITextView

For some reason, i'm not able to change or add text in my UITextView from code.
I have made Outlet and connected (All in IB) - nothing happens.
Even tried to add -setNeedsDisplay
Do i need to set some property ? - This i driving me nuts....
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextViewDelegate> {
UITextView *textInfoView;
}
#property (nonatomic, retain) IBOutlet UITextView *textInfoView;
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
textInfoView.text = #"Test";
}
-(void)viewDidAppear:(BOOL)animated{
textInfoView.text = #"Test";
textInfoView.text = [textInfoView.text stringByAppendingString:#"Line2"];
[textInfoView setNeedsDisplay];
It seems, really strange that this code doesn't update the text inside the UITextView. I have tried and it works for me ... I think that you don't have correctly linked the Outlet in interface builder, or you don't have associated your view controller to your view ...
UPS - Got it...
Forgot to #synthesize...
For some reason i did not get warning or "setter error" in Xcode 4.4, maybe it's a version bug ?

Uncaught exception when selecting one particle view

I'm building an app but having a problem when i try and open a second view.
The code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSInteger FontSizeValue = [[NSUserDefaults standardUserDefaults] integerForKey:#"FontSize"];
UIFont *pagedefault = [UIFont fontWithName:#"Arial" size:FontSizeValue];
Page1Text.font = pagedefault;
Page2Text.font = pagedefault;
}
Output:
2012-03-16 11:16:11.573 Accessibility Guide[3418:f803] *** Terminating app due to
uncaught exception 'NSUnknownKeyException', reason: '[<TextViewController 0x687e4d0>
setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
Page2.'
*** First throw call stack:
(0x13cd022 0x155ecd6 0x13ccee1 0x9c5022 0x936f6b 0x936edb 0x951d50 0x23971a 0x13cedea
0x13387f1 0x23826e 0xde1fc 0xde779 0xde99b 0xded11 0xf08fd 0xf0aef 0xf0dbb 0xf185f
0xf1e06 0xf1a24 0x44dde6 0x4424d0 0xa8581 0xa87fa 0x93d85d 0x13a1936 0x13a13d7 0x1304790
0x1303d84 0x1303c9b 0x12b67d8 0x12b688a 0x17626 0x233d 0x22a5)
terminate called throwing an exception(lldb)
Feel free to request any more information. I'm still fairly new to objective-c so I'll do my best.
NOTE: I am using one view controller file (TextViewController.h TextViewController.m) for two views (i only have two views so far but there will be more) because all the views look the same but they all share the same code. Each view has a "Next Button" and a "Text Box"
TextViewController.h
#import <UIKit/UIKit.h>
#interface TextViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextView *Page1Text;
#property (weak, nonatomic) IBOutlet UITextView *Page2Text;
#end
TextViewController.m
#import "TextViewController.h"
#interface TextViewController ()
#end
#implementation TextViewController
#synthesize Page1Text;
#synthesize Page2Text;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSInteger FontSizeValue = [[NSUserDefaults standardUserDefaults] integerForKey:#"FontSize"];
UIFont *pagedefault = [UIFont fontWithName:#"Arial" size:FontSizeValue];
Page1Text.font = pagedefault;
Page2Text.font = pagedefault;
}
- (void)viewDidUnload
{
[self setPage1Text:nil];
[self setPage2Text:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
This seems to happen when some component, like a XIB file, expects your class to have an accessor for a key that it can't find. Are you binding something to Page2?
Similar questions here and here.
What is Page1Text? (and Page2Text)
Is it an instance of your TextViewController? (if so.. please.. variables starts with lower case letter)
And.. if it is so.. Does your TextViewController class has a property names font?
#interface TextViewController : UIViewController
#property (nonatomic, strong) UIFont* font;
#end