gpt4 book ai didi

ios - 从另一个 View 单击时如何隐藏按钮

转载 作者:行者123 更新时间:2023-11-30 14:13:33 25 4
gpt4 key购买 nike

我是xcode编程的初学者。我正在尝试执行一个操作,当我单击 A View 中的按钮时,B View 的按钮将被隐藏。我已经知道我可以使用 button.hidden = true; 作为自身 View Controller ,但我不知道如何从其他 View 控制按钮。

谢谢

@IBAction func TestBut(sender: UIButton) {
setting.hidden = false
}

最佳答案

之前,您使用按钮、按钮操作和协议(protocol)创建自定义 View ,如下所示:

protocol CustomViewDelegate {
func buttonPressed (sender: AnyObject)
}

class CustomView: UIView {

@IBOutlet weak var button: UIButton!
var delegate: CustomViewDelegate!

override func awakeFromNib() {
super.awakeFromNib()
}

class func loadViewFromXib() -> CustomView {
return NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)[0] as! CustomView
}

@IBAction func buttonPressed(sender: AnyObject) {
self.delegate.buttonPressed(sender)
}
}

在你的 ViewController 中。

class ViewController: UIViewController, CustomViewDelegate {

var firstView: CustomView?
var secondView: CustomView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

self.firstView = CustomView.loadViewFromXib()
self.secondView = CustomView.loadViewFromXib()

firstView!.frame = CGRectMake(0, 0, 100, 100)
secondView!.frame = CGRectMake(0, 200, 100, 100)

firstView!.delegate = self
secondView!.delegate = self

self.view.addSubview(firstView!)
self.view.addSubview(secondView!)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func buttonPressed(sender: AnyObject) {
if (sender as! UIButton) == self.firstView!.button {
self.secondView?.button.hidden = true
}else {
self.firstView?.button.hidden = true
}
}
}

关于ios - 从另一个 View 单击时如何隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31490667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com