gpt4 book ai didi

ios - 从 UIViewController 实例化 UIView 子类

转载 作者:行者123 更新时间:2023-11-29 01:48:44 26 4
gpt4 key购买 nike

我找到了一个教程,它完全符合我想在我现有的 Objective-C 项目中执行的操作,但它是用 Swift 编写的。我了解 Swift 中的所有内容,但我正在经历一段噩梦般的时间,将 UIView 的 初始化程序“翻译”为 Objective-C。这应该很简单,但我想不通。

我的代码没有崩溃,但它没有加载任何东西。我在我的 UIViewController 中放置了一个断点,以查看当我尝试加载 UIView 子类时发生了什么,这就是我所看到的:

(lldb) po self.boxSize
75

(lldb) po self.holderView.frame
(origin = (x = 0, y = 0), size = (width = 0, height = 0))
(origin = (x = 0, y = 0), size = (width = 0, height = 0))

(lldb) po self.holderView
nil

这是我试图在 Objective-C 中“复制”的 View 。

HolderView.swift:

class HolderView: UIView {

var parentFrame :CGRect = CGRectZero

weak var delegate:HolderViewDelegate?

override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = Colors.clear
}

required init(coder: NSCoder) {
super.init(coder: coder)
}
// A bunch of class methods
}

这是我“翻译的”Objective-C .h/.m 文件:

HolderView.h:

#import <UIKit/UIKit.h>

@protocol HolderViewDelegate <NSObject>
// delegate methods
@ end

@interface HolderView : UIView

@property (nonatomic, weak) id <HolderViewDelegate> delegate;

@property (nonatomic) CGRect frame;
@property (nonatomic) CGRect parentFrame;

HolderView.m:

- (instancetype)initWithFrame:(CGRect)frame
{
self.parentFrame = CGRectZero;
self = [super initWithFrame:frame];
if (self) {
[super setFrame:self.parentFrame];
self.backgroundColor = [UIColor orangeColor]; // Added to see if it's working
NSLog(@"anyone home?");
}
return self;
}

这是 Swift 和 Objective-C UIViewControllers 添加 UIView 的代码:

ViewController.swift

  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 View Controller :

- (void)addHolderView {
self.boxSize = 75.0; // I added as a global variable, as local returned NULL when I tried locally
self.holderView.frame = CGRectMake(self.view.bounds.size.height / 2 - self.boxSize / 2,
self.view.bounds.size.width / 2 - self.boxSize / 2,
self.boxSize,
self.boxSize);

self.holderView.parentFrame = self.view.frame;
self.holderView = [self.holderView initWithFrame:self.holderView.frame];

self.holderView.delegate = self;

NSLog(@"addHolderView called");
[self.view addSubview:self.holderView];
}

感谢阅读。

最佳答案

这段代码怎么样..

- (void)addHolderView {
self.boxSize = 75.0; // I added as a global variable, as local returned NULL when I tried locally
CGRect frame = CGRectMake(self.view.bounds.size.height / 2 - self.boxSize / 2,
self.view.bounds.size.width / 2 - self.boxSize / 2,
self.boxSize,
self.boxSize);

self.holderView = [[HolderView alloc] initWithFrame: frame];
self.holderView.parentFrame = self.view.frame;

self.holderView.delegate = self;

NSLog(@"addHolderView called");
[self.view addSubview:self.holderView];
}

关于ios - 从 UIViewController 实例化 UIView 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31644116/

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