0

我想为 UITextView 设置默认文本。下面是我使用 text 属性进行初始化的代码,但它不起作用。我在这里想念什么?

@implementation DetailViewController
{
    NSDictionary* inputFields;

}

- (id) init
{
    self = [super init];
    self.currentClaim = NULL;

    inputFields = @{
                    // These fields must match AppDelegate's newFormData method
                    // TODO: Refactor so we don't need this dependency.
                    @"actionPlan" : [self createTextAreaForActionPlan:@"Action Plan" at:CGPointMake(20.0f, 1200.0f)],
                   // createTextAreaForActionPlan
                    };

    return self;
}



- (UITextView*)createTextAreaForActionPlan:(NSString*)title at:(CGPoint)origin
{
    float height = [self createTextLabel:title at:origin];
    UITextView* textArea = [[UITextView alloc] initWithFrame:CGRectMake(origin.x, origin.y + height, 660.0f, 100.0f)];
    [[textArea layer] setBorderWidth:1.0f];
    [[textArea layer] setBorderColor:[[UIColor blackColor] CGColor]];
    textArea.text =@"Default Text"; // this is the default text. how to show in UI TextView.
    [textArea setFont:[UIFont systemFontOfSize:16.0f]];
    [textArea setDelegate:self];
    [[self view] addSubview:textArea];

    return textArea;
}
4

2 回答 2

3

您只能UIVIewController在加载后将视图添加到 的视图。

所以你应该把你的初始化代码移到

- (void)viewDidLoad

或进入

- (void)loadView

如果你自己实现它。

编辑:

这段代码应该可以工作。虽然我没有测试它。

- (id)init
{
    self = [super init];

    if (self) 
    {
        self.currentClaim = NULL;    
    }

    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];

    inputFields = @{...};
}
于 2013-09-21T10:42:43.617 回答
-1

工作代码

视图控制器.m

- (UITextView*)createTextAreaForActionPlan:(NSString*)title at:(CGPoint)origin
{
    //float height = [self createTextLabel:title at:origin];
    UITextView* textArea = [[UITextView alloc] initWithFrame:CGRectMake(origin.x, origin.y + 50, 100.0f, 100.0f)];
    [[textArea layer] setBorderWidth:1.0f];
    [[textArea layer] setBorderColor:[[UIColor blackColor] CGColor]];
    textArea.text =@"Default Text"; // this is the default text. how to show in UI TextView.
    [textArea setFont:[UIFont systemFontOfSize:16.0f]];
    [textArea setDelegate:self];
    [[self view] addSubview:textArea];

    return textArea;
}
- (id) init
{
    self = [super init];
    //self.currentClaim = NULL;

    inputFields = @{
                    // These fields must match AppDelegate's newFormData method
                    // TODO: Refactor so we don't need this dependency.
                    @"actionPlan" : [self createTextAreaForActionPlan:@"Action Plan" at:CGPointMake(20.0f, 50.0f)],
                    // createTextAreaForActionPlan
                    };

    return self;
}
- (void)viewDidLoad
{
[self init];
}

你会得到

在此处输入图像描述

于 2013-09-21T11:47:47.913 回答