Q: Detail Disclosure loading on top of the previous detail disclosure I have a map view with annotations and every annotation has a corresponding date with day, month, and time. 
I'm pushing the day to the new view controller then using a switch to see what day of the month it falls on and then displaying the corresponding calendar icon. I can't put the func anywhere other than viewDidAppear which cause a brief lag and two, prevents me from using 
for view in self.view.subviews {
    view.removeFromSuperview()
}

So two questions: 


*

*how can I call the dayCalendar function before viewDidAppear (anywhere else I've tried to put the code, it returns a nil) 

*how do I prevent my detailDisclosures from appearing on top of one another?


This is the code responsible for showing the image:
override func viewDidAppear(animated: Bool) {
    dayCalendar()
}

func dayCalendar() {

    switch day {
    case 1:
        let calendarDay = "Calendar 1-50.png"
        let calendarDayImage = UIImage(named: calendarDay)
        let calendarDayImageView = UIImageView(image: calendarDayImage!)
        calendarDayImageView.frame = CGRect(x: 25 , y: 350, width: 50, height: 50)
        self.view.addSubview(calendarDayImageView)
    case 8:
        let calendarDay = "Calendar 8-50.png"
        let calendarDayImage = UIImage(named: calendarDay)
        let calendarDayImageView = UIImageView(image: calendarDayImage!)
        calendarDayImageView.frame = CGRect(x: 25 , y: 350, width: 50, height: 50)
        self.view.addSubview(calendarDayImageView)
    case 11:
        let calendarDay = "Calendar 11-50.png"
        let calendarDayImage = UIImage(named: calendarDay)
        let calendarDayImageView = UIImageView(image: calendarDayImage!)
        calendarDayImageView.frame = CGRect(x: 25 , y: 350, width: 50, height: 50)
        self.view.addSubview(calendarDayImageView)

    case 12:
        blahh blahh blahh all the way down

    case 31:
        let calendarDay = "Calendar 30-50.png"
        let calendarDayImage = UIImage(named: calendarDay)
        let calendarDayImageView = UIImageView(image: calendarDayImage!)
        calendarDayImageView.frame = CGRect(x: 25 , y: 350, width: 50, height: 50)
        self.view.addSubview(calendarDayImageView)
    default:
        println("Default")
        break
    }

}




A: First off: why are you actually switching? As far as I can tell the code can be reduced to
func dayCalendar() {
    if day < 1 || day > 31 {
        return 
    }
    let calendarDay = "Calendar \(day)-50.png"
    let calendarDayImage = UIImage(named: calendarDay)
    let calendarDayImageView = UIImageView(image: calendarDayImage!)
    calendarDayImageView.frame = CGRect(x: 25 , y: 350, width: 50, height: 50)
    self.view.addSubview(calendarDayImageView)
}

You might want to move the call to viewWillAppear.
To fix the overlaying of the detail closures you have two options:


*

*remove the added calendarDayImageView every time you display a new day.


or (much better)


*

*reuse the already added image view if it is already present (might want to create a lazy variable) and just change the image it is displaying.


The later one you can achieve in the following way:
// add this variable to your class
lazy var calendarDayImageView: UIImageView = {
    [unowned self] in
    let imageV = UIImageView(frame: CGRect(x: 25 , y: 350, width: 50, height: 50))
    self.view.addSubview(imageV)
    return imageV
}()

// change the dayCalender to make use of the newly added variable
func dayCalendar() {
    if day < 1 || day > 31 {
        return 
    }
    let calendarDay = "Calendar \(day)-50.png"
    let calendarDayImage = UIImage(named: calendarDay)
    calendarDayImageView.image = calendarDayImage
}

Alternatively you can just move the initialization of the view inside viewDidLoad and have dayCalender again only set the actual image - but i like lazy vars ;)
