简体   繁体   English

在通过模态方式执行Segue之后获得“发现无”异常

[英]Getting 'found nil' exception after performing Segue through present modally

I have to view controllers, from first controller's menu item click, a segue is performed by 'Present Modally' Segue which will open the second viewController (acting as a menu) when i press Refresh button on the second VC, it is calling the method from the first VC To refresh its data and load it on tableView. 我必须查看控制器,从第一个控制器的菜单项单击,“ Present Modally” Segue执行一个segue,当我按下第二个VC上的“刷新”按钮时,它将打开第二个viewController(充当菜单),从第一个VC刷新其数据并将其加载到tableView上。 the problem is it is call the method from second to first VC but is returning nil on this line. 问题是它是从第二个VC到第一个VC调用方法,但在此行返回nil。

    self.tableView.dataSource = self 

i think it is because the first VC is not active properly as it is in the background ( the second VC's background is transparent so that i can see the first VC at the back ). 我认为这是因为第一个VC不能在后台正常激活(第二个VC的背景是透明的,所以我可以在后面看到第一个VC)。 i'm new to swift and i don't know how to deal with it. 我是新手,我不知道如何处理它。

EDIT: This is the refresh method 编辑:这是刷新方法

   func refresh() {  

   //   self.dismiss(animated: true, completion: nil)
  //  present(DayView_Controller, animated: true, completion: nil)

   // self.dismiss(animated: true, completion: nil) // it is dismissing the first view controller instead of second



    ////////
    Extensions.ShowAlert(self.pwait, sender: self) // here it causing the exception on viewDidLoad
    let defaults = UserDefaults.standard
    let date = Date()
    let formatter = DateFormatter()
    formatter.dateFormat = "MM/d/yyyy"
    let finaldate = formatter.string(from:date)
    if Reachability.isConnectedToNetwork(){
    // all the services are being call and getting reloaded on tableview
  }

 override func viewDidLoad() {
    super.viewDidLoad()


    self.tableView.dataSource = self // exception here
  }

2nd EIDT: now i'm dismissing the 2nd VC on the refresh click and call the method from 1st VC on the completion, still getting the same error. 第二个EIDT:现在,我在刷新单击时关闭了第二个VC,并在完成时从第一个VC调用了该方法,仍然遇到相同的错误。

 @IBAction func Refresh_CLick(_ sender: Any) {
    self.dismiss(animated: true, completion: {
        DayView_Controller().refresh()

    })
  //  DayView_Controller().refresh()

}

I believe, you need to do something like this: 我相信,您需要执行以下操作:

1) Create protocol: 1)创建协议:

protocol RefreshDelegate {
    func dataChanged(data: [Items])
}

2) In your second view controller add: 2)在第二个视图控制器中添加:

var delegate: RefreshDelegate ?

if let del = delegate {
     del.dataChanged(data: items) // your refreshed data
}

3) In your first view controller add extension: 3)在您的第一个视图控制器中添加扩展名:

extension FirstViewController: RefreshDelegate {
    func dataChanged(data: [Items]) {
         // update your tableView data -> then reload
    }
}

Something like this. 这样的事情。 You should understand main idea. 您应该了解主要思想。

Hope it helps 希望能帮助到你

Use Delegate to refresh data 使用委托刷新数据

protocol RefreshDelegate: class {
    func refreshData(data: Int)
}

In your first view controller add delegate 在您的第一个视图控制器中添加委托

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, RefreshDelegate {

In your prepare for segue method set delegate 在您准备segue方法集的委托中

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "go" {
        let viewController:SecondViewController = segue.destination as! SecondViewController
        viewController.refreshDelegate = self
    }

}

// Delegate
    func refreshData(data: Int) {
        tableData = data
        self.tableView.reloadData()
    }

In second view controller add variable for delegate 在第二个视图控制器中添加代表变量

weak var refreshDelegate: RefreshDelegate?

Now call refresh delegate from second view controller 现在从第二个视图控制器调用刷新委托

@IBAction func refreshTapped(_ sender: UIButton) {
    self.dismiss(animated: true, completion: {
        self.refreshDelegate?.refreshData(data: 10)
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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