从另一个视图中单击时如何隐藏按钮

时间:2015-07-18 11:25:20

标签: ios xcode swift

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

由于

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

2 个答案:

答案 0 :(得分:0)

按钮必须是视图(或视图控制器)的属性。 电话会是这样的:

view.button.hidden = true

答案 1 :(得分:0)

之前,您可以使用按钮,按钮操作和协议创建自定义视图:

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
    }
  }
}