我正在用Xcode 6 GM开发一款应用程序,使用故事板。在我的viewcontroller上,如果我拖放任何视图并将其与相关视图控制器文件中的IBOutlet连接起来,那么该视图总是在viewDidLoad中为零,并且如果我尝试与该视图进行交互,例如更改按钮的标题,我总是会遇到BAD_INSTRUCTION错误或任何其他因为该引用的出口总是零,我不明白为什么..我已经检查故事板上的viewcontroller属于我的viewcontroller文件在身份检查器下的自定义类选项..是否是Xcode的问题和某人其他人遇到同样的问题,或者我做错了什么?Xcode 6 GM故事板插座始终为零
编辑1:
这是推出
import UIKit
类的ViewController第一的ViewController: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出口和视图 - 控制定制单元
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编程和我出口的tableview在viewDidLoad中总是零,然后我试着使用在我的ViewController按钮的“触发分段”动作来推动TableViewController(ViewController在它的视图中只包含4个按钮,呈现所有呈现TableViewController ..没有什么特别或奇怪的行动),并开始初始化tableview,然后我'已经注释掉了tableview的init方法和委托设置(委托和数据源已经设置在故事板),但结果再次是一个空白的屏幕显示没有..我已经双重检查与故事板和各种标识符的所有连接,所以他们都正确设置..不能出了什么问题
您可以发布您的代码? – 2014-09-22 23:00:37
我已经改变了我的代码。首先,我通过使用'code'presentViewController(vc,animated:true,completion:nil)'code'以编程方式推送viewcontroller,现在我添加了一个按钮单击操作, segue和现在我的outlet是一个tableview被初始化,而不是零,但现在的问题是,我的tableview的自定义单元格只在我使用xib文件来定义单元格布局而不是直接在故事板中的tableview与它的相关模型类和相应的插座.. – user3676870 2014-09-23 07:21:11
我认为问题出来,如果我用程序推视图控制器,如果我使用“Triggered Segues”选项,插座已正确初始化,我可以与它们交互,但仍然tableview不显示我的自定义单元格事件,如果它被初始化(从未调用cellForRowAtIndexPath) – user3676870 2014-09-23 09:24:09