ios - How can I "remove" the embed segue in my container view, or remove the container view completely? -
i have splashviewcontroller
containerview
, created in storyboard.
in story board, automatically drag embed segue containerview
profileviewcontroller
.
inside splashviewcontroller
, want programatically "destroy" containerview
+ profileviewcontroller
(both of them).
i've tried this:
self.containerview.hidden = true //obviously doesn't work. it's visual self.containerview.removefromsuperview() //nope. it's visual.
how can remove both containerview and profileviewcontroller completely, making sure both deinit
appropriately? if that's not possible, can @ least deinit profileviewcontroller? (i'll set containerview hidden).
note: play movie video automatically (looping) in profileviewcontroller. has sound, , when set containerview nil , remove superview, sound keeps playing.
i wrote quick example project here demonstrating solution here. autoplaying video (with dog barking) automatically plays in bottom of screen. pressing "stop player" button removes child uiviewcontroller
, container.
mpmovieplayercontroller
finished playing audio after movieplayer stopped or set nil (although not deallocated if internal part of mpmovieplayercontroller
held strong reference suspect problem).
anyway, found workaround setting contenturl
of movie player nil on deinit
before calling movieplayer.stop()
//the `profileviewcontroller` created in storyboard child of `viewcontroller`. class viewcontroller: uiviewcontroller { @iboutlet weak var container: uiview? @ibaction func stopplayer(sender: uibutton) { //we remove child view controller (the profileviewcontroller) parent controller in self.childviewcontrollers { if let child = controller as? profileviewcontroller { child.removefromparentviewcontroller() //this causes deinit call container?.removefromsuperview() //clear container (stays black box otherwise) } } } }
in profileviewcontroller
, have change contenturl , stop our movie player
class profileviewcontroller: uiviewcontroller { lazy var movieplayer: mpmovieplayercontroller = { let path = nsbundle.mainbundle().pathforresource("testmovie", oftype: "mp4")! let movieplayer = mpmovieplayercontroller(contenturl: nsurl(fileurlwithpath: path)!) movieplayer.moviesourcetype = .file movieplayer.preparetoplay() movieplayer.view.frame = self.view.frame return movieplayer }() override func viewdidappear(animated: bool) { view.addsubview(movieplayer.view) movieplayer.play() //looping nsnotificationcenter.defaultcenter().addobserver(self, selector: "movieplayerdidfinish:", name:mpmovieplayerplaybackdidfinishnotification, object: movieplayer) } //change our contenturl stop deinit { movieplayer.contenturl = nil movieplayer.stop() } @objc func movieplayerdidfinish(notification: nsnotification) { let reasonint = notification.userinfo![mpmovieplayerplaybackdidfinishreasonuserinfokey] as! int let reason = mpmoviefinishreason(rawvalue: reasonint) if (reason == mpmoviefinishreason.playbackended) { movieplayer.play() } } }
tested on iphone 5 device , on simulator
tl;dr
- remove container
uiview
callingremovefromsuperview
- the
mpmovieplayercontroller
keeps playing audio after being stopped, bug. workaround setting contenturl stopping
Comments
Post a Comment