2013-01-14 44 views
2

我在一個UI中有兩個UITextViews,其中UITextView是第一響應者。另一個UITextView是不可編輯的。當我雙擊不可編輯的UITextView時,鍵盤消失,我想避免這種情況。他們的鍵盤應該始終保持。UITextView使鍵盤永久?

+1

返回NO如果妳ddn't想什麼事,在第二TextView的再設置它的用戶交互禁用。 – Girish

+2

嗯,爲什麼不使用UILabel而不是(UITextView),如果它不可編輯? (只是想知道) –

+0

1.因爲多行文本在UILabel中垂直居中顯示。 2. UILabels不能滾動。 3.在UILabel上使用多行看起來不錯的動態文本很複雜。 –

回答

0

這不是默認行爲。實現您的要求嘗試使用這種方法delagate和shouldChangeTextInRange讓你的TextView編輯

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //nonEditingTextView.editable = NO; 
    //make the nonEditingTextView editable 
} 

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 
    if (textView == nonEditingTextView) { 
     return NO; 
    } 
    return YES; 
} 
1

如果您在文本視圖雙擊,就說明UIMenuController與剪切,複製等選項。

所以要達到您的要求,請將用戶交互屬性設置爲NO(False)。

希望這是你在找什麼。

-Mrunal

+0

如果你有setUserInteractionEnabled:NO或@Matthias共享的解決方案,它不會顯示剪貼複製粘貼等 –

1

讓您的viewController TextView的的代表和來自UITextViewDelegate方法textViewShouldEndEditing:

- (BOOL)textViewShouldEndEditing:(UITextView *)textView { 
    if (textView == self.editableTextView) { 
     return NO; 
    } 
    return YES; 
} 
+0

你的解決方案可以工作,但我仍然希望剪貼複製粘貼菜單控制器。謝謝。 –

0
//Firstly add the below code for keyboard notification into your viewdidload method. 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keybordWillHide:) name:UIKeyboardWillHideNotification object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keybordWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

//Let the two textview's names be, NonEditable and Editable 

//declare a flag globally 
bool Appearflag; 

//Then implement the two methods as follows 

-(void)keybordWillHide:(NSNotification *)notification 

{ 

    if ([NonEditable isFirstResponder] && Appearflag) 
    { 
     [Editable becomeFirstResponder]; 
    }else if ([Editable isFirstResponder]) 
    { 
     Appearflag = NO; 
    } 
} 

-(void)keybordWillShow:(NSNotification *)notification 

{ 

    if ([Editable isFirstResponder]) 
    { 
     Appearflag = YES; 
    } 
}