【问题标题】:UIButton not working loaded in a child ViewControllerUIButton 无法在子 ViewController 中加载
【发布时间】:2017-09-08 02:27:33
【问题描述】:

我有一个MainViewController 我正在加载一个滑出菜单。 MainViewController 包含一个静态的TopView 和一个ContentView,其中加载了不同的子控制器或视图,具体取决于选择的菜单条目。

其中一个子项内的按钮已加载,但无法正常工作。 当我使用包含按钮的 ViewController 启动应用程序时,设置为“是初始视图控制器”,一切正常。

当使用 MainViewController 作为初始视图控制器启动应用程序并加载子项时,按钮不起作用(它已加载到视图中,但没有反应)。 有什么建议可以让我完成这项工作吗?

MainViewController 加载子视图

class MainViewController: UIViewController, SideBarDelegate {
var contentView = UIView()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    ....
    contentView.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
    contentView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(contentView)
    contentView.topAnchor.constraint(equalTo: statusBarView.bottomAnchor).isActive = true
    contentView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    contentView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    contentView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
    contentView.layoutIfNeeded()

}

func sideBarDidSelectButtonAtIndex(_ index: Int) {
    ...       
    if index == 0{
        statusBarLabel.text = "First"
        let controller:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController
        controller.view.frame = ContentView.bounds
        controller.willMove(toParentViewController: self)
        contentView.addSubview(controller.view)
        controller.didMove(toParentViewController: self)
        print("touched index 0")
    } else if index == 1{
        // index is 1
    }
}

FirstViewController

class FirstViewController: UIViewController, UIScrollViewDelegate {

    let addButton = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)

        addButton.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)
        addButton.clipsToBounds = true
        addButton.setTitleColor(UIColor.white, for: .normal)
        addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
        addButton.setTitle("add feed", for: .normal)
        addButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: UIFontWeightRegular)
        self.view.addSubview(AddButton)
        addButton.layoutIfNeeded()
        addButton.addTarget(self, action: #selector(touchUpAddButton), for: UIControlEvents.touchUpInside)
        addButton.addTarget(self, action: #selector(touchDownAddButton), for: UIControlEvents.touchDown)

    func touchUpAddButton() {
        addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
    }

    func touchDownAddButton() {
        addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
    }
}

【问题讨论】:

    标签: ios uiviewcontroller uibutton uikit uiresponder


    【解决方案1】:

    在容器中添加视图时,您不必使用 addSubview 函数,但 FirstViewController 功能不起作用。

    使用下面的代码:

    let newViewController:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController
    let oldViewController  = childViewControllers.last! as UIViewController
        oldViewController.willMove(toParentViewController: nil)
        addChildViewController(newViewController)
        transition(from: oldViewController, to: newViewController, duration: 0.25, options: .transitionCrossDissolve, animations:{ () -> Void in
            // nothing needed here
        }, completion: { (finished) -> Void in
            oldViewController.removeFromParentViewController()
            newViewController.didMove(toParentViewController: self)
        })
    

    你可以改变持续时间,它用于动画

    【讨论】:

    • 我必须将它添加为子视图,因为我的 MainViewController 中有两个视图,即 TopView 和 ContentView(我想在其中添加我的 First- 和 SecondViewController)。还是我没有得到您的解决方案?
    • 你的 ContentView 是不是一种 UIContainerView?用 UIContainerView 替换 MainViewController 中的视图,它将完美地代替 UIView
    【解决方案2】:

    将你的函数更新到这些

    func TouchUpAddButton(sender: UIButton){
        addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1)
    
    }
    
    func TouchDownAddButton(sender: UIButton) {
        addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)
    }
    

    并更改将目标添加到的代码

    addButton.addTarget(self, action: #selector(FirstViewController.touchUpAddButton(_:)), for: .touchDown)
    addButton.addTarget(self, action: #selector(FirstViewController.touchDownAddButton(_:)), for: .touchDown)
    

    【讨论】:

    • 和 sender 将是可以更改的按钮,而不是 addButton
    • 没错,你可以在 touchUp 和 touchDown 函数中将 AddButton 更改为 sender。
    • 它仍然无法工作,现在它在编译时给我一个错误:使用未解析的标识符'TouchUpAddButton'
    • 你把代码改成我的了吗?函数末尾有 (sender: UIButton) 吗?该错误是说它看不到 TouchUpAddButton 功能
    • 是的,我确实像您所说的那样更改了代码,因此将 (sender: UIButton) 用于这两个函数并将选择器更改为 (_:)
    【解决方案3】:

    第一个解决方案:

    删除自动布局约束

    第二种解决方案:

    Button 未执行操作,因为应将子视图控制器视图直接添加到容器控制器视图(在您的情况下:MainViewController 视图),而不是通过中间视图(在您的情况下: 内容视图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 2015-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多