Xcode 6 GM故事板出口总是零

时间:2014-09-22 15:41:01

标签: ios uiviewcontroller swift iboutlet xcode6gm

我正在使用故事板在Xcode 6 GM中使用swift开发应用程序。在我的viewcontroller上,如果我拖放任何视图并将其与相关viewcontroller文件中的IBOutlet连接,那么在viewDidLoad中该出口始终为nil,如果我尝试与该出口交互,我总是会收到BAD_INSTRUCTION错误,如更改按钮的标题或其他任何因为引用的插座总是零,我无法理解为什么..我已经检查了故事板上的viewcontroller是由自定义类选项下的身份检查器中的viewcontroller文件所拥有的。这是Xcode和某人的问题否则会遇到同样的问题,或者我做错了什么?

编辑1:

这是第一个启动的viewcontroller

import UIKit

类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 outlet和自定义单元格的viewcontroller

import UIKit

class 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(ViewController在它的视图中)只包含4个按钮,显示所有呈现TableViewController的segues动作..没什么特别或奇怪的)并且tableview开始初始化然后我已经注释掉了tableview和委托设置的init方法(委托和dataSource已经在storyboard中设置)但结果再次是一个空白的屏幕显示什么都没有..我已经双重检查所有与故事板和各种标识符的连接,所以他们都正确设置..无法弄清楚是什么问题

2 个答案:

答案 0 :(得分:1)

我在Xcode 6.1测试版中遇到了同样的问题,最后从我的viewDidLoad方法删除自定义单元格注册行后,网点停止为零:

// Remove this: self.tableView.registerClass( MyTableViewCell.self, forCellReuseIdentifier: "MyIdentifier" )

从故事板实例化并以编程方式将子视图作为子视图推送或添加到另一个视图时工作。

答案 1 :(得分:0)

  

在viewDidLoad中,该出口始终为零,我总是得到   如果我尝试与该插座进行交互,则会出现BAD_INSTRUCTION错误   更改按钮的标题或其他任何内容,因为它   引用的出口总是零,我不明白为什么

请尝试引用viewWillAppearviewDidAppear函数中的控件。

viewDidLoad运行时,您的应用尚未创建视图和控件,因此修改其内容为时尚早。

此外,在您的Storyboard中,右键单击您的控件,并检查您是否在"引用Outlets"部分,该行上的填充 。 (这只是证实你确实有一个绑定到该控件的变量。)

enter image description here