重新加载NSWindow Xcode Swift2

时间:2016-03-03 18:13:19

标签: swift nsview nsoutlineview nswindowcontroller

我正在使用NSOutlineView,它使用NSView子类在大纲中生成自定义单元格。我已经开始工作了,但是在大纲从模型类中汲取数据并正确显示之后,大纲从内存中释放(?)并转到零,我还没弄明白回来的方法。

这是MainViewController类

class MainWindowController: NSWindowController, ShareInfoDelegate,    NSOutlineViewDelegate, NSOutlineViewDataSource {

override var windowNibName: String {

    return "MainWindowController"

}

@IBOutlet var daOutline: NSOutlineView!
//  The NSoutline I'm trying to get back to    

与测试数据相关的一些内容(略) 引导我们进入NSOutlineViewDataSource的东西

//MARK: - NSOutlineViewDataSource

func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {
    if let item: AnyObject = item {
        switch item {
        case let work as Work:
            return work.movements[index]

        case let movement as Movement:
            return movement.tracks[index]

        default:

            let track = item as! Track
            return track.credits[index]
        }
    } else {
        if allWorks.count > 0 {

            return allWorks[index]
        }
    }
    let q = "patience"
    return q
}

func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {
    switch item {
    case let work as Work:
        return (work.movements.count > 0) ? true : false

    case let movement as Movement:
        return (movement.tracks.count > 0) ? true : false

    case let track as Track:
        return (track.credits.count > 0) ? true: false

    default:
        return false
    }
}

func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int {
    if let item: AnyObject = item {
        switch item {
        case let work as Work:
            return work.movements.count

        case let movement as Movement:
            return movement.tracks.count

        case let track as Track:
            return track.credits.count

        default:
            return 0
        }
    } else {
        return allWorks.count
    }
}



func outlineView(daOutline: NSOutlineView, viewForTableColumn theColumn: NSTableColumn?, item: AnyObject) -> NSView? {

          switch item {

        case let worked as Work:

            let cell = daOutline.makeViewWithIdentifier("newTry", owner:self) as! newTry

            cell.fourthLabel.stringValue = worked.composer

            cell.fourthCell.stringValue = worked.title

            return cell


        case let moved as Movement:

            let cell2 = daOutline.makeViewWithIdentifier("SecondTry", owner:self) as! SecondTry

            cell2.roman.stringValue = moved.name!

            cell2.details.stringValue = moved.sections!

            cell2.track.stringValue = "0"

            return cell2


        default:

            print("probably not")
    }

    print("not again")
    return nil
}

func outlineView(daOutline: NSOutlineView, heightOfRowByItem item: AnyObject) -> CGFloat {

    switch item {

    case let worked as Work:
        return 40

    default:
        return 24

    }        
}

WindowDidLoad中的东西

    override func windowDidLoad() {
    super.windowDidLoad()

    let nib = NSNib(nibNamed: "newTry", bundle: NSBundle.mainBundle())
    daOutline.registerNib(nib!, forIdentifier: "newTry")

    let nib2 = NSNib(nibNamed: "SecondTry", bundle: NSBundle.mainBundle())
    daOutline.registerNib(nib2!, forIdentifier: "SecondTry")

    //give Sender it's Receiver
    mailItOut.delegate = receiver

    allWorks.append(work1)
    allWorks.append(work2)

    work1.movements.append(move1)
    work1.movements.append(move2)
    work1.movements.append(move3)
    work1.movements.append(move4)

    work2.movements.append(move5)
    work2.movements.append(move6)
    work2.movements.append(move7)

    daOutline.reloadData()

    daOutline?.expandItem(work1, expandChildren: false)

   daOutline?.expandItem(work2, expandChildren: false)
}

}

最后newTry NSView类是什么样的

class newTry: NSView {

var delegate: ShareInfoDelegate?



@IBOutlet weak var fourthCell: NSTextField!

@IBOutlet weak var fourthLabel: NSTextField!



@IBAction func cellAdd(sender: NSTextField) {

    var catchIt: String = String()



    catchIt = sender.stringValue

    if catchIt != "" {
     tryAgain = catchIt

    whichField = "title"

    //Trigger the sender to send message to it's Receiver
    mailItOut.sendMessage()

    }
}

cellAdd Action用于尝试将用户输入从文本单元格返回到模型中。要做到这一点,我(AFAIK)需要访问NSOutline(daOutline)并获取我所在的行并将发送者的数据放入Model类的相应部分。这是我设法在标准(1个单元格/ 1个数据值)大纲中工作的东西。但是在这个原型中,据我所知,MainWindowController已经释放了它的所有内容,daOutline是nil(坏)。

如何让XCode带来/重新加载已完成的轮廓(或从不释放它)并将daOutline变为非零状态?

1 个答案:

答案 0 :(得分:0)

对于那些来之后,似乎有两个问题导致NSOutline出口变为零。第一个是在实现委托协议“shareInfoDelegate”时,我创建了一个MainWindowController的新实例,而不是包含数据的实例。这个新实例没有连接IBOutlets(或者对它有很多有用的东西)。 一旦我取消了委托并转而使用NSNotification更新有关NSView textFields的信息,我的NSOutline就“回来了”。

第二个更小的问题是我放置的NSView nib文件和NSBox模仿组行的行为(例如灰色背景)。作为副作用,NSBox禁止轮廓的正常行选择行为。这使得很难确定选择了哪一行。当我删除NSBox时,行选择变得更容易确定。

特别是这个问题以及Chuck的答案有助于嗅出这个问题。

Why is my NSOutlineView datasource nil?

非常感谢(!)