开发者

destroy uiviewcontroller containing UIWebView when playing video?

开发者 https://www.devze.com 2023-03-11 10:41 出处:网络
Hey all... I have a view controller (A) which on some action, alloc init\'s another view controller (B) and then adds B\'s view to its view as a subview. So now ViewController B\'s view is a subview o

Hey all... I have a view controller (A) which on some action, alloc init's another view controller (B) and then adds B's view to its view as a subview. So now ViewController B's view is a subview of ViewController A. The problem I have is If I simply remove B's view from A it seems to still stick around for example. View B contains a web view, when I load a video on the webView, even after I remove the view from view Controller A's view I can still hear the video??

How can I destroy viewcontroller B and remove its subview from A? Im finding this tricky as I dont really push it onto a navigationcontroller's stack which I can just pop from... I hope this makes sense, if not please say and I will try and clarify.

Many thanks

Jules

 -(void)showNewsWebView:(int)index {

NewsWebViewController *myWebView = [[[NewsWebViewController alloc] initWithNibName:@"NewsWebViewController" bundle:nil]autorelease];

//setup webview with request etc

[[self.view开发者_StackOverflow中文版.superview superview] addSubview:myWebView.view];
myWebView.alpha = 0.
[UIView beginAnimations:@"test" context:nil];
[UIView setAnimationDuration:.3];
myWebView.view.alpha = 1.;
[UIView commitAnimations];

}

//called after delegate callback from webviewcontroller
- (void)newsWebViewDismissedView:(NewsWebViewController *)controller {
[UIView beginAnimations:@"test" context:nil];
[UIView setAnimationDuration:.3];
controller.view.alpha = 0.0;
[self performSelector:@selector(removeView:) withObject:controller.view afterDelay:.5];

[UIView commitAnimations];
}

-(void) removeView:(UIView *)view {
[view removeFromSuperview];
view = nil;
}


Does ViewController B really need to be a ViewController?

If you're adding subviews you should probably have B subclass UIView instead of UIViewController. Adding B's view as a subview essentially negates any advantage you'd have of B being a ViewController.

Anyway to answer your question. You might want to make viewcontroller B an ivar of A so that viewcontroller A can manage the memory of viewController B. Once you remove the view of B from A, you can release viewcontroller B from memory (I still don't support this as it sounds like ineffective code. You should probably state what you're aiming to do, and post some code as to how you're doing it so we can help you out better :) )

EDIT:

From your code seems like you should just be pushing and popping. Are you using MyWebViewController just to show a webpage? You might be better off using a simple UIWebView.

I also noticed something wrong in your animation code for setting alpha to 0. If you want some method to be executed after an animation ends you should use the following code:

//called after delegate callback from webviewcontroller
- (void)newsWebViewDismissedView:(NewsWebViewController *)controller {
[UIView beginAnimations:@"test" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(removeView)];

controller.view.alpha = 0.0;

[UIView commitAnimations];
}


ViewControllerB *vc = [[[ViewControllerB alloc] initWithNibName:@"SomeNib" bundle:nil] autorelease];

This should do the trick. Autoreleasing the view controller at the end of its initial allocation should tell the application to deallocate it after you remove the view from viewcontroller A since nothing else is holding a retain value on it. Though using a navigation controller might be an easier solution if you are willing to rework your code to push and pop the view instead

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号