我正在将我的Objective-C代码转换为Swift,并想知道这段代码在Swift中的等效写法。
[[[NSBundle mainBundle] loadNibNamed:@"Games-New" owner:nil
options:nil] firstObject];
谢谢。
[[[NSBundle mainBundle] loadNibNamed:@"Games-New" owner:nil
options:nil] firstObject];
谢谢。
可能API已更改。 loadNibNamed
的第三个参数不再是options
,而是topLevelObjects
。以下代码适用于我:
var objects: NSArray?
NSBundle.mainBundle().loadNibNamed("MyView", owner: self, topLevelObjects: &objects)
value of optional type 'NSArray?' not unwrapped; did you mean to use '!' or '?'?
。与其将“objects”声明为可选值,似乎更应该使用空的NSArray()
进行初始化。 - Chris FrederickBundle.main.loadNibNamed("Games-New", owner: nil, options: nil)[0]
var awesomeView = NSBundle.mainBundle().loadNibNamed("Games-New", owner: nil, options: nil)[0] as? AwesomeView
- aryaxt这里是一个UIView扩展
public extension UIView {
public class func instantiateFromNib<T: UIView>(viewType: T.Type) -> T {
return NSBundle.mainBundle().loadNibNamed(String(describing: viewType), owner: nil, options: nil)?.first as! T
}
public class func instantiateFromNib() -> Self {
return instantiateFromNib(self)
}
}
使用方法
let awesomeView = AwesomeView.instantiateFromNib()
或者
let awesomeView = UIView.instantiateFromNib(AwesomeView.self)
NSBundle.mainBundle().loadNibNamed(String(viewType), owner: nil, options: nil).first as? T
, func instantiateFromNib<T: UIView>(viewType: T.Type) -> T?
, public class func instantiateFromNib() -> Self?
。 - Hugues BRas
:) - aryaxt.first
呢?这样做有什么必要吗?难道nib文件中不仅仅包含视图吗? - mfaaniSwift 3
所有者
分配为 nib 的“文件所有者”对象的对象。
选项
包含在打开 nib 文件时使用的选项的字典。
第一个视图
如果您没有定义第一个视图,则会获取所有视图。因此,您需要在其中获取一个视图并设置first
。
Bundle.main.loadNibNamed("yourUIView", owner: self, options: nil)?.first as! yourUIView
let myView = NSBundle.mainBundle().loadNibNamed("MyView", owner: nil, options: nil)[0] as UIView
view.addSubview(myView)
func loadFromNib(_ nibName: String) -> NSView? {
var topLevelObjects: NSArray?
if Bundle.main.loadNibNamed(NSNib.Name(rawValue: nibName), owner: self, topLevelObjects: &topLevelObjects) {
return topLevelObjects?.first(where: { $0 is NSView } ) as? NSView
} else {
return nil
}
}
对于Swift 3:
Bundle.main.loadNibNamed("Games-New", owner: nil, options: nil)?[0]
FYI:NS在Swift 3中已经(并非完全)被移除
Bundle.main.loadNibNamed("MyDenizbankLoading", owner: nil, options: nil)![0]
这对我有用
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
var objects: NSArray?
Bundle.main.loadNibNamed("CellView", owner: self, topLevelObjects: &objects)
var newView = NSView()
for object in objects ?? [] {
if (object is NSView) {
newView = object as! NSView
break;
}
}
var cellView: CellView?
for view in newView.subviews {
print("class: \(view.className) ")
if (view is CellView) {
cellView = view as? CellView
break;
}
}
cellView?.imageView.image = image
cellView?.textFieldView.stringValue = text
return cellView
}
这是在Swift 4中如何在macOS上执行的方式
注册
collectionView.register(NSNib(nibNamed: NSNib.Name(rawValue: "Cell"), bundle: nil)!,
forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"))
出队
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), for: indexPath)
guard let cell = item as? Cell else { return item }
let value = values[(indexPath as NSIndexPath).item]
cell.imageView?.setImage(url: value, placeholder: Image(named: NSImage.Name(rawValue: "placeholder")))
return cell
}