- 使用的Xcode 7
- 夫特2
我一直在研究以迅速的UIViewController继承目标C基类。我已经正确添加我的桥接报头文件,它是正确的我的项目生成设置部分:继承的类斯威夫特Objective-C的基类未投射到正确类型
在我的SCS-桥接-Header.h文件我有以下代码:
#import "UIViewControllerPlus.h"
然后我试图继承一个类雨燕的目标C类UIViewControllerPlus:
import UIKit
class TestBasicTabBarSegueViewController: UIViewControllerPlus {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这将构建并运行,但是当我顺着接下去日Ë夫特TestBasicTabBarSegueViewController
所述目标C基类或超类的属性是不可访问:
#ifndef UIViewControllerPlus_h
#define UIViewControllerPlus_h
#import <UIKit/UIKit.h>
#import "MMTabBarController.h"
@interface UIViewControllerPlus : UIViewController
@property MMTabBarController* mt;
@end
#endif /* UIViewControllerPlus_h */
它是属性mt
其类型为MMTabBarController的。
您可以在以下自定义Segue公司是destinationController.mt不可访问,并导致应用中看到试图设置时崩溃:
#import "MMNavigationSegue.h"
#import "MMTabBarController.h"
#import "UIViewControllerPlus.h"
@implementation MMNavigationSegue
- (void) perform
{
MMTabBarController *tabBarController = (MMTabBarController *) self.sourceViewController;
UIViewControllerPlus *destinationController = (UIViewControllerPlus *) self.destinationViewController;
destinationController.mt = tabBarController; //HERE APP CRASHES
for (UIView *view in tabBarController.placeholderView.subviews)
{
[view removeFromSuperview];
}
// Add view to placeholder view
tabBarController.currentViewController = destinationController;
[tabBarController.placeholderView addSubview: destinationController.view];
// Set autoresizing
[tabBarController.placeholderView setTranslatesAutoresizingMaskIntoConstraints:NO];
UIView *childview = destinationController.view;
[childview setTranslatesAutoresizingMaskIntoConstraints: NO];
// fill horizontal
[tabBarController.placeholderView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|[childview]|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(childview)]];
// fill vertical
[tabBarController.placeholderView addConstraints:[ NSLayoutConstraint constraintsWithVisualFormat: @"V:|-0-[childview]-0-|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(childview)]];
[tabBarController.placeholderView layoutIfNeeded];
// notify did move
[destinationController didMoveToParentViewController: tabBarController];
}
@end
注意,上面的代码工作,如果目的地的UIViewController Segued的就是从Objective C类UIViewControllerPlus
继承的Objective C代码UIViewController实现。
但下一行的应用程序崩溃:
错误:
2016-06-14 21:16:39.795 SCS[49747:17004544] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setMt:]: unrecognized selector sent to instance 0x7d967080'
的destinationController没有铸造键入UIViewControllerPlus
它改为铸造式UIViewController
,即使它定义为UIViewControllerPlus
。这只发生,如果它是一个斯威夫特文件/类继承的目标C UIViewControllerPlus
类,但剧组工作,如果它是Objective C的子类继承Objective C的基类UIViewControllerPlus
:
**在继承的目标C在Swift中可以使用基类?我已经阅读了关于这个网上的各种冲突声明?
有什么我可能会丢失在我的目标C项目引用/配置**
哇,我不能相信我做到了,谢谢! –