我有一个带有五个选项卡的 UITabController。每个选项卡只包含一个自定义 UIViewController 的实例,每个实例都包含一个 UIWebView。
我希望每个选项卡中的 UIWebView 打开不同的 URI,但我认为没有必要为每个选项卡创建一个新类。
如果我这样做,我可以让它工作[self.webView loadRequest:]
,-(void)viewDidLoad
但是当我真正想要改变的只是 URI 时,用五个不同版本的 viewDidLoad 创建五个不同的类似乎很荒谬。
这是我尝试过的:
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;
}
自定义VC.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
自定义VC.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];
}