ios - 刷新数据后 IBOutlet nil

标签 ios swift caching parse-platform iboutlet

我正在使用 XCode 6.3.1,我遇到了一个我无法解决的奇怪问题。

我的 Storyboard 上有一个 View Controller,它使用模式转场打开。当 ViewController 打开时,它从我的后端(解析)加载一些数据,它首先查看缓存并显示缓存数据(如果存在),同时在后台检索来自服务器的更新数据。接下来的过程是:

  1. 获取缓存数据
  2. 如果存在缓存数据则更新接口(interface)
  3. 请求服务器数据(在后台)
  4. 数据到达时更新接口(interface)

在第 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:

最佳答案

由于我上面评论中的代码显示不正确,这里再次评论:

像这样:

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

关于ios - 刷新数据后 IBOutlet nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30248662/

相关文章:

ios - 在屏幕之间导航

ios - 如何在 UITableViewCell 中创建 "underline"文本?

swift - 如何在 Swift 中处理多个泛型协议(protocol)?

java - 如何在多个模块/项目中配置多个缓存管理器 spring cache java

c# - 缓存 Linq 查询 - 这可能吗?

java - 当共享 JDBC 缓存存储到位时,将 "numOwners"在 Infinispan 中设置为大于 1 是否有效?

ios - 存储在 'imageCrop' 中的对象的潜在泄漏

ios - Swift 类 "MyClass"没有名为 "My_Var"的成员

ios - 当 CAKeyframeAnimation 完成动画时有没有办法调用方法?

ios - 将结构传递给 OCMock invokeBlockWithArgs 以获得 AVCaptureSession