刷新数据后IBOutlet为零

时间:2015-05-14 22:41:20

标签: ios swift caching parse-platform iboutlet

我正在使用XCode 6.3.1而我正面临一个奇怪的问题,我无法弄清楚。

我的故事板上有一个带有模态segue的View Controller。当ViewController打开时,它会从我的后端(Parse)加载一些数据,它首先在缓存中查看并显示缓存的数据(如果存在),同时在后台检索来自服务器的更新数据。这个过程是下一个:

  1. 获取缓存数据
  2. 如果存在缓存数据,则更新界面
  3. 请求服务器数据(在后台)
  4. 数据到达时更新界面
  5. 一切正常,直到第4步。当我尝试刷新界面时,我的@IBOutlets的一半突然变为nil,当然还有应用程序崩溃。

    我错过了什么?

    以下是代码:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Do any additional setup after loading the view.
    
        //eventId is set when the ViewController is instantiated
        if eventId != nil {
            loadEvent(eventId)
        }
    }
    
    func loadEvent(id: String) {
    
        var query = Event.query()
        query?.cachePolicy = Util.getCachePolicy()   //First look in cache, the request network data
        query?.getObjectInBackgroundWithId(id, block: { (event: PFObject?, error: NSError?) -> Void in
    
            if error == nil {
    
                var updatedEvent = event as! Event
                self.event = updatedEvent
                self.updateLayout()    
                //When self.updateLayout() is called with cached data 
                //all my IBOutlets are fine but when it's called the second time,
                //with data from server half of the IBOutlets are nil
            }
        })
    }
    
    func updateLayout() {
    
        if event != nil {
    
            eventTitle.text = event.name
    
            var paletteColor : UIColor!
            var location = event.location
            var locationName = location["name"] as! String
            eventLocation.text = NSString(format: NSLocalizedString("event_subtitle", comment: ""), event.formattedTimes(), locationName) as String
    
            eventDescription.text = event.abstract
    
            if event.paletteColor != 0 {
                paletteColor = Util.colorFromInt(event.paletteColor)
                eventHeader.backgroundColor = paletteColor
                speakersBlockTitle.textColor = paletteColor  
                mapButton.tintColor = paletteColor
            }
    
            if event.hasPhoto() {
    
                self.eventPhoto.file = event.eventPhoto
                self.eventPhoto.loadInBackground({ (image:UIImage?, error:NSError?) -> Void in
                    UIView.animateWithDuration(0.5, animations: { () -> Void in
    
                        self.eventPhoto.alpha = 1.0
                    })
                })
    
            } else {
                self.eventPhoto.removeFromSuperview()
                if paletteColor == nil {
                    paletteColor = eventHeader.backgroundColor
                }
                actionBar.backgroundColor = paletteColor
            }
    
            if event.speaker.isDataAvailable() {
    
                var speaker = event.speaker
                speakerName.text = speaker["name"] as? String
                speakerInfo.text = speaker["speakerInfo"] as? String
                speaker["speakerPhoto"]?.getDataInBackgroundWithBlock({ (imageData:NSData?, error:NSError?) -> Void in
                    if error == nil {
                        self.speakerPhoto.image = UIImage(data:imageData!)
                        self.speakerPhoto.layer.cornerRadius = self.speakerPhoto.frame.size.width/2
                        self.speakerPhoto.clipsToBounds = true
                    }
                })
            } else {
                speakerBlock.removeFromSuperview()
            }
    
            UIView.animateWithDuration(0.5, animations: { () -> Void in
    
                self.eventHeader.alpha = 1.0
                self.eventDescription.alpha = 1.0
                self.speakerBlock.alpha = 1.0
                self.mapButton.alpha = 1.0
            })
        }
    }
    

    这是所有的无IBOutlets: This are all the nil IBOutlets:

1 个答案:

答案 0 :(得分:0)

由于我上面评论中的代码没有正确显示,所以再次发表评论:

这样的事情:

@IBOutlet weak var myOutlet: UIButton! {
    didSet {
        if myOutlet == nil {
            // put a breakpoint on the next line
            println("myOutlet set to nil")
        }
    }
}