简体   繁体   English

ViewController加载两次Xcode iOS

[英]ViewController loading twice xcode iOS

I know what causes it to load twice, but I do not know how to fix the problem. 我知道是什么原因导致它两次加载,但是我不知道如何解决该问题。 I have looked through many other questions at the same topic, but any of those are matching to my problem. 我在同一个主题下浏览了许多其他问题,但这些问题都与我的问题相符。

So here's the code 所以这是代码

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){

        let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
        super.performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)
        print("kolm")
    }

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "TVtoVCSegue" {
            let otherViewController = segue.destinationViewController as! TTDetailCntr

            if let cell = sender as? UITableViewCell, text = cell.textLabel?.text {
                otherViewController.CurrentClass = text
            }
        }
    }

As I understand, in tableview function 据我了解,在tableview功能

super.performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)

Calls out the view to load and the function below itself calls out the view to load, 调出要加载的视图,下面的函数本身调出要加载的视图,

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)

both will load the view so I tried to integrate those methods and I tried to do sth like this: 两者都将加载视图,因此我尝试集成这些方法,并尝试执行以下操作:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let indexPath = tableView.indexPathForSelectedRow! as NSIndexPath
    let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell

    super.performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)

    if segue.identifier == "TVtoVCSegue" {
        let otherViewController = segue.destinationViewController as! TTDetailCntr

        if let cell = sender as? UITableViewCell, text = cell.textLabel?.text {
            otherViewController.CurrentClass = text
        }
    }
}

But It says an error if I try to access the view that loaded twice before: The error comes from the line: 但是如果我尝试访问之前加载过两次的视图,则会显示错误:错误来自以下行:

super.performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)

And the error is: 错误是:

warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
(lldb) 

The idea of the code is to get the indexPath value and then pass it to another ViewController 代码的想法是获取indexPath值,然后将其传递给另一个ViewController。

The segue that you've created is most probably from a tableViewCell rather than the UIViewController 您所创建的赛格瑞是最有可能从tableViewCell而不是UIViewController中

Delete the segue and create one from the UIViewController instead. 删除序列并从UIViewController创建一个序列

从高度突出部分拖放

Ctrl + drag from the circle highlighted in the image to your destination VC. Ctrl +从图像中突出显示的圆圈拖动到目标VC。

This will solve this problem. 这样可以解决这个问题。

write

self.performSegueWithIdentifier

and check instead of 然后检查而不是

super.performSegueWithIdentifier

Its working fine with this 它与此工作正常

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){     
    let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
    self.performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)
    print("kolm")
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "TVtoVCSegue" {
        let otherViewController = segue.destinationViewController as! SecondViewController

        if let cell = sender as? UITableViewCell {
            otherViewController.CurrentClass = cell.textLabel?.text
        }
    }

}

SecondControllerClass.swift SecondControllerClass.swift

class SecondViewController: UIViewController {

var CurrentClass:String!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

Here is the SecondviewController that receives the passed data: 这是SecondviewController,用于接收传递的数据:

class TTDetailCntr: UIViewController {
    @IBOutlet var ClassIDLbl: UILabel!
    @IBOutlet var TeacherIDlbl: UILabel!

    var CurrentClass = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        ClassIDLbl.text = CurrentClass

    }

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

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