我找到了一个关于如何做某事的教程
Swift
.我在一个
Objective-C
项目,我已经尝试将类导入
目标-C
但是
that's been a bug-riddled, time monopolizing disaster
,所以我将Swift转换为Objective-C。
我得到了
UIView.h/m
到没有警告/错误的程度,但将视图添加到
UIViewController
是另一回事。我已经在下面发布了我试图“翻译”的初始化代码。我欢迎输入关于我所犯的错误。
用于初始化UIView的Swift类代码:
var parentFrame :CGRect = CGRectZero
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = Colors.clear
}
用于初始化UIView的“翻译”Objective-C类中的代码
myUIView.h
// At MartinR's suggestion, I removed the pointer here
@property (nonatomic) CGRect parentFrame;
myUIView.m
// No compiler errors -- the errors occur on the UIViewController
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:(self.parentFrame)];
self.parentFrame = CGRectZero;
if (self) {
[super setFrame:self.parentFrame];
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
当我尝试将UIView添加到UIViewController时,出现了问题。我无法理解为什么我会收到以下警告:
Swift UIViewController代码,用于添加UIView
func addHolderView() {
let boxSize: CGFloat = 75.0
holderView.frame = CGRect(x: view.bounds.width / 2 - boxSize / 2,
y: view.bounds.height / 2 - boxSize / 2,
width: boxSize,
height: boxSize)
holderView.parentFrame = view.frame
holderView.delegate = self
view.addSubview(holderView)
holderView.addOval()
}
我的“翻译”Objective-C ViewController充斥着实现错误
myUIViewController.h
#import "HolderView.h"
// blah blah
@property (nonatomic, strong) HolderView *holderView;
myUIViewController.m
- (void)addHolderView {
CGFloat boxSize = 75.0;
// WARNING: The following line says "expression result unused"
[self.holderView initWithFrame:CGRectMake(_view.bounds.size.width / 2 - boxSize / 2,
_view.bounds.size.height / 2 - boxSize / 2,
boxSize,
boxSize)];
// Warning here went away by removing the pointer on CGRect parentFrame
self.holderView.parentFrame = view.frame;
self.holderView.delegate = self; // I have the delegate stuff squared away
[self.view addSubview:self.holderView];
}
感谢您的阅读。
更新以反映MartinR在评论中的意见。
我更新了上面的代码以删除
CGRect parentFrame
.