如何解决Swift-子类化的视图控制器不继承超类属性
我有一个基本视图控制器BaseViewController
,所有其他视图控制器都从该基础视图控制器中继承而来。看起来像这样:
class BaseViewController : UIViewController,NSFetchedResultsControllerDelegate {
// let appDelegate = UIApplication.shared.delegate as! AppDelegate
var appDelegate : AppDelegate {
return (UIApplication.shared.delegate as? AppDelegate)!
}
var currentOwner : Issuer = Issuer()
var currentColor : UIColor = UIColor()
var now = Calendar.current.component(.year,from: Date())
@objc func changeIssuer(issuerName : String) -> Issuer {
let context = appDelegate.persistentContainer.viewContext
let issuerSort = NSSortDescriptor(key: "name",ascending: false)
let issuerFetchRequest = NSFetchRequest<Issuer>(entityName: "Issuer")
issuerFetchRequest.sortDescriptors = [issuerSort]
let mainIssuerFRC = NSFetchedResultsController(fetchRequest: issuerFetchRequest,managedObjectContext: context,sectionNameKeyPath: nil,cacheName: nil)
do {
try mainIssuerFRC.performFetch()
} catch {
fatalError("Failed to initialize FetchedResultsController: \(error)")
}
if ((mainIssuerFRC.fetchedObjects!.count)) > 0 {
if let fObjects = mainIssuerFRC.fetchedObjects{
for issuerEntity in fObjects{
if issuerEntity.name == issuerName{
issuerEntity.isCurrent = true
currentOwner = issuerEntity
print("Current Color is \(currentColor)")
}
}
}
currentColor = UIColor.retrieveFromData(data: (currentOwner.colorD!))!
print("In changeIssuer,BaseVC,currentOwner == \(String(describing: currentOwner.name))")
print("In changeIssuer,currentColor == \(currentColor)")
appDelegate.saveContext()
}
return currentOwner
}
}
extension UIColor {
class func retrieveFromData(data:Data) -> UIColor? {
return try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? UIColor
}
func encodeToData() -> Data? {
return try? NSKeyedArchiver.archivedData(withRootObject: self,requiringSecureCoding: false)
}
}
我在各种子类中使用属性currentOwner
和currentColor
。函数changeIssuer
还应该更改currentColor
,因为它是实体Issuer的属性。
问题是某些子类似乎无法识别currentColor
,而我一直需要一个颜色主题来代替[Issuer name]: unrecognized selector sent to instance 0x280946c40
有人有什么想法吗?我可以提供更多必要的代码。
非常感谢所有帮助!
编辑1 说明
在某些VC上,我需要这样指定currentColor
:
currentColor = UIColor.retrieveFromData(data: (currentOwner.colorD!))!
即使currentOwner
是通过继承识别的,这也使我相信currentColor
也应该被识别。毕竟,它们是用相同的方法定义的。
在其他情况下,我需要同时指定currentOwner
和currentColor
。我首先通过调用VC在currentOwner
中指定了prepareForSegue
,
popoverVC.currentOwner = currentOwner
然后,我必须在目标VC的currentColor
中指定这样的viewDidLoad
:
currentColor = UIColor.retrieveFromData(data: (currentOwner.colorD!))!
但是,我不明白为什么我需要这样做。由于项目中的所有视图控制器都是从相同的BaseViewController
继承而来的,所以它们不应该都继承相同的currentOwner
吗?
我在changeIssuer
的{{1}}中做错什么了吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。