如何以编程方式创建一个按钮,以显示在单独的视图控制器swift中

时间:2016-05-20 08:01:41

标签: swift uibutton viewcontroller

我在ViewController创建了一个创建UIButton的函数。

当我在同一个ViewController中调用该函数时,在ViewDidLoad中,它可以正常工作。但是当我尝试使用我在弹出窗口中创建的按钮来调用该函数时,我会继续这个

  

错误“Thread1:EXC_BREAKPOINT(code = 1,subcode = 0x1002ce588)”。

我意识到这是一个常见的错误,但我似乎无法找到解决方法。

这就是我所拥有的。

class func addButtonToMainController()
{
    var button1: UIButton!
    button1 = UIButton(type: .System)
    button1.setTitle("placeholder", forState: UIControlState.Normal)
    button1.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
    button1.center = CGPoint(x: 0, y: 0)
    button1.titleLabel?.font = UIFont.systemFontOfSize(20)
    button1.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    button1.backgroundColor = UIColor.blueColor()
    button1.addTarget(MainViewController(), action: Selector("animateButtonPressed:"), forControlEvents: UIControlEvents.TouchUpInside)
    MainViewController().view.addSubview(button1)
}
func animateButtonPressed(sender: AnyObject){
    print("animate")
}

这是我从popover viewcontroller调用函数的地方。

@IBAction func buttonPressed(sender: AnyObject) {
    MainViewController.addButtonToMainController()
}

这里的要点是在弹出框中在主视图控制器上创建一个工作按钮。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

您所拥有的内容并非有效,因为每次致电MainViewController时,您都会创建MainViewController的新实例。您需要确保您正在与故事板创建的唯一MainViewController对话。

为了在另一个viewController中调用一个函数,你需要一个viewController的实例。使用showOver呈现的viewController的最佳方法是使用委托。

在下面的示例中,我使用了一个segue来显示SecondViewController。我已经定义了一个名为ButtonAdder的协议,它简单地描述了一个实现方法addButton的类。

然后,通过将MainViewController添加到ButtonAdder声明行并提供ButtonAdder方法,确保class实施addButton协议。

prepareForSegue中,将MainViewController(即self)的实例添加为delegate的{​​{1}}。

SecondViewController中,当您需要SecondViewController添加按钮时,请致电delegate?.addButton()

确保属性检查器中的segue标识符设置为MainViewController,以使下面的代码生效。

<强> MainViewController.swift

"presentPopover"

<强> SecondViewController.swift

import UIKit

protocol ButtonAdder: class {
    func addButton()
}

class MainViewController: UIViewController, ButtonAdder {

    func addButton() {
        var button1: UIButton!
        button1 = UIButton(type: .System)
        button1.setTitle("placeholder", forState: UIControlState.Normal)
        button1.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
        button1.center = CGPoint(x: 0, y: 0)
        button1.titleLabel?.font = UIFont.systemFontOfSize(20)
        button1.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button1.backgroundColor = UIColor.blueColor()
        button1.addTarget(self, action: Selector("animateButtonPressed:"), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button1)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "presentPopover" {
            let dvc = segue.destinationViewController as! SecondViewController
            dvc.delegate = self
        }
    }

    func animateButtonPressed(button: UIButton) {
        // do something
    }
}