I am a new programmer and I'm making my first app, i already made the Android one. I'm having troubles with the webviews in Xcode.
I saw this tutorial Xcode 5 Creating a Web View
And now I'm able to make one functional webview. My question is: Do I have to create one ViewController by every webview that I'd like to make? Or can i use one ViewController for 50 webviews?
I tried using the same ViewController for two webviews. The first one works, the second one does nothing.
nsotrosWebViewController.h
#import <UIKit/UIKit.h>
#interface nsotrosWebViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *Nosotros;
#property (weak, nonatomic) IBOutlet UIWebView *Tienda;
#end
nsotrosWebViewController.m
#import "nsotrosWebViewController.h"
#interface nsotrosWebViewController ()
#end
#implementation nsotrosWebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Webview nosotros-spanish
NSURL *nosotrosURL = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *nosotrosRequest = [NSURLRequest requestWithURL:nosotrosURL];
[_Nosotros loadRequest:nosotrosRequest ];
// Webview tienda-spanish
NSURL *tiendaURL = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *tiendaRequest = [NSURLRequest requestWithURL:tiendaURL];
[_Tienda loadRequest:tiendaRequest ];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I also tried creating one ViewController for each webviews but it won't show more than 2 custom ViewControllers on the "Custom class" menu.
Related
I have a uiwebview in my ios program and it is connected to a variable inside the ViewController interface
#interface ViewController() <UIWebViewDelegate>
#property(weak, nonatomic) IBOutlet UIWebView *uiwebview;
#end
I want to call functions on this uiwebview from outside the viewcontroller
For example, I can control the webview just fine if it is used between
#implementation ViewController
.
.
.
#end
But I want to use it somewhere outside. For example, I want to do something like this
[uiwebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
from outside #implementation ViewController
What are the options to achieve this?
But I want to use it somewhere outside. For example, I want to do something like this
[uiwebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
from outside #implementation ViewController
Stop wanting that. An IBOutlet should be private to its own view controller. If you want other view controllers to be able to do something to that web view, then arm your ViewController with public methods that they can call.
I think I understand what you are trying to do, I have an app that has various tabs some of which are web view. When my app starts I get it to pre-load the web views so that when the tabs are selected they are already loaded. Its important the the other tabs are all singleton classes so that there is only one instance of them.
In my main ViewController viewDidLoad I have this ..
//Pre load calculators and status page
EvolutionViewController *evoCalc = [EvolutionViewController sharedInstance];
[evoCalc.view layoutSubviews];
IVCalcViewController *ivCalc = [IVCalcViewController sharedInstance];
[ivCalc.view layoutSubviews];
StatusViewController *serverStatus = [StatusViewController sharedInstance];
[serverStatus.view layoutSubviews];
Those instantiate the classes for the other tabs which also loads up the web views associated with those classes. All the classes are similar this is the .h
#import <UIKit/UIKit.h>
#interface EvolutionViewController : UIViewController <UIWebViewDelegate>
+ (EvolutionViewController *) sharedInstance;
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#end
And the .m
#import "EvolutionViewController.h"
#interface EvolutionViewController ()
#end
#implementation EvolutionViewController
#synthesize webView;
#pragma mark - Singleton Methods
static EvolutionViewController *_sharedInstance;
- (id) init
{
if (self = [super init])
{
// custom initialization
}
return self;
}
+ (EvolutionViewController *) sharedInstance
{
return _sharedInstance;
}
-(id)initWithCoder:(NSCoder *)decoder {
if (!_sharedInstance) {
if (self = [super initWithCoder:decoder]) {
_sharedInstance = self;
}
}
return _sharedInstance;
}
#pragma mark - View Controller Methods
-(void)viewWillLayoutSubviews {
self.webView = [[UIWebView alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource: #"evo_calc" withExtension:#"html"];
NSURLRequest*request=[NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
#end
Mine uses local html resources but works the same with a proper URL as well.
I am working on an app in Xcode, using modal segues to navigate forwards and using unwind segues to go back to the previous view and also to the home view. However, the segue is just not working when I open up the simulator, no errors are shown and I have used the same approach to unwind segue as is used in the Apple To-do list tutorial, so not sure why this isn't working.
Below is my .h file for one of the views I want to unwind from:
#import <UIKit/UIKit.h>
#interface XYZBonBonoftheDayViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *BBoD;
- (IBAction)unwindBBoD:(UIStoryboardSegue *)segue;
#end
Here is my .m file from the same view:
#import "XYZBonBonoftheDayViewController.h"
#interface XYZBonBonoftheDayViewController ()
#end
#implementation XYZBonBonoftheDayViewController
- (IBAction)unwindBBoD:(UIStoryboardSegue *)segue
{
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
If anyone could help, this would be immensely helpful! Thanks in advance :)
I'm developing an iOS application and I have a strange problem.
The application sends local notifications to the user and when he taps on it, my app programmatically chooses a ViewController, sets its labels and images and then displays it to the user. But it simply doesn't work! Labels and images are not set. Here's my code in AppDelegate.m
- (void) manageLocalNotification:(UILocalNotification *) notification {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
if ([self.theme intValue] == 1) {
ADATheme1ViewController *notifyViewController = [[ADATheme1ViewController alloc] init];
[notifyViewController.titleLabel setText:self.title];
[notifyViewController.bodyLabel setText:self.text];
[notifyViewController.bgImage setImage: [self getImageFromURL:self.background]];
self.window.rootViewController = notifyViewController;
} else {
ADATheme2ViewController *notifyViewController = [[ADATheme2ViewController alloc] init];
[notifyViewController.titleLabel setText:self.title];
[notifyViewController.bodyLabel setText:self.text];
[notifyViewController.offerLabel setText:self.offer];
[notifyViewController.bgImage setImage: [self getImageFromURL:self.background]];
self.window.rootViewController = notifyViewController;
}
}
I made some debugging and my properties (text, title, offer and so on) are properly set. The method getImageFromUrl: returns an UIImage* and it works.
My ViewControllers are very simple, here's the code (but nothing special)
.h file
#import
#interface ADATheme1ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *bgImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UILabel *bodyLabel;
#end
.m file
#import "ADATheme1ViewController.h"
#interface ADATheme1ViewController ()
#end
#implementation ADATheme1ViewController
- (id)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 from its nib.
}
#end
and there's a .xib file attached.
Can you help me?
Solved: the view weren't loaded so the properties were set to nil.
I added just a line of code to "force" the loading of my view before setting labels:
[notifyViewController view];
I am creating a simple UIWebView like shown.
All works fine but the websites are corrupted. it looks like, the images arent loaded.
Curious is, that in the Browser, the same page is loading fine O_o Any Ideas?
#import "WebViewController.h"
#interface WebViewController ()
#property NSURL *adUrl;
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#end
#implementation WebViewController
-(id) initWithUrl:(NSURL *)url
{
self = [super init];
if (self)
{
self.adUrl = [NSURL new];
self.adUrl = url;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.adUrl]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Task solved. The Problem was, that an old Class which was responsible for WebCaching was disturbing the redirect and on that way it suppressed the load of baseURL objects. Thanks for givin me ideas how to solve.
Cheers
I'm new for iOS develop. I tried to make a very simple app just show a UIWebView which display a web page:
ViewController.m:
#import "ViewController.h"
#implementation ViewController
#synthesize webView = _webView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *urlAddress = #"http://jsfiddle.net/emj365/qaQnF/embedded/result/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
...
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
UIWebView * _webView;
}
#property (nonatomic, retain) IBOutlet UIWebView * webView;
#end
UIWebView show blank. What's wrong?
There are a few different things that could go wrong. A few options you should check:
Did you check to see if the outlet was actually connected to the webview in the xib?
Implement the UIWebView delegate and check for (networking) errors