一种方法在swift中多次启动
我有一门课:一种方法在swift中多次启动,swift,Swift,我有一门课: class ViewController: UIViewController { ... override func viewDidLoad() { super.viewDidLoad() ... NotificationCenter.default.addObserver( self, selector: #selector(self.onDealLaunched), name: Notification.N
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)
}
}
然后我有CorpoViewController,它提供自定义UITableView,如果用户单击按钮,它会显示弹出窗口
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()
}
}
实现以下功能的类:
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()
}
}
以及表示弹出窗口的类:
class NegotiationPopup: UIView {
...
@objc fileprivate func animateOut() {
self.removeFromSuperview()
NotificationCenter.default.post(name: Notification.Name("negotiationPopupClosed"), object: self.negotiatedDeal)
}
}
@objc fileprivate func cancelButtonPressed() {
...
animateOut()
}
}
我的问题是,当关闭弹出窗口方法animateOut()启动一次时,但方法@objc func onPopupClosed会启动多次。次数取决于我关闭CorpoViewController然后再次打开它的次数
因此,我猜我的内存中有多个CorpoViewController实例,但我不知道如何确认它以及如何修复它。我替换了observer,它表明弹出窗口已被委托关闭,并以某种方式修复了问题。您没有提供足够的信息。显然,您希望通过通知调用
onPopupClosed
。但是,您从未向我们显示使用onPopupClosed
选择器添加观察者的位置。最简单的解释是,您多次添加同一个观察者,但无法猜测,因为您完全忽略了相关代码。@matt谢谢您的评论,我更新了我的问题。谢谢matt的建议。我替换了observer,它表示弹出窗口已被委托关闭,并以某种方式修复了问题。:)好吧,现在您已经展示了更多的代码。整个事情都是通知的糟糕使用。通知不是通过代码传递信号的正确方式。请不要添加“谢谢”作为答案。相反,你认为这是最有帮助的-