简体   繁体   中英

A method is launched more than once in swift

I have a class:

class ViewController: UIViewController {
...
override func viewDidLoad() {
    super.viewDidLoad()
    ...
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(self.onDealLaunched),
        name: Notification.Name("newDealLaunched"),
        object: nil)
}
@objc func onDealLaunched(notification: NSNotification) { 
    let deal = notification.object as! SimpleSaveGame.deal
    let i = projectCollection.count
    let indexPath = IndexPath(row: i, section: 0)
    let projectDeal: project = project(...)

    projectCollection.append(projectDeal)
    activeDeals.append(deal)
    projectCollectionView!.numberOfItems(inSection: 0) 
    projectCollectionView.insertItems(at: [indexPath])
    projectCollectionView.reloadData()
}
    @IBAction func corpoButtonPressed(_ sender: Any) {
        let vcCorpo = UIStoryboard(name: "Corpo", bundle: nil).instantiateViewController(withIdentifier: "CorpoViewController") as! CorpoViewController
        vcCorpo.currentRound = second
        vcCorpo.yearlyTaxIncome = gameVariables[14].value
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(self.onReturnCorpo),
            name: Notification.Name("corpoBackButtonPressed"),
            object: nil)

        self.present(vcCorpo, animated: true, completion: nil)
    }
}

Then I have CorpoViewController which present custom UITableView and if a user clicks button it shows popup

class CorpoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
...
@objc func onLaunchButtonPressed(notification: NSNotification) { // it is launched when player presses negotiate button
    dealId = notification.object as! Int
    NotificationCenter.default.removeObserver(self, name: Notification.Name("dealLaunchButtonPressed"), object:dealId) 

        NotificationCenter.default.addObserver(
            self,
            selector: #selector(self.onPopupClosed),
            name: Notification.Name("negotiationPopupClosed"),
            object: nil)
        self.negotiatedDeal = self.deals[self.dealId]
        popup = NegotiationPopup(dealId: self.dealId, deal: self.negotiatedDeal, corpo: self.corpos[self.deals[self.dealId].corpo])
        self.view.addSubview(popup)

}
    @objc func onPopupClosed(notification: NSNotification) { 
        NotificationCenter.default.removeObserver(self, name: Notification.Name("negotiationPopupClosed"), object:nil)
        negotiatedDeal = notification.object as! SimpleSaveGame.deal
        if (negotiatedDeal.status == 2) {
            addActiveDeal(deal: negotiatedDeal)
            negotiatedDeal.status = 4 
            deals.remove(at: dealId)
        } 
    }
override func viewDidLoad() {
    super.viewDidLoad()

    corpoTableView.delegate = self
    corpoTableView.dataSource = self
    corpoTableView.register(CorpoCell.self, forCellReuseIdentifier: "cellId")
    
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(self.onLaunchButtonPressed),
        name: Notification.Name("dealLaunchButtonPressed"),
        object: nil)
    planTimer()
}
}

A class implementing:

class CorpoCell: UITableViewCell {
    ...
    @IBAction func launchButtonPressed(_ sender: UIButton) {
        NotificationCenter.default.post(name: Notification.Name("dealLaunchButtonPressed"), object: launchButton.tag)
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupView()
    }
}

And a class representing the popup:

    class NegotiationPopup: UIView {
    ...
        @objc fileprivate func animateOut() {
                    self.removeFromSuperview()
                    NotificationCenter.default.post(name: Notification.Name("negotiationPopupClosed"), object: self.negotiatedDeal)
                }
            }
    @objc fileprivate func cancelButtonPressed() {
   ...
        animateOut()
        }
    }

My problem is that when close popup method animateOut() is launched once, but method @objc func onPopupClosed is launched many times. Number of times depends on how many times I close CorpoViewController and then open it again.

Therefore I guess that I have multiple instances of CorpoViewController in memory, but I do not know how to confirm it and how to fix it.

I replaced observer which indicated that popup was closed with delegate and it fixed the problem somehow.

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