Adrian
Adrian

Reputation: 16735

Instantiating a UIView subclass from a UIViewController

I found a tutorial that does exactly what I want to do in my existing Objective-C project, but it's written in Swift. I understand everything in Swift, but I'm having a nightmarish time "translating" a UIView's initializer to Objective-C. This should be really simple, but I can't figure it out.

My code doesn't crash, but it doesn't load anything. I put a breakpoint in my UIViewController to see what's happening when I try to load the UIView subclass and here's what I'm seeing:

(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

Here's the view I'm trying to "replicate" in Objective-C.

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
}

Here are my "translated" Objective-C .h/.m files:

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;
}

This is the code for the Swift & Objective-C UIViewControllers to add the 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()
  }

Here's my "translated" 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];
}

Thank you for reading.

Upvotes: 0

Views: 438

Answers (1)

tom.e.kid
tom.e.kid

Reputation: 121

How about this code..

- (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];
}

Upvotes: 1

Related Questions