为什么View.cidLoad()中的self.currentViewController为零?

时间:2017-01-13 16:06:33

标签: ios swift viewcontroller

最近更改了我的项目中的哪个View是起点(在IB中指向它的箭头),现在当应用程序在模拟器中启动时出现以下错误:

  

致命错误:在解包可选值时意外发现nil

错误发生在视图的ViewDidLoad()期间 以前是 的起点,但现在不是。老实说,甚至不确定它为什么要加载,但那不是Swift的一部分,我知道如何确认这个视图(也)是否在预期启动时加载。发生错误的代码是:

//////////////////////////////////////////////////////////
// In web.config...

<system.webServer>
   ... 
   <httpProtocol>
      <customHeaders>
         <add name="Content-Security-Policy" value="default-src 'none'; 
            script-src 'self' 'unsafe-inline' 'unsafe-eval' *.ourdomain.com www.google-analytics.com; 
            connect-src 'self' *.ourdomain.com; img-src 'self' www.google-analytics.com; 
            style-src 'self' 'unsafe-inline';" />
      </customHeaders>
   </httpProtocol> 
</system.webServer>


//////////////////////////////////////////////////////////
// In PayorManager.aspx (e.g. https://www.ourdomain.com/Billing/PayorManager.aspx)

var url = "Payor.aspx?mode=Edit&payorID=392&sec=19";
var windowName = "PayorDetail";
var params = "width=980,height=775,left=150,top=124.5,alwaysRaised,scroll=0";

var childWindow = window.open(url, windowName, params);

// When the above childWindow closes, this gets executed...
 function _onChildCloseWrapper(childWindow, onChildClose) {
    if (onChildClose) {
       // THIS is where I get a "SCRIPT70: Permission denied" exception when trying to access
       // the returnValue parameter
       onChildClose(childWindow.returnValue);
    }

    return true;
};


//////////////////////////////////////////////////////////
// In Payor.aspx (e.g. https://www.ourdomain.com/Billing/Payor.aspx),
// After postback and data saved

window.onload = function () {
   if (closeWindow == "1") {
      window.returnValue = "SAVED";
      self.close();
   }
};

通过测试,self.currentViewController实际上现在是零,但我不清楚如何解决这个问题。在更改之前,当这个视图开始时,它运行正常。需要发生什么才能使self.currentViewController不再为零?

2 个答案:

答案 0 :(得分:1)

与xcode 9和swift 3相同的问题。 我修复了它以这种方式投射ViewController:

if let destinationViewController = self.storyboard?.instantiateViewController(withIdentifier: "ReaIDController") as? DetalleIpadViewController 

答案 1 :(得分:0)

你必须致电

super.viewDidLoad()

viewDidLoad函数的第一行。

然后一个正确的解决方法是停止强行打开选项

之前: BAD

// Code to enable the containerView to work:
self.currentViewController = self.storyboard?.instantiateViewController(withIdentifier: "SmallViewWithinMainView")
self.currentViewController!.view.translatesAutoresizingMaskIntoConstraints = false
self.addChildViewController(self.currentViewController!)

之后: GOOD

    // Code to enable the containerView to work:
        if let curView = self.storyboard?.instantiateViewController(withIdentifier: "SmallViewWithinMainView") {
            curView.view.translatesAutoresizingMaskIntoConstraints = false
            self.addChildViewController(curView)
        } else {
            // Error
        }
相关问题