我正在尝试使用函数configButtonLH()
和按钮名称menuButtonLH
中的按钮名称。概念是单击按钮,菜单展开,进行选择,然后隐藏menuButtonLH
。那部分起作用。我有一个onTap()
函数,我想用来取消隐藏它,但我无法访问menuButtonLH
。
我尝试过但没有运气:
@objc func onTap() {
let LH = configButtonLH()
LH.isHidden = True
self.preview.isHidden = true
self.lockScreen.isHidden = true
// cancel button
self.pageOutlet.isHidden = false // dismiss button is not hidden
}
fileprivate func configButtonLH() {
let menuButtonSize: CGSize = CGSize(width: 64.0, height: 64.0)
let menuButtonLH = ExpandingMenuButton(frame: CGRect(origin: CGPoint.zero, size: menuButtonSize), image: UIImage(named: "buttonA")!, rotatedImage: UIImage(named: "buttonAL")!)
menuButtonLH.center = CGPoint(x: 30, y: self.view.bounds.height - 65)
self.view.addSubview(menuButtonLH)
// button settings
menuButtonLH.layer.shadowOpacity = 0.8
menuButtonLH.layer.shadowRadius = 5
menuButtonLH.layer.shadowOffset = CGSize(width: 0, height: 6)
menuButtonLH.menuTitleDirection = .right
menuButtonLH.bottomViewAlpha = 0.09
//menuButton2.alpha = 0
// Lets hide the right button
let item1 = ExpandingMenuItem(size: menuButtonSize, title: "Home Screen", image: UIImage(named: "icon")!, highlightedImage: UIImage(named: "chooser-moment-icon-place-highlighted")!, backgroundImage: UIImage(named: "chooser-moment-button"), backgroundHighlightedImage: UIImage(named: "chooser-moment-button-highlighted")) { () -> Void in
self.preview.isHidden = false
self.pageOutlet.isHidden = true
// menuButton2.alpha = 0
}
let item2 = ExpandingMenuItem(size: menuButtonSize, title: "Lock Screen", image: UIImage(named: "lock")!, highlightedImage: UIImage(named: "chooser-moment-icon-place-highlighted")!, backgroundImage: UIImage(named: "chooser-moment-button"), backgroundHighlightedImage: UIImage(named: "chooser-moment-button-highlighted")) { () -> Void in
// showAlert "Share"
self.lockScreen.isHidden = false
self.pageOutlet.isHidden = true
menuButtonLH.isHidden = true
}
menuButtonLH.addMenuItems([item1, item2])
menuButtonLH.willPresentMenuItems = { (menu) -> Void in
print("MenuItems will present.")
}
menuButtonLH.didDismissMenuItems = { (menu) -> Void in
print("MenuItems dismissed.")
// menuButton2.isHidden = false
}
}
这是onTap函数:
@objc func onTap() {
self.preview.isHidden = true
self.lockScreen.isHidden = true
// cancel button
self.pageOutlet.isHidden = false // dismiss button is not hidden
}
答案 0 :(得分:0)
在显示的代码中,menuButtonLH在函数“ configButtonLH”的范围内定义。在函数外部不可见。
包含这些功能的类大概是视图控制器?
您可以将其定义为该类的实例属性,如下所示:
var menuButtonLH: ExpandingMenuButton!
它必须为“ var”,因此您可以在对象初始化后对其进行初始化,并提供一个未包装的可选内容,只要您保证它会在使用前进行初始化即可。
添加:
我在您发布的代码中看到的唯一未包装的可选参数是这些调用中的可失败初始化器:
let menuButtonLH = ExpandingMenuButton(frame: CGRect(origin: CGPoint.zero, size: menuButtonSize), image: UIImage(named: "buttonA")!, rotatedImage: UIImage(named: "buttonAL")!)
// Lets hide the right button
let item1 = ExpandingMenuItem(size: menuButtonSize, title: "Home Screen", image: UIImage(named: "icon")!, highlightedImage: UIImage(named: "chooser-moment-icon-place-highlighted")!, backgroundImage: UIImage(named: "chooser-moment-button"), backgroundHighlightedImage: UIImage(named: "chooser-moment-button-highlighted"))
let item2 = ExpandingMenuItem(size: menuButtonSize, title: "Lock Screen", image: UIImage(named: "lock")!, highlightedImage: UIImage(named: "chooser-moment-icon-place-highlighted")!, backgroundImage: UIImage(named: "chooser-moment-button"), backgroundHighlightedImage: UIImage(named: "chooser-moment-button-highlighted"))
如果其中一个引起故障,则可能由于某种原因而找不到您的一张或多张图像。
调试器应该告诉您程序在哪里爆炸。