Can't affect UITextView content with initWithCoder

ε祈祈猫儿з 提交于 2019-12-19 09:54:34

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2264357/cant-affect-uitextview-content-with-initwithcoder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!