2016-08-24 101 views
1

我正在使用Swift,並且正在修復iOS 8的一些bug。我使用.xib創建了一個視圖控制器。例如,一個視圖控制器和一個表格視圖。iOS8 Swift無法使用xib創建視圖控制器

這是我如何創建我的.xib文件。

1.I拖動的tableView到視圖,enter image description here 2.將類文件的所有者,enter image description here 3.拖動以文件的所有者enter image description here 4.drag了的tableView到文件的所有者enter image description here,我有這樣的代碼在文件的所有者

class BicycleServiceInfoController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutLet tableView: UITableView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.delegate = self 
     tableView.dataSource = self 
    } 

    //other functions 
    ... 
} 

但是當我運行這個應用程式的iOS 8.4,並嘗試提出這個觀點控制器,它會崩潰。 Xcode說:「意外地發現零,同時解開可選值」。這是因爲tableView在這段代碼中是無效的。但它在iOS 9中運行良好。

有人能幫助我嗎?

+2

,你如何創建視圖 - 控制自己? – ogres

回答

0

設置viewWillAppear函數中的代表。或者在界面生成器中。 iOS 8有一些比需要更晚的創建屏幕的問題。

2
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { 
    let classString = String(describing: type(of: self)) 
    if Bundle.main.path(forResource: classString, ofType: "nib") == nil { 
     super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) 
    } else { 
     super.init(nibName: nibNameOrNil ?? classString, bundle: nibBundleOrNil) 
    } 
} 
required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

在iOS 8上,您需要調用此方法,在iOS 9或更高版本中,系統修復此錯誤。 你可以得到託南已經提供正確答案的答案here

+0

只是我,還是有太多的大括號?或者格式不好? – jww

+1

這是正確的答案。解決問題。感謝那! – OutOfBounds

1

,但大家誰鬥爭這個轉換爲雨燕2.3,在這裏你去:

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { 
    let classString = String(MyClassName) 
    if NSBundle.mainBundle().pathForResource(classString, ofType: "nib") == nil{ 
     super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) 
    } else { 
     super.init(nibName: nibNameOrNil ?? classString, bundle: nibBundleOrNil) 
    } 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 
相關問題