我想爲UITextView設置默認文本。以下是我的代碼 我已經使用文本屬性來初始化,但它不工作。我在這裏錯過了什麼?無法在UITextView中設置默認文本Objective-C
@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 [email protected]"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;
}
這段代碼爲我工作,對於「height」值,硬編碼值爲100。怎麼了? – MaxGabriel
在黑暗中沒有更多細節的鏡頭,但我猜測'init'方法沒有被調用。在那裏添加一個'NSLog'語句來檢查。如果例如你的視圖控制器是從故事板創建的,它將使用'initWithCoder:'代替。如果這是問題,那麼@ rokjarc的建議將代碼移動到'viewDidLoad'將會修復它,儘管它不會解決你的init代碼不被調用的潛在問題。 – MaxGabriel
MaxGabriel是正確的:通常'initWithFrame:','initWithCoder:'或'initWithNibName:bundle:'與'UIVIewController'一起使用 –