0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift:NSNibでCustom Viewを取得

0
Last updated at Posted at 2020-05-05
  • CustomView.xibを作る
  • CustomView.swiftを作ってCustomView: NSViewクラスを作る
  • xibのViewのCustom ClassにCustomViewを設定する
読み込み方
let nib = NSNib(nibNamed: "CustomView", bundle: Bundle.main)!
var topLevelArray: NSArray? = nil
guard
    nib.instantiate(withOwner: nil, topLevelObjects: &topLevelArray),
    let results = topLevelArray as? [Any],
    let item = results.last(where: { $0 is CustomView }),
    let customView = item as? CustomView else {
        // failed to load
        return
}
customViewゲット
CustomViewの例
class CustomView: NSView {
    
    @IBOutlet weak var label: NSTextField!
    @IBOutlet weak var imageView: NSImageView!
    
    // 初期化はここで行う
    override func awakeFromNib() {
        super.awakeFromNib()

        label.stringValue = "Hello World"
        imageView.image = NSImage(imageLiteralResourceName: "Sample")
    }
    
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?