Reputation: 1527
I have a func that calls itself over and over again for a simple little slideshow.
func animateImage(no:Int)
{
println("ANIMATE")
var imgNumber:Int = no
let t:NSTimeInterval = 1;
let t1:NSTimeInterval = 0;
let url = NSURL(string: self.itemss[imgNumber][0])
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
imgView!.alpha = 0.4
self.imgView?.image = UIImage(data: data!)
//code to animate bg with delay 2 and after completion it recursively calling animateImage method
UIView.animateWithDuration(2.0, delay: 0, options:UIViewAnimationOptions.CurveEaseOut, animations: {() in
self.imgView!.alpha = 1.0;
},
completion: {(Bool) in
imgNumber++;
if imgNumber>self.itemss.count-1 //only for 4 image
{
imgNumber = 0
}
self.animateImage(imgNumber);
})
}
The problem I'm having is that the func seems to continue to call and doesn't seem to be stopping when i dismiss the view controller ? Why is this func continuing to call after the view controller is dismissed.
@IBAction func dismissMe(sender: AnyObject) {
if((self.presentingViewController) != nil){
self.dismissViewControllerAnimated(false, completion: nil)
println("done")
}
}
Upvotes: 1
Views: 1167
Reputation: 62062
The reason that your function is continuing to be called after the view controller is dismissed is because there's no logic that should prevent it from discontinuing to call itself once it is dismissed.
A dismissed view controller should cease to have a parent view controller, however, so we could put an end to this loop by checking if there's a parent view controller at the top of the method:
if self.parentViewController == nil {
return;
}
If this is the first thing your method does, then the method should only continue to execute (and line up another call to itself) if the view controller still has a parent.
Upvotes: 1