Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewDidLoad in UITextView subclass?

Cocoa/Objective-C noob here.

I'm working on a simple app to learn some more about iOS development, and am struggling to see how my subclass of UITextView has it's viewDidLoad method called.

I am subclassing UITextView to CMTextView.

Using storyboard, I have a CMTextView in the window.

In CMTextView.m, I have the following:

#import "CMTextView.h"

@interface UITextView ()

- (id)styleString;

@end

@implementation CMTextView

- (id)styleString {
    return [[super styleString] stringByAppendingString:@"; line-height: 1.1em"];
}

- (void)viewDidLoad {
    NSLog(@"LOADED!"); // not doing anything...
}

I'm not doing anything fancy to add this as a subview of my window, but I thought the storyboard did that for me?

Does anyone have any advice?

like image 564
Connor Avatar asked Jun 15 '26 10:06

Connor


2 Answers

The above answers are correct if you manually initialize the UITextView. If loading them from a nib, you need to override the -awakeFromNib method.

-(void)awakeFromNib
{
    [super awakeFromNib]; // Don't forget to call super

    //Do more intitialization here
}


If you want to handle the resizing also override the -layoutSubviews method

like image 62
graver Avatar answered Jun 17 '26 23:06

graver


viewDidLoad is a method in UIViewController not UIView/UITextView

if you want to do any initialize then put it in initWithFrame:

like image 40
Bryan Chen Avatar answered Jun 18 '26 00:06

Bryan Chen