2013-09-21 21 views
0

我想爲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; 
} 
+0

這段代碼爲我工作,對於「height」值,硬編碼值爲100。怎麼了? – MaxGabriel

+1

在黑暗中沒有更多細節的鏡頭,但我猜測'init'方法沒有被調用。在那裏添加一個'NSLog'語句來檢查。如果例如你的視圖控制器是從故事板創建的,它將使用'initWithCoder:'代替。如果這是問題,那麼@ rokjarc的建議將代碼移動到'viewDidLoad'將會修復它,儘管它不會解決你的init代碼不被調用的潛在問題。 – MaxGabriel

+0

MaxGabriel是正確的:通常'initWithFrame:','initWithCoder:'或'initWithNibName:bundle:'與'UIVIewController'一起使用 –

回答

3

只有在加載後才能在UIVIewController的視圖中添加視圖。

所以,你應該,如果你自己實現它移到您的初始化代碼爲

- (void)viewDidLoad 

或成

- (void)loadView 

編輯:

此代碼應工作。我沒有測試它。

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

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

    return self; 
} 

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    inputFields = @{...}; 
} 
+1

很確定,每當你第一次訪問'UIViewController'的'view'屬性'viewDidLoad'被調用,所以這不應該是一個問題。 – MaxGabriel

+0

你有一個有效的點。但據我所知,在初始化之前,您不應該「觸摸」視圖屬性。另一方面,'[super init];'在這裏被調用。但是:如果'view'沒有被加載,那麼'[self loadView]'被調用,並且只有在執行'viewDidLoad'後纔會起作用。 –

-1

工作代碼

Viewcontroller.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 [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; 
} 
- (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]; 
} 

你會得到

enter image description here

+0

看起來有點_strange_從' - (void)viewDidLoad'中調用'[self init]' - 不要這樣做:它從所有已構造的對象調用類構造函數。 –

+1

有點奇怪,我會說。找到比此更好的理由,在對象實例化上下文之外調用'init'。 –

+0

@CarlVeazey;我只是有禮貌 - 當然這不是正確的方法 –