Unexpected error on adding a button action in the Keyboard Extension

孤街浪徒 提交于 2020-02-06 07:57:54

问题


I'm developing a little app and I want to implement a responder function when a button is clicked in a view (ExtraView) inside the Keyboard Extension. Unfortunatelly it has been impossible to me due to the following errors that appear whhen I push the button (the apps doesn't crashes but the functionality implemented for the button doesn't works):

2020-01-31 03:27:36.724821+0100 MyApp[1841:354840] [lifecycle ] [u F8708363-74FE-4BA6-B64F-7DA7B719F80D:m (null)] [MyApp.Keyboard(1.0)] Connection to plugin invalidated while in use.

2020-01-31 03:27:37.797067+0100 MyApp[1841:354676] viewServiceDidTerminateWithError:: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted}

And the code that causes the error is:

    import UIKit

    @available(iOSApplicationExtension 9.0, *)
    class CatboardBanner: ExtraView {

        var scrollView = UIScrollView()
        var stack = UIStackView()
        let button1 = UIButton()

        required init(globalColors: GlobalColors.Type?, darkMode: Bool, solidColorMode: Bool) {
            super.init(globalColors: globalColors, darkMode: darkMode, solidColorMode: solidColorMode)
            self.backgroundColor = UIColor.white.withAlphaComponent(0.01)
            self.addSubview(scrollView)
            self.scrollView.translatesAutoresizingMaskIntoConstraints = false
            self.scrollView.addSubview(self.stack)
            self.stack.translatesAutoresizingMaskIntoConstraints = false
            self.stack.axis = .horizontal
            self.stack.spacing = 10

            button1.setTitle("btn 1", for: .normal)
            button1.backgroundColor = UIColor.lightGray
            button1.roundedCorners(rounding: 5)
            button1.translatesAutoresizingMaskIntoConstraints = false
            button1.addTarget(button1, action:#selector(buttonClicked), for: .touchUpInside)

            self.stack.addArrangedSubview(button1)
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        override func layoutSubviews() {
            super.layoutSubviews()

            self.scrollView.showsHorizontalScrollIndicator = false
            self.scrollView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true;
            self.scrollView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true;
            self.scrollView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true;
            self.scrollView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true;

            let topStack = NSLayoutConstraint(item: stack, attribute: .top, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1, constant: 10)
            let bottomStack = NSLayoutConstraint(item: stack, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1, constant: 0)
            NSLayoutConstraint.activate([topStack,bottomStack])
            self.stack.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor).isActive = true
            self.stack.trailingAnchor.constraint(equalTo: self.scrollView.trailingAnchor).isActive = true
            let leadingConstraint = NSLayoutConstraint(item: button1, attribute: .leading, relatedBy: .equal, toItem: self.stack, attribute: .leading, multiplier: 1, constant: 10)

            let widthConstraint = NSLayoutConstraint(item: button1, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)

            NSLayoutConstraint.activate([leadingConstraint, widthConstraint])
        }

        @objc func buttonClicked() {
            print("Button Clicked")
        }
    }

来源:https://stackoverflow.com/questions/59997286/unexpected-error-on-adding-a-button-action-in-the-keyboard-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!