Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different URI for each instance of UIViewController

I have a UITabController with five tabs. Each tab simply holds an instance of a custom UIViewController, and each instance holds a UIWebView.

I want the UIWebView in each tab to open a different URI, but I don't think it should be necessary to create a new class for each tab.

I can make it work if I do [self.webView loadRequest:] in -(void)viewDidLoad but it seems ridiculous to create five different classes with five different versions of viewDidLoad when all I really want to change is the URI.

Here's what I've tried:

appDelegate.h

#import <UIKit/UIKit.h>

@interface elfAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;

@end

appDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    customVC *start = [[customVC alloc] init];
    customVC *eshop = [[customVC alloc] init];
    customVC *table = [[customVC alloc] init];
    customVC *video = [[customVC alloc] init];
    customVC *other = [[customVC alloc] init];

    // Doesn't do anything... I wish it would!
    [start.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[start, eshop, table, video, other];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

customVC.h

#import <UIKit/UIKit.h>

@interface customVC : UIViewController <UIWebViewDelegate> {
    UIWebView* mWebView;
}

@property (nonatomic, retain) UIWebView* webView;

- (void)updateAddress:(NSURLRequest*)request;
- (void)loadAddress:(id)sender event:(UIEvent*)event;

@end

customVC.m

@interface elfVC ()

@end

@implementation elfVC

@synthesize webView = mWebView;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.webView = [[UIWebView alloc] init];
    [self.webView setFrame:CGRectMake(0, 0, 320, 480)];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;

    [self.view addSubview:self.webView];

}
like image 252
Jezen Thomas Avatar asked Mar 20 '13 13:03

Jezen Thomas


Video Answer


2 Answers

Create a property of type NSURL* in your customVC.

Change your customVC's -(id)init method to -(id)initWithURL:(NSURL*)url as follows:

-(id)initWithURL:(NSURL*)url{
 self = [super init];
 if(self){ 
  self.<URLPropertyName> = url;
 }
 return self;
}

Then call

[start.webView loadRequest:[NSURLRequest requestWithURL:self.<URLPropertyName>]];

in viewDidLoad

Then when you initialize your different instances of customVC, just use

customVC *vc = [customVC alloc]initWithURL:[NSURL URLWithString:@"http://..."]];
like image 118
Petar Avatar answered Sep 30 '22 15:09

Petar


I suspect you are initialising your webview after calling it's loadRequest: method. To avoid this, it's a better practice to initialise nonatomic properties by overriding their setters:

- (UIWebView*)webview {
    if (mWebView == nil) {
        // do the initialization here
    }
    return mWebView;
}

This way your webview will be initialised when you first access it (while calling loadRequest:) and not after it, in your custom view controller's viewDidLoad method.

like image 45
jkovacs Avatar answered Sep 30 '22 17:09

jkovacs