2016-06-15 13 views
1
  • 使用的Xcode 7
  • 夫特2

我一直在研究以迅速的UIViewController继承目标C基类。我已经正确添加我的桥接报头文件,它是正确的我的项目生成设置部分:继承的类斯威夫特Objective-C的基类未投射到正确类型

enter image description here

在我的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实现。

但下一行的应用程序崩溃:

enter image description here

错误:

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

enter image description here

**在继承的目标C在Swift中可以使用基类?我已经阅读了关于这个网上的各种冲突声明?

有什么我可能会丢失在我的目标C项目引用/配置**

回答

1

-[UIViewController setMt:]:意味着被实例化视图控制器是一个普通视图控制器 - 你的子类“加”型的一个也没有。

您可能忘了将正确的类分配给故事板中的视图控制器。

+0

哇,我不能相信我做到了,谢谢! –

相关问题