简体   繁体   中英

iphone how to remove modal uiviewcontroller from memory

I have a root UIViewController which has a property called webView. WebView is a UIViewController with a XIB that contains a UIWebView. From my root view I modally (is there any other way?) load the webView ViewController and set its URL, always to the same page.

I discovered that if, after loading the webView, I used its default Web page to navigate to another Web page, and then closed the webView and returned to the root controller -- AND THEN reopened the webView, that the webView was not showing the default page but rather the page I navigated to, which means the webView ViewController never got destroyed and removed from memory. This strikes me as very bad.

So in the root ViewController, I added this code under viewWillAppear:animated --

if (self.webView != nil) {
    self.webView = nil)
}

Is that sufficient? Is there a better way?

I have a root UIViewController which has a property called webView. WebView is a UIViewController with a XIB that contains a UIWebView.

First, a style suggestion. Don't name your view controller classes FooView . Name them FooViewController . Because that's what they are. Controllers. Not Views.

From my root view I modally (is there any other way?) load the webView ViewController and set its URL, always to the same page.

You can bring a view on screen in any number of ways; presenting it as a model view controller is one way. Another would be to have a navigation controller and push it on the navigation stack. Another would be to use the animation primitives and swap an active view in and out.

If you're presenting the view controller modally, you should be using the delegate pattern, and having the "parent" view controller dismiss it modally when you want it go to away.

The answer I gave here:

Pop-up modal with UITableView on iPhone

Is pretty close to the code you'll need, except you'll probably just want to have a simpler delegate protocol like:

@protocol FooViewControllerDelegate <NSObject>
- (void)fooViewControllerDidFinish:(FooViewController*)fooViewController;
@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM