简体   繁体   中英

Button visibly clicking but event not registering

Swift 3/iOS 10/Xcode 8

I have a view controller (pieChart) that contains a label, two buttons and an empty view (which will contain a pie chart). The label and two buttons are incorporated into a horizontal stack view, which lies above the pie chart view.

The above VC is embedded into one of four container views (The main screen of the app is comprised of these four container views) when the app starts up.

In pieChart, I have linked both buttons up to their respective IBActions and IBOutlets. When clicking on button 2, a modal segue should occur to another VC but this does not happen. Visibly, the button is registering the click - ie it changes colour when you click it. I have placed a print statement in the IBAction method for button 2 but this too does not display. No error messages are displayed in console either.

The only reasons for this occurring that I have found after several hours of hunting are:

  1. sub views have been added to the button itself so the click event signal passes by the button to be received by the added sub views. This is not the case for me. Order is Main App Window > Container View > Embedded VC > Stack View > Button. InteractionEnabled is set to true for all.

  2. Button lies partially outside containing view (it's height/width might be greater than its containing view). This is not the case for me. The stack view, label and both buttons share the same height and the width of the stack view is equal to the sum of the widths of the label and buttons plus the spacing between the label and buttons.

I have also tried adding an event handler programmatically with:

SelectAnalyisButtonOutlet.addTarget(self, action: #selector(SelectAnalysisButtonClicked), for: .touchUpInside)

but the same outcome occurs.

Are there any other reasons for the click events seemingly not registering?

EDIT 1

The pieChart VC mentioned above is one of several VC's that are swapped out of the same container view (called detailContainerView), depending on which button (all of which work just fine) is clicked in one of the OTHER container views (called TabBar).

I placed a button in each of two other VC's that get displayed in detailContainerView and hooked them each up to an IBAction. Each IBAction contains a print statement that fires when the button is clicked. At the moment then, these two VC's only consist of a label and the newly inserted buttons. None of the buttons worked when I ran the app.

I then set one of the VC's of detailContainerView as the Initial View Controller in the Attributes Inspector and re-ran the app. Suddenly the buttons now work! If I then hook the buttons up to a segue, the segues work too!

Something seems to change when I swap out the VC's in detailContainerView. The code I am using to swap the VC's out is as follows:

    func SwapOutControllers(vc: UIViewController, vcName: String){

    //REMOVE OLD VC
    detailPaneVCReference?.willMove(toParentViewController: nil)
    detailPaneVCReference?.view.removeFromSuperview()
    detailPaneVCReference?.removeFromParentViewController()

    var newVc: UIViewController?

    switch vcName {
    case "Biography":
        newVc = vc as! Biography

    case "Social Media":
        newVc = vc as! SocialMedia

    case "News Feed":
        newVc = vc as! NewsFeeds

    case "Stats":
        newVc = vc as! StatsAboutParliament

    case "Petitions":
        newVc = vc as! Petitions

    default:
        print("Error: No VC Found!")
    }

    //ADD NEW VC
    ParentVC?.addChildViewController(newVc!)

    let width = detailContainerView?.frame.width
    let height = detailContainerView?.frame.height

    newVc?.view.frame = CGRect(x: 0, y: 0, width: width!, height: height!)

    detailContainerView?.addSubview((newVc?.view)!)

    newVc?.didMove(toParentViewController: ParentVC)

}

detailPaneVCReference is a reference to whichever VC is currently being displayed by detailContainerView. ParentVC is the VC that contains the four container views.

The VC that is removed from the ParentVC still exists in the debugging view hierarchy after it has been removed/swapped out - could this be somehow blocking the click event from reaching the event handler?

SOLUTION!

The source of my problem has been that the references I had made to each of the view controllers that get swapped in and out of detailContainerView were incorrectly declared as weak references. I deleted "weak" (eg "weak var x: UIViewController?" --> "var x: UIViewController?") from each of the declarations and voila!, the code now works as intended!

The source of my problem has been that the references I had made to each of the view controllers that get swapped in and out of detailContainerView were incorrectly declared as weak references. I deleted "weak" (eg "weak var x: UIViewController?" --> "var x: UIViewController?") from each of the declarations and voila!, the code now works as intended!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM