UITraitCollection - swift 到 objective-C
UITraitCollection - swift to objective-C
我正在尝试将 swift 转换为 objective-C。下面是我的代码:
Swift
import UIKit
protocol TraitCollectionOverridable {
func preferredTraitCollection() -> UITraitCollection?
}
class CustomNavigationController: UINavigationController {
override func overrideTraitCollectionForChildViewController(childViewController: UIViewController) -> UITraitCollection? {
guard let conformingViewController = childViewController as? TraitCollectionOverridable else {
return super.overrideTraitCollectionForChildViewController(childViewController)
}
return conformingViewController.preferredTraitCollection()
}
}
Objective-C
头文件
@protocol TraitCollectionOverridable <NSObject>
- (UITraitCollection *) preferredTraitCollection;
@end
@interface CustomNavigationController : UINavigationController
@property (nonatomic, weak) id <TraitCollectionOverridable> traitsDelegate;
@end
.m 文件
@implementation CustomNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
[self.traitsDelegate preferredTraitCollection];
// Do any additional setup after loading the view.
}
- (UITraitCollection *)overrideTraitCollectionForChildViewController:(UIViewController *)childViewController
{
if (CGRectGetWidth(self.view.bounds) < CGRectGetHeight(self.view.bounds)) {
return [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
} else {
return [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
}
}
@end
我的猜测是 overrideTraitCollectionForChildViewController 没有正确转换。任何帮助都会很棒。
My guess is overrideTraitCollectionForChildViewController is not converted properly.
Obj-C 和 Swift 版本的 overrideTraitCollectionForChildViewController
并不相同。这是更正后的代码:
Obj-C
- (UITraitCollection *)overrideTraitCollectionForChildViewController:(UIViewController *)childViewController
{
if( [childViewController conformsToProtocol:@protocol(TraitCollectionOverridable)] ) {
return [(NSObject<TraitCollectionOverridable>*)childViewController preferredTraitCollection];
} else {
return [super overrideTraitCollectionForChildViewController:childViewController];
}
}
Swift
override func overrideTraitCollectionForChildViewController(childViewController: UIViewController) -> UITraitCollection? {
guard let conformingViewController = childViewController as? TraitCollectionOverridable else {
return super.overrideTraitCollectionForChildViewController(childViewController)
}
return conformingViewController.preferredTraitCollection()
}
我正在尝试将 swift 转换为 objective-C。下面是我的代码:
Swift
import UIKit
protocol TraitCollectionOverridable {
func preferredTraitCollection() -> UITraitCollection?
}
class CustomNavigationController: UINavigationController {
override func overrideTraitCollectionForChildViewController(childViewController: UIViewController) -> UITraitCollection? {
guard let conformingViewController = childViewController as? TraitCollectionOverridable else {
return super.overrideTraitCollectionForChildViewController(childViewController)
}
return conformingViewController.preferredTraitCollection()
}
}
Objective-C 头文件
@protocol TraitCollectionOverridable <NSObject>
- (UITraitCollection *) preferredTraitCollection;
@end
@interface CustomNavigationController : UINavigationController
@property (nonatomic, weak) id <TraitCollectionOverridable> traitsDelegate;
@end
.m 文件
@implementation CustomNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
[self.traitsDelegate preferredTraitCollection];
// Do any additional setup after loading the view.
}
- (UITraitCollection *)overrideTraitCollectionForChildViewController:(UIViewController *)childViewController
{
if (CGRectGetWidth(self.view.bounds) < CGRectGetHeight(self.view.bounds)) {
return [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
} else {
return [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
}
}
@end
我的猜测是 overrideTraitCollectionForChildViewController 没有正确转换。任何帮助都会很棒。
My guess is overrideTraitCollectionForChildViewController is not converted properly.
Obj-C 和 Swift 版本的 overrideTraitCollectionForChildViewController
并不相同。这是更正后的代码:
Obj-C
- (UITraitCollection *)overrideTraitCollectionForChildViewController:(UIViewController *)childViewController
{
if( [childViewController conformsToProtocol:@protocol(TraitCollectionOverridable)] ) {
return [(NSObject<TraitCollectionOverridable>*)childViewController preferredTraitCollection];
} else {
return [super overrideTraitCollectionForChildViewController:childViewController];
}
}
Swift
override func overrideTraitCollectionForChildViewController(childViewController: UIViewController) -> UITraitCollection? {
guard let conformingViewController = childViewController as? TraitCollectionOverridable else {
return super.overrideTraitCollectionForChildViewController(childViewController)
}
return conformingViewController.preferredTraitCollection()
}