gpt4 book ai didi

ios - UITraitCollection - 快速到 objective-C

转载 作者:行者123 更新时间:2023-11-28 07:03:52 25 4
gpt4 key购买 nike

我正在尝试将 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-CSwift 版本的 overrideTraitCollectionForChildViewController 并不等效。这是更正后的代码:

objective-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()
}

关于ios - UITraitCollection - 快速到 objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31331257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com