5

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];

}
4

2 に答える 2

4

NSURL*customVCに type のプロパティを作成します。

customVC の-(id)initメソッドを-(id)initWithURL:(NSURL*)url次のように変更します。

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

それから電話する

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

viewDidLoad

次に、customVC のさまざまなインスタンスを初期化するときに、次を使用します。

customVC *vc = [customVC alloc]initWithURL:[NSURL URLWithString:@"http://..."]];
于 2013-03-20T13:53:45.670 に答える
1

メソッドを呼び出した後、webview を初期化していると思われますloadRequest:。これを回避するには、セッターをオーバーライドして非アトミック プロパティを初期化することをお勧めします。

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

loadRequest:このようにして、カスタムビューコントローラーのviewDidLoadメソッドで、最初にアクセスしたときに(を呼び出している間)、その後ではなく、Webビューが初期化されます。

于 2013-03-20T14:09:07.480 に答える