得到一些奇怪的结果,我正在加载一个我创建的自定义警报视图的nib文件。但是,当它尝试更改自定义警报视图中的UILabel文本时,它在viewDidLoad方法中失败。 (得到解开零错误)
当我转到nib文件时,我的所有插座都已连接(显示它们旁边的突出显示的点)。文件的所有者也设置为正确的类名。
然而,当代码失败并启动调试器时,所有插座都是空的。此代码发生在viewDidAppear中,所以不应该在那时加载出口和所有UI?
辅助信息: 另一个奇怪的部分是我在两个不同的设备上测试它。一个运行iOS 8.4,另一个运行iOS 9.1(beta)。 iOS 9.1设备正常运行,并且不会抛出此nil错误。 iOS 8.4设备是抛出错误的设备。运行Xcode 6.4 swift 1.2。为了让代码在我的iOS 9.1上运行,我打开Xcode 7,我的设备连接到计算机,然后关闭并重新打开Xcode 6.4,它允许我将我的目标iOS设置为9.1。
更新
Nib文件视图控制器:
@IBOutlet var messageLabel: UILabel!
@IBOutlet var notificationHeightConstraint: NSLayoutConstraint!
@IBOutlet var notificationWidthConstraint: NSLayoutConstraint!
@IBOutlet var singleButton: UIButton!
@IBOutlet var rightButton: UIButton!
@IBOutlet var leftButton: UIButton!
@IBOutlet var middleButtonSpacerView: UIView!
var buttonDelegate: NotificationButtonDelegate?
var grayView: UIView!
var buttonTitles = [String]()
var notificationWidth: CGFloat!
var notificationHeight: CGFloat = 200.0
var hideProgressView = false
var hideNotificationView = false
var currentProgress: CGFloat = 0.0
var messageLabelText = ""
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
messageLabel.text = messageLabelText
if buttonTitles.count > 0 {
if buttonTitles.count == 1 {
rightButton.hidden = true
leftButton.hidden = true
middleButtonSpacerView.hidden = true
singleButton.setTitle("\(buttonTitles[0])", forState: UIControlState.Normal)
} else {
leftButton.setTitle("\(buttonTitles[0])", forState: UIControlState.Normal)
rightButton.setTitle("\(buttonTitles[1])", forState: UIControlState.Normal)
}
}
if self.hideProgressView {
self.progressView.hidden = true
if messageLabel.bounds.height >= 90 {
notificationHeightConstraint.constant = 225
} else {
notificationHeightConstraint.constant = 200.0
}
}
if self.hideNotificationView {
self.notificationView.hidden = true
self.activityIndicator.startAnimating()
notificationHeightConstraint.constant = 160
}
self.notificationWidthConstraint.constant = notificationWidth
self.view.needsUpdateConstraints()
// Do any additional setup after loading the view.
}
主视图控制器中的自定义警报视图类初始化
var notificationPopup: NotificationPopupViewController!
var progressPopup: NotificationPopupViewController!
override func viewDidLoad() {
super.viewDidLoad()
self.hasError = false
notificationPopup = NotificationPopupViewController()
progressPopup = NotificationPopupViewController()
在主viewController中调用以显示自定义提醒视图
@IBAction func configureSystemButtonTapped(sender: AnyObject) {
var prefs: NSUserDefaults = NSUserDefaults.standardUserDefaults()
var hostedSSID = prefs.valueForKey("HostedSSID") as? String
var currentSSID = sureVue.getUserSSID()
if currentSSID != hostedSSID || currentSSID == "" {
self.notificationPopup.showInView(self.view, withMessage: "You are not connected to the device's wifi.\nFollow instructions on launch page to connect.", animated: false, buttonLabels: ["Ok"])
nib viewController中设置自定义警报视图的方法
func showInView(aView: UIView!, withMessage message: String!, animated: Bool, buttonLabels:[String]) {
self.hideNotificationView = false
self.hideProgressView = true
self.buttonTitles = buttonLabels
notificationWidth = aView.bounds.width * 0.925
grayView = UIView(frame: aView.frame)
grayView.backgroundColor = UIColor(red: 170/255.0, green: 170/255.0, blue: 170/255.0, alpha: 0.75)
grayView.center = CGPoint(x: aView.bounds.width/2, y: aView.bounds.height/2)
grayView.addSubview(self.view)
self.view.center = CGPoint(x: aView.bounds.width/2, y: aView.bounds.height/2)
aView.addSubview(grayView)
messageLabelText = message
if animated
{
self.showAnimate()
}
}
答案 0 :(得分:1)
根据评论,我认为问题在于self.notificationPopup的初始化方式。您需要使用init(nibName: ...)
而不是notificationPopup = NotificationPopupViewController()
才能从笔尖加载实例并设置其出口。
现有代码在iOS 9中正常运行,因为如果在初始化UIViewController时没有指定,则Apple已更改了使用的默认nibNames。请参阅iOS9 release notes中的UIKit下的注释。
答案 1 :(得分:0)
如果正确使用init(nibName: ...)
,请尝试 -
有时,在添加新文件时,Xcode 7会错误地构建应用程序。