web view did finish load labelname.hidden = yes not working? - objective-c

I am very new to iOS programming and am making a simple app. the first UITabBarItem loads a page but before it loads there is a label. I am trying to make the label disappear after the web page loads but it doesn't work. I believe I need to set the web view delegate but I don't know how.
firstcontroller.h
#import <UIKit/UIKit.h>
#interface OTFFirstViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webPage;
#property (strong, nonatomic) IBOutlet UILabel *pageLoading;
#end
firstcontroller.m
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *fullURL = #"http://asdf.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webPage loadRequest:requestObj];
}
- (void)webViewDidFinishLoad:(UIWebView *)_webPage
{
_pageLoading.hidden = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Insert _webPage.delegate = self; before calling loadRequest:.
You should alse modify interface definition to #interface OTFFirstViewController : UIViewController <UIWebViewDelegate>.

Related

no visible #interface for declares the selector errors

I'm getting No visible #interface for 'NSObject' declares the selector 'viewDidLoad' on
the lines:
[super viewDidLoad];
[_viewWeb loadRequest:requestObj];
[super didReceiveMemoryWarning];
UIViewControllerHUB.m
#import "UIViewControllerHUB.h"
#interface UIViewControllerHUB ()
#property (strong, nonatomic) NSMutableArray *subItems;
#end
#implementation UIViewControllerHUB
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://conecode.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
UIViewControllerHUB.h
#import <Foundation/Foundation.h>
#interface UIViewControllerHUB : NSObject
#property (strong, nonatomic) IBOutlet UIView *viewWeb;
#end
How can I fix this?
EVERYTHING ABOVE IS NOW RESOLVED.
New error below:
Now getting 'No visible #interface for 'UIView' declares the selector 'loadRequest:'
on line
[_viewWeb loadRequest:requestObj];
In your code 'No visible #interface for UIView declares the selector 'loadRequest:' on line why you are getting this error is becuase loadRequest is not the method of UIView. But in-spite of that it is the method of UIWebView. For more refer this UIWebView documentation . So just replace UIView to UIWebView and check
//Comment this
//#property (strong, nonatomic) IBOutlet UIView *viewWeb;
//Modify this
#property (strong, nonatomic) IBOutlet UIWebView *viewWeb;
Note:- As you have created outlet of UIView. So delete the same and drag-drop UIWebView and then reconnect the outlet to UIWebView
viewDidLoad is a UIViewController method. To fix it, change to inherit from that:
#import <Foundation/Foundation.h>
#interface UIViewControllerHUB : UIViewController
#property (strong, nonatomic) IBOutlet UIView *viewWeb;
#end

Mac OS X using WebView object

I can't see the webview when I launch tha app..
In the delegate that's my code
#import <Cocoa/Cocoa.h>
#include "NewsViewController.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic,strong) IBOutlet NSSplitView *splitView;
#property (nonatomic,strong) IBOutlet NewsViewController *newsViewController;
#end
#import "AppDelegate.h"
#implementation AppDelegate
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
// 1. Create the master View Controller
self.newsViewController = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.splitView addSubview:self.newsViewController.view];
}
#end
That's my viewcontroller
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface NewsViewController : NSViewController{
IBOutlet WebView *web;
}
#property (assign) IBOutlet WebView *web;
#end
#import "NewsViewController.h"
#interface NewsViewController ()
#end
#implementation NewsViewController
#synthesize web;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
-(void)loadView{
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[web mainFrame] loadRequest:requestObj];
}
#end
if I out one label on my view in the controller everything is fine but if I put a webview I see only a grey window.
Why this?
thanks
I do believe you need to call [super loadView] in your loadView implementation.

XCODE 4.5 - Expected Identifier... (UIWebView) - iPhone

View controller h:
#interface ViewController : UIViewController
{
IBOutlet UIWebView *WebView1;
IBOutlet UIWebView *WebView2;
IBOutlet UIWebView *WebView3;
IBOutlet UIWebView *WebView4;
IBOutlet UIWebView *WebView5;
}
#end
view controller m:
#import "ViewController.h"
#interface UIViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *myURL1 = [NSURL URLWithString:[#"http:://www.youtube.com"]];
NSURLRequest *MyRequest1 = [NSURLRequest requestWithURL:myURL1];
[WebView1 loadRequest:MyRequest1];
NSURL *myURL2 = [NSURL URLWithString:[#"http:://www.gmail.com"]];
NSURLRequest *MyRequest2 = [NSURLRequest requestWithURL:myURL2];
[WebView2 loadRequest:MyRequest2];
NSURL *myURL3 = [NSURL URLWithString:[#"http:://www.soundcloud.com"]];
NSURLRequest *MyRequest3 = [NSURLRequest requestWithURL:myURL3];
[WebView3 loadRequest:MyRequest3];
NSURL *myURL4 = [NSURL URLWithString:[#"http:://www.socialblade.com"]];
NSURLRequest *MyRequest4 = [NSURLRequest requestWithURL:myURL4];
[WebView4 loadRequest:MyRequest4];
NSURL *myURL5 = [NSURL URLWithString:[#"http:://www.fullscreen.net"]];
NSURLRequest *MyRequest5 = [NSURLRequest requestWithURL:myURL5];
[WebView5 loadRequest:MyRequest5];
}
#end
Decription:
The error is on each of the links "]". What am I dong wrong?
And I can't link/connect my outlets to the UIWebView's set up in tabs.
Please help me.
It's simple. You do not need the []'s around each string. Change it's URL to the following:
SURL *myURL4 = [NSURL URLWithString:#"http:://www.socialblade.com";
NSURLRequest *MyRequest4 = [NSURLRequest requestWithURL:myURL4];
[WebView4 loadRequest:MyRequest4];
That should solve it. If not, let me know.
Edit
Also, you are declaring your properties incorrectly, which is causing your to not connect them. You should declare your webView's like this:
View controller h:
#interface ViewController : UIViewController
#property (assign) IBOutlet UIWebView *WebView1;
#property (assign) IBOutlet UIWebView *WebView2;
#property (assign) IBOutlet UIWebView *WebView3;
#property (assign) IBOutlet UIWebView *WebView4;
#property (assign) IBOutlet UIWebView *WebView5;
#end
Then you must synthesize them in your .m
view controller m:
#import "ViewController.h"
#interface UIViewController ()
#end
#implementation ViewController
#synthesize WebView1;
#synthesize WebView2;
#synthesize WebView3;
#synthesize WebView4;
#synthesize WebView5;
After they are synthesized, you can call them as you have done. :)

I have two error : No visible #interface for 'UIWebview'

I have two error :No visible #interface for 'UIWebView' declares the selector 'highlightAllOccurencesOfString:'
another one:No visible #interface for 'UIWebView' declares the selector 'removeAllHighlights'
Please someone help me.
WBSecondViewController.h
#import <UIKit/UIKit.h>
#interface WBSecondViewController : UIViewController <UIWebViewDelegate, UIScrollViewDelegate>{
}
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property(copy) NSArray *menuItems;
#property (weak, nonatomic) IBOutlet UIToolbar *webToolBar;
- (IBAction)back:(id)sender;
- (IBAction)foward:(id)sender;
-(IBAction)searchButtonPressed:(id)sender;
-(IBAction)clearHighlights:(id)sender;
#end
WBSecondViewController.m
#import "WBSecondViewController.h"
#import "Word.h"
#import "WordController.h"
#import "AddWordController.h"
#import "WBAppDelegate.h"
#import "WBFirstViewController.h"
#import "SearchWebView.h"
#interface WBSecondViewController ()
#end
#implementation WBSecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Second", #"Second");
self.tabBarItem.image = [UIImage imageNamed:#"second"];
}
return self;
}
- (void)viewDidLoad
{
UIMenuController *menu = [UIMenuController sharedMenuController];
[super viewDidLoad];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.co.jp"];
[_webView loadRequest:[NSURLRequest requestWithURL:theURL]];
}
-(IBAction)searchButtonPressed:(id)sender{
[_webView highlightAllOccurencesOfString:#"cat"];
}
-(IBAction)clearHighlights:(id)sender{
[_webView removeAllHighlights];
}
SearchWebView.h
#import <Foundation/Foundation.h>
#interface SearchWebView : UIWebView
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;
#end
SearchWebView.m
#import "SearchWebView.h"
#implementation SearchWebView
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"UIWebViewSearch" ofType:#"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self stringByEvaluatingJavaScriptFromString:jsCode];
NSString *startSearch = [NSString stringWithFormat:#"uiWebview_HighlightAllOccurencesOfString('%#')",str];
[self stringByEvaluatingJavaScriptFromString:startSearch];
NSString *result = [self stringByEvaluatingJavaScriptFromString:#"uiWebview_SearchResultCount"];
return [result integerValue];
}
- (void)removeAllHighlights
{
[self stringByEvaluatingJavaScriptFromString:#"uiWebview_RemoveAllHighlights()"];
}
#end
U have subclassed uiwebview and called it SearchWebView but then when you create an instance of web view in your wbsecondviewcontroller, you use a regular web view instead of the subclass that you created and the regular web view does not have the two extra methods that you defined for that custom one. Above #interface in the wbsecondviewcontroller.h do #class SearchWebView. Then where you declare the property UiWebView, declare it as a SearchWebView instead. In the .m file of the wbsecondviewcontroller do #import "SearchWebView.h"

How Do I Update UIWebView After viewDidLoad?

This is my first iOS app, so I am probably missing something very simple. Please be kind. I have been tearing my hair out and I could really use some help.
Overview Of App
Basically, this is a single page application that just loads a UIWebView. I have an external accessory (bluetooth barcode scanner) that I connect and basically what I want to do is when the the app receives a scan, I want to call a method in my ViewController and update the UIWebView accordingly.
What Is Working
I am able to connect the scanner, load the first view, which loads the initial webpage, scan a barcode and call the method in my controller.
My Problem
I can't seem to figure out how to update the UIWebView from the method in my controller. It logs the url string to my debugger area, but never actually updates the webview. I am pretty sure I have some delegation wrong or something with my webview instance. There must be some glue code here that I am missing.
My Code HelloWorldViewController.h
#import <UIKit/UIKit.h>
#import "KScan.h"
#interface HelloWorldViewController : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *page;
IBOutlet UILabel *myLabel;
Boolean IsFirstTime;
KScan *kscan;
}
- (void)setFirstTime;
- (void)DisplayConnectionStatus;
- (void)DisplayMessage:(char *)Message;
- (void)newBarcodeScanned:(NSString *)barcode;
- (void)loadBarcodePage:(NSString *)barcode;
#property (nonatomic, retain) KScan *kscan;
#property (nonatomic, retain) UIWebView *page;
#property (nonatomic, retain) UILabel *myLabel;
#end
My Code HelloWorldViewController.m
#import "HelloWorldViewController.h"
#import "common.h"
#implementation HelloWorldViewController
#synthesize myLabel;
#synthesize page;
#synthesize kscan;
- (void)setFirstTime
{
IsFirstTime = true;
}
- (void)viewDidLoad
{
self.kscan = [[KScan alloc] init];
[super viewDidLoad];
page.scrollView.bounces = NO;
//page.delegate = self;
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://192.168.0.187:3000"]]];
}
- (void) newBarcodeScanned:(NSString *)barcode
{
NSLog(#"%s[%#]",__FUNCTION__, barcode);
[self loadBarcodePage:barcode];
}
- (void)loadBarcodePage:(NSString *)barcode
{
NSLog(#"%s",__FUNCTION__);
NSString *url = [[NSString alloc] initWithFormat:#"http://www.google.com/%#", barcode];
NSLog(#"%#", url);
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
- (void)viewDidUnload
{
[myLabel release];
myLabel = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (void)dealloc {
[page release];
[kscan release];
[myLabel release];
[super dealloc];
}
#end
Basically, I am just trying to load google.com into my page webview when scanning a barcode. My log statements are being logged with the correct URL, but this line of code doesn't work.
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
I am not getting any errors and my xCode debugging skills are not the greatest.
Any help would be greatly appreciated!
It looks like your webview is never allocated, or added to your main view. You are probably talking to a nil instance.
Unless your web view comes from a XIB file (which I doubt since it is not declared as an IBOutlet in your heder file) try adding something like this to your viewDidLoad:
self.page = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.page];