Reputation: 7193
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?
Upvotes: 0
Views: 87
Reputation: 2892
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.
Upvotes: 0
Reputation: 534893
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.
Upvotes: 2