2013-03-20 21 views
5

5つのタブを持つUITabControllerがあります。各タブにはカスタムUIViewControllerのインスタンスが保持され、各インスタンスにはUIWebViewが保持されます。UIViewControllerのインスタンスごとに異なるURI

各タブのUIWebViewに別のURIを開きたいが、タブごとに新しいクラスを作成する必要はないと思う。

私が-(void)viewDidLoadに​​を入れれば動作させることができますが、私が実際に変更したいのはURIですので、viewDidLoadの5つの異なるバージョンで5つの異なるクラスを作成するのはうんざりです。

ここで私が試したものです:

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

} 

答えて

3

あなたcustomVCにおけるタイプNSURL*のプロパティを作成します。次のように

-(id)initWithURL:(NSURL*)urlにごcustomVCの -(id)init方法を変更し

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

次にあなたがcustomVCのあなたの異なるインスタンスを初期化するときに、ちょうど

を使用 viewDidLoad

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

を呼び出します

customVC *vc = [customVC alloc]initWithURL:[NSURL URLWithString:@"http://..."]]; 
+0

有望に見えますが、どこかで混乱しているはずです。 _customVC.h_では、@property(nonatomic、retain)NSURL * initURL;を追加しました。 _customVC.m_では、@synthesize initURL;と ' - (id)initWithURL:(NSURL *)url { [self setInitURL:url];を追加しました。 return self; } '。私は、 'Initメソッドは受信者の種類に関連する型を返す必要があります。'というエラーが表示されます。私は何を返すべきですか? –

+0

編集された回答を参照してください。 – Petar

+0

私はまだ同じエラーが発生しています。このエラーは_ARCセマンティクスの問題です。私のプロパティを間違って作成していますか? NSURL * initURL; '@property(nonatomic、retain)でなければならない? –

1

loadRequest:メソッドと呼ばれると、あなたのウェブビューを初期化していると思われます。これを避けるために、それは彼らのセッターをオーバーライドすることで非アトミックプロパティを初期化するためのより良いプラクティスです:

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

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

+0

ありがとうございます。私はそれが本当に私の問題を解決するかどうかはわかりませんが、助言に感謝します。 –

関連する問題