如何使用 Xcode 7 上的 swift 代码使按钮不可见?
How to make buttons invisible with swift code on Xcode 7?
我使用 Xcode 7 Beta 在 SpriteKit 上的游戏中放置了一些按钮,它们始终保持可见,我想让它们在游戏开始时不可见,在游戏结束时可见。我尝试使用 Bool 但它不起作用,因为它们位于不同的文件中 (类)。游戏开始和结束功能在GameScene.swift,按钮功能在GameViewController.swift.
我用游戏启动的代码是:
var isStarted = false
func start() {
isStarted = true
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if isGameOver {
restart()
} else !isStarted {
start()
所以游戏从触摸屏幕开始。
我用游戏结束的代码是:
var isGameOver = false
func gameOver() {
isGameOver = true
}
按钮在 GameViewController.swift 作为发件人:UIButton。
@IBAction func facebookShare(sender: UIButton){
let facebookShare : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
self.presentViewController(facebookShare, animated: true, completion: nil)
}
@IBAction func twitterShare(sender: UIButton) {
let twitterShare : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
self.presentViewController(twitterShare, animated: true, completion: nil)
}
@IBAction func leaderboard(sender: UIButton) {
}
@IBAction func removeAds(sender: UIButton) {
}
如你所见,我没有再完成两个按钮,无论如何我会在这之后完成。
我是初学者,希望您能有所帮助
好的,下面您将找到一种方法,如何将事件发送到正在侦听它的任何对象....
class ClassWithButtonsInIt: UIViewController {
@IBOutlet weak var buttonOne: UIButton!
@IBOutlet weak var buttonTwo: UIButton!
@IBOutlet weak var buttonThree: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("hideButtonsEvent"), name: "hideButtonsEvent", object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
private func hideButtonsEvent (notification: NSNotification) {
buttonOne.hidden = true
buttonTwo.hidden = true
buttonThree.hidden = true
}
}
class ClassWithoutButtonsThatTriggersTheHidingEvent {
private func triggerdFunctionThatHidesButtonsInOtherClass () {
NSNotificationCenter.defaultCenter().postNotificationName("hideButtonsEvent", object: nil)
}
}
编辑
是的,没错,抱歉,我的代码中也有一个小错误:-)
请更改此行:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("hideButtonsEvent"), name: "hideButtonsEvent", object: nil)
到这一行(见 hideButtonsEvent 后面的双点):
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("hideButtonsEvent:"), name: "hideButtonsEvent", object: nil)
还有这一行:
private func hideButtonsEvent (notification: NSNotification) {
到这一行(删除私有修饰符):
func hideButtonsEvent (notification: NSNotification) {
我使用 Xcode 7 Beta 在 SpriteKit 上的游戏中放置了一些按钮,它们始终保持可见,我想让它们在游戏开始时不可见,在游戏结束时可见。我尝试使用 Bool 但它不起作用,因为它们位于不同的文件中 (类)。游戏开始和结束功能在GameScene.swift,按钮功能在GameViewController.swift.
我用游戏启动的代码是:
var isStarted = false
func start() {
isStarted = true
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if isGameOver {
restart()
} else !isStarted {
start()
所以游戏从触摸屏幕开始。
我用游戏结束的代码是:
var isGameOver = false
func gameOver() {
isGameOver = true
}
按钮在 GameViewController.swift 作为发件人:UIButton。
@IBAction func facebookShare(sender: UIButton){
let facebookShare : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
self.presentViewController(facebookShare, animated: true, completion: nil)
}
@IBAction func twitterShare(sender: UIButton) {
let twitterShare : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
self.presentViewController(twitterShare, animated: true, completion: nil)
}
@IBAction func leaderboard(sender: UIButton) {
}
@IBAction func removeAds(sender: UIButton) {
}
如你所见,我没有再完成两个按钮,无论如何我会在这之后完成。
我是初学者,希望您能有所帮助
好的,下面您将找到一种方法,如何将事件发送到正在侦听它的任何对象....
class ClassWithButtonsInIt: UIViewController {
@IBOutlet weak var buttonOne: UIButton!
@IBOutlet weak var buttonTwo: UIButton!
@IBOutlet weak var buttonThree: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("hideButtonsEvent"), name: "hideButtonsEvent", object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
private func hideButtonsEvent (notification: NSNotification) {
buttonOne.hidden = true
buttonTwo.hidden = true
buttonThree.hidden = true
}
}
class ClassWithoutButtonsThatTriggersTheHidingEvent {
private func triggerdFunctionThatHidesButtonsInOtherClass () {
NSNotificationCenter.defaultCenter().postNotificationName("hideButtonsEvent", object: nil)
}
}
编辑
是的,没错,抱歉,我的代码中也有一个小错误:-) 请更改此行:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("hideButtonsEvent"), name: "hideButtonsEvent", object: nil)
到这一行(见 hideButtonsEvent 后面的双点):
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("hideButtonsEvent:"), name: "hideButtonsEvent", object: nil)
还有这一行:
private func hideButtonsEvent (notification: NSNotification) {
到这一行(删除私有修饰符):
func hideButtonsEvent (notification: NSNotification) {