关闭当前模态视图后如何重新加载collectionview?

时间:2020-07-11 18:07:59

标签: ios swift user-interface frontend viewcontroller

我有一个带有两个选项卡栏项目的选项卡栏,并在其上添加了一个中央addButton,这可以使我转到添加项viewcontroller。这是中间按钮的设置。

 func setupMiddleButton() {
    
    let tabBarHeight = tabBar.frame.size.height
    
   
    let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: tabBarHeight*1.5, height: tabBarHeight*1.5))
    var menuButtonFrame = menuButton.frame
    
    menuButtonFrame.origin.y = view.bounds.height - menuButtonFrame.height/2 - tabBarHeight - 8

    menuButtonFrame.origin.x = view.bounds.width/2 - menuButtonFrame.size.width/2
    menuButton.frame = menuButtonFrame
    
    menuButton.backgroundColor = UIColor.red
    menuButton.layer.cornerRadius = menuButtonFrame.height/2
    view.addSubview(menuButton)
    
    let largeConfiguration = UIImage.SymbolConfiguration(scale: .large)
    let addIcon = UIImage(systemName: "plus", withConfiguration: largeConfiguration)
    menuButton.setImage((addIcon), for: .normal)
    
    menuButton.addTarget(self, action: #selector(menuButtonAction(sender:)), for: .touchUpInside)
    
    view.layoutIfNeeded()
}

@objc private func menuButtonAction(sender: UIButton) {
      let addVC = AddViewController()
      let navigationController = UINavigationController(rootViewController: addVC)
      
      performSegue(withIdentifier: "addEventSegue", sender: sender)
    

    
}

,我在其中一个标签栏视图中执行此操作以重载Data,但是我发现在结束编辑并以模态关闭当前内容后,collectionView无法重载

override func viewWillAppear(_ animated: Bool) {
    lifeCollectionView.delegate = self
    lifeCollectionView.dataSource = self
    self.fetchData()
    DispatchQueue.main.async {
        self.lifeCollectionView.reloadData()
    }
    
}

我以模态方式解散该呈现后,应该怎么做才能重新加载收藏视图?非常感谢!

2 个答案:

答案 0 :(得分:0)

有多种方法可以实现委托,闭合和modalPresentation样式

方法1: 如果您添加navigationController.modalPresentationStyle = .fullScreen,则会调用显示控制器的控制器的viewWillAppear,并且您的reload方法将被调用

方法2:代表和协议

在“第二个控制器”中声明一个类似于以下协议的协议

protocol CallParent{
    func reloadCollection()
}

在Second Controller中声明一个属性以保存确认此协议的视图控制器的引用

weak var myParent : CallParent?

现在在关闭前调用reloadCollection

 if let parent = myParent {
    parent.reloadCollection()
}

确认具有CallParent协议的第一个控制器

class FirstVC: UIViewController,CallParent{

然后在调用segue时

@objc private func menuButtonAction(sender: UIButton) {
      let addVC = AddViewController()
       addVC.myParent = self 
      let navigationController = UINavigationController(rootViewController: addVC)
      
      performSegue(withIdentifier: "addEventSegue", sender: sender)
    

    
}

方法3:在您提供的控制器中,调用dismiss函数是这样的

 if let fvc = self.presentingViewController as? FirstController {

        self.dismiss(animated: true) {
             fvc.callReloadFunctionHere()
        }
    }

方法4:关闭

class SecondViewController: UIViewController {
    var onViewWillDisappear: (()->())?

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        onViewWillDisappear?()
    }
    ...
}

在FirstController中

 @objc private func menuButtonAction(sender: UIButton) {
          let addVC = AddViewController()
              addVC.onViewWillDisappear = {
                // reload collection view here 
            }
          let navigationController = UINavigationController(rootViewController: addVC)
          
          performSegue(withIdentifier: "addEventSegue", sender: sender)
        
    
        
    }

答案 1 :(得分:0)

设置集合视图数据源,仅在viewDidLoad中委派和重新加载。
您不需要每次使用viewWillAppear重新加载您的收藏夹视图!

使用代表传递消息。

protocol ContentChangedDelegate: class {
    func itemAdded()
}

AddViewController内添加以下变量

weak var delegate: ContentChangedDelegate? = nil

设置您的UIViewController子类实例,其中lifeCollectionView作为AddViewController的委托存在。

let addVC = AddViewController()
addVC.delegate = self
//push or present addVC

使您的视图控制器符合ContentChangedDelegate协议,并且可以在itemAdded方法中重新加载集合视图。

delegate?.itemAdded()中添加项目后致电AddViewController。这将在listVC中调用该方法,并且应立即更新。