Question

I'm trying to get a string displayed in my UITextView the moment the app is launched. I have a UIView and a UITextView in my interface, and one outlet, myText, which is connected to the UITextView. It doesn't seem to be working and I can't say why....

Here is the code in question:

MainView.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MainView : UIView {

    IBOutlet UITextView *myText;

}
@end

MainView.m

@implementation MainView

-(id) initWithCoder:(NSCoder *)coder {


    if ((self = [super initWithCoder:coder]) != nil)
    {
        NSLog(@"initWithCoder executed");
        myText.text = @"Hello, World!";
    }

    return self;
}

@end

The NSLog() message is printed, so I assume that that myText.text is indeed set to @"Hello World", but this isn't reflected in my UITextView. Where is the hang up here? If it helps, this is a simplified version of my actual app, and when I put that same myText.text = @"Hello, World!"; into a method that responds to a button press, the change is reflected in the text view.

Was it helpful?

Solution

Nib connections are not necessarily there in initWithCoder: — just because this object has been unfrozen doesn't mean everything else in the nib has been. You want awakeFromNib.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top