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 tha开发者_JS百科t 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
精彩评论