2013-10-29 51 views
0

我有一個scrollview。我在同一行放置了一個textfield和一個按鈕。我希望當用戶在textbox中輸入URL並點擊button時,URL應該被添加動態地創建textview而不是textboxbutton。 (我正在使用textview,因爲它會自動檢測URL並作爲鏈接顯示)。 我已經嘗試下面的代碼,但它不工作:動態添加textview點擊按鈕中的ios目標c

- (IBAction)addLinkClicked:(id)sender { 

    NSLog(@"Add Link button clicked"); 
    UITextView *linkTextView = [[UITextView alloc] init]; 
    linkTextView.text = @"http://google.com"; 
    linkTextView.layer.cornerRadius = 5.0; 
    [self.scrollView addSubview:linkTextView]; 
} 

2.

- (IBAction)addLinkClicked:(id)sender { 

    NSLog(@"Add Link button clicked"); 
    UITextView *linkTextView = [[UITextView alloc] init]; 
    linkTextView.text = @"http://google.com"; 
    linkTextView.layer.cornerRadius = 5.0; 
    [self.scrollView insertSubview:linkTextView atIndex:[self.scrollView.subviews count]]; 
} 

請幫我解決我的問題。

+0

設置UITextView框架 – Mutawe

回答

1

我的朋友,你需要設置爲UITextFiled框架...

你有兩種可能的方式:

UITextView *linkTextView = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)] 

或者

UITextView *linkTextView = [[UITextView alloc] init]; 
linkTextView.frame = CGRectMake(x, y, width, height); 

乾杯!

+0

這是評論不是答案。 – Jitendra

0

設置爲TextView的框架:以上

linkTextView = [[UITextView alloc] initWithFrame:CGRectMake(240, 35, 70, 20)]; //put the values you want 
0

兩個問題的答案是相同的,是正確的。只需一個備用答案,當您創建textfieldbutton並將hidden屬性設置爲true時,也可以在同一幀中添加textview。輕觸button後,隱藏textfieldbutton,並通過將hidden設置爲false來顯示文本視圖。

相關問題