Same WebView on every view

2019-06-24 03:50发布

Basically i have a WebView on SecondViewController and I wish for the WebView to be visible on every view like a tab bar and fully controllable on each view.

Please note the WebView will be on a webpage with a online slideshow so I cannot simply reload on each view

Also in the SecondViewController I have

- (void)webViewDidFinishLoad:(UIWebView *)YouTubePlayer {

8条回答
萌系小妹纸
2楼-- · 2019-06-24 04:54

In iOS UIViewControllers are expected to manage an entire "screen" worth of content so it's not normal to try to share a single view across many view controllers. Trying to have UIViewControllers whose views only manage part of their window is problematic and will result in unexpected behavior as UIKit will not send messages like -viewWillAppear to all view controllers with visible views. Instead you would normally create a single UIViewController whose view includes that web view and whatever other views compose your tab like interface. Alternately you could have a hierarchy of many view controllers and add a single web view as a subview of all of them. You would then pull your web view delegate behavior out into some non-UIViewController controller class to manage the behavior of the web view.

查看更多
放我归山
3楼-- · 2019-06-24 04:55

I'd just set up a singleton UIWebView and add it to each view-controller-view when that view controller is about to become visible. Here's one way to do it:

//.h

@interface SharedWebView : UIWebView
{
}

+ (SharedWebView*) shared;

@end

//.m

SharedWebView* g_sharedWebView;

@implementation SharedWebView


+ (SharedWebView*) shared 
{
    if ( g_sharedWebView == nil )
    {
        g_sharedWebView = [[SharedWebView alloc] init];

        // ... any other intialization you want to do
    }

    return g_sharedWebView;
}

@end


// in your view controller(s)

@property (readonly) UIWebView* webView

- (UIWebView*) webView
{
    return [SharedWebView shared];
}

- (void) viewWillAppear: (BOOL) animated
{
    [super viewWillAppear: animated];

    [self.view addSubview: self.webView ];
    self.webView.frame = CGRectMake(10, 10, 300, 300);

    // want to re-set the delegate?
    // self.webView.delegate = self;
}
查看更多
登录 后发表回答