我正在使用 Storyboard在 Xcode 6 GM 中快速开发一个应用程序。在我的 View Controller 上,如果我拖放任何 View 并将其与相关 View Controller 文件中的 IBOutlet 连接,则该 socket 在 viewDidLoad 中始终为 nil,并且如果我尝试与该 socket 交互(例如更改按钮的标题),我总是会收到 BAD_INSTRUCTION 错误或其他任何原因,因为引用的导出始终为零,我不明白为什么..我已经检查了 Storyboard上的 viewcontroller 是否归我在自定义类选项下的身份检查器中的 viewcontroller 文件所有..这是 Xcode 和某人的问题吗其他人遇到同样的问题还是我做错了什么?
编辑 1:
这是启动的第一个 View Controller
import UIKit
类 View Controller :UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let vc = segue.destinationViewController as TableViewController
vc.prepare(/*initializing things..*/)
}
//这是带有我的 tableview socket 和自定义单元格的 viewcontroller
import UIKit
类 TableViewController:UIViewController、UITableViewDelegate、UITableViewDataSource {
@IBOutlet var tableView: UITableView!
//this array contains the date to display in cells and in viewDidLoad is correctly initialized with all the right data..
private var toVisualize:[Visualizable] = []
override func viewDidLoad() {
super.viewDidLoad()
/*I need to initialize the tableview even if it's an outlet rightly connected to the storyboard because it's always nil when viewDidLoad is called*/
self.loadArticles()
self.tableView = UITableView(frame: self.tableView.frame, style: UITableViewStyle.Plain)
//navigationController?.hidesBarsOnSwipe = true
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.allowsSelection = true
var cell = UINib(nibName: "MyCell", bundle: nil)
/*first I've used a prototype cell defined in the storyboard and a class with the related outlets and all connected in the right way but the result was a blank screen so I've tried to use a xib file to define the layout of my custom cell and the tableview started showing my cells.. but still I can't figure out why the tableview is nil*/
self.tableView.registerNib(cell, forCellReuseIdentifier: "Cell")
//self.tableView.registerClass(CellaTableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
self.view.addSubview(self.tableView)
self.tableView.reloadData()
// Do any additional setup after loading the view.
}
func prepare(/*preparing function used from first view controller..*/)->()
{
//this func is not changing anything because I've added it later..
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//this function is correctly working and showing the cells when the tableview is initialized
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView?.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MyCell
var toLoad = self.toVisualize[indexPath.row].getVisualizableProperties()
cell.loadItem(toLoad.title, descr: toLoad.descr, date: toLoad.date)
if let URL = toLoad.thumbUrl
{
var image = self.imageCache[URL]
if( image == nil ) {
// If the image does not exist, we need to download it
var imgURL: NSURL = NSURL(string: URL)
// Download an NSData representation of the image at the URL
let request: NSURLRequest = NSURLRequest(URL: imgURL)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
if error == nil {
image = UIImage(data: data)
// Store the image in to our cache
self.imageCache[URL] = image
dispatch_async(dispatch_get_main_queue(), {
if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) as? MyCell {
cellToUpdate.thumbnail?.image = image
}
})
}
else {
println("Error: \(error.localizedDescription)")
}
})
}
else {
dispatch_async(dispatch_get_main_queue(), {
if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) as? MyCell {
cellToUpdate.thumbnail?.image = image
}
})
}
}
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView!,heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat
{
return 120.0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.toVisualize.count
}
首先,我使用 presentViewController 以编程方式推送 TablewViewController,而我的 outlet tableview 在 viewDidLoad 中始终为 nil,然后我尝试使用 ViewController 中按钮的“Triggered Segues”操作来推送 TableViewController( View 中的 ViewController仅包含 4 个按钮,所有呈现 TableViewController 的 segues 操作。没有什么特别或奇怪的)并且 tableview 开始初始化,然后我注释掉了 tableview 和委托(delegate)设置的 init 方法(委托(delegate)和数据源已经在 Storyboard中设置) 但结果又是一个空白屏幕,什么也没有显示。我已经仔细检查了所有与 Storyboard 和各种标识符的连接,所以它们都设置正确了。无法弄清楚是什么问题
最佳答案
我在 Xcode 6.1 beta 中遇到了同样的问题,最终在从我的 viewDidLoad 方法中删除自定义单元格注册行后, socket 不再为 nil:
//删除这个:
self.tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "MyIdentifier")
在从 Storyboard实例化并以编程方式将 subview 推送或添加到另一个 View 时有效。
关于ios - Xcode 6 GM storyboard outlets always nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25978038/