简体   繁体   中英

View outlet nil after segue

I am working on the online Stanford IOS development course and am having a bug I don't know how to deal with. When I segue to the detail view in the split view controller I am able to set parameters of the detail UIView from the detail UIViewController in the didSet of outlet to the view. Later when I try to access or mutate the detail view from the controller the view variable(outlet) is nil even though the view is still on screen and responds to gestures. This is the prepare method of the master view in the split view controller

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {  
    let destinationViewController = segue.destination  
    if let graphViewController = destinationViewController as? GraphViewController {  
        if let identifier = segue.identifier{  
            switch identifier {  
            case "DisplayGraph":  
                graphViewController.function = funcForGraphView(_:)  
            default:  
                break  
            }  
        }  
    }  
}  

This is the detail view controller. I commented on the interesting parts.

class GraphViewController: UIViewController {  
@IBOutlet weak var graphView: GraphView!{  
    didSet{  
        graphView.addGestureRecognizer(UITapGestureRecognizer(target: graphView, action: #selector(graphView.tap(recognizer:))))  
        graphView.addGestureRecognizer(UIPanGestureRecognizer(target: graphView, action: #selector(graphView.pan(recognizer:))))  
        graphView.addGestureRecognizer(UIPinchGestureRecognizer(target: graphView, action: #selector(graphView.zoom(recognizer:))))  
        graphView.function = cos //I can set variables in the view here  
        // After this didSet I am no longer able to set or read any variables from the graphView  
    }  
}  
var function: ((Double)->(Double))? {  
    didSet{  
        if let newfunction = function {  
            graphView.function = newfunction // I can not set or access variables in the view here since for some reason graphView is nil  
        }  
    }  
}  

And this is the relevant variable in the detail view

var function: ((Double) -> Double)? {  
    didSet{  
        setNeedsDisplay()  
    }  
}  

Storyboard Picture Any help would be greatly appreciated and hopefully this isn't a stupid question. If more information would be helpful let me know. Thanks!

It will be nil because of your view not fully loaded while your are push viewcontrolle. so you needs to pass as below :

In GraphViewController

var yourValue : ((Double)->(Double))?

override func viewDidLoad() {
        super.viewDidLoad()
         self.function = yourValue
}

Set value while navigation

 if let identifier = segue.identifier{  
            switch identifier {  
            case "DisplayGraph":  
                graphViewController.yourValue = funcForGraphView(_:)  
            default:  
                break  
            }  
        } 

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