2010-05-18 26 views
0

我有一個視圖上的UITextView成爲第一響應者。UITextView裏面的UIScrollView不是第一響應者

當我在界面生成器中將UITextView嵌入到UIScrollView中時,UITextView不再是第一響應者。我不確定發生了什麼變化?

我想讓UITextView成爲第一響應者。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [scrollView setContentSize:CGSizeMake(540,620)]; 
    composeTextView.delegate = self;  
    [composeTextView becomeFirstResponder]; 
} 

回答

1

這是我的.h文件:

#import <UIKit/UIKit.h> 

@interface Untitled1ViewController : UIViewController <UITextFieldDelegate> { 
    UIScrollView *scrollView; 
    UITextField *composeTextView; 
} 

@property (nonatomic,retain) IBOutlet UIScrollView *scrollView; 
@property (nonatomic,retain) IBOutlet UITextField *composeTextView; 

@end 

這是我的.m文件:

#import "Untitled1ViewController.h" 

@implementation Untitled1ViewController 
@synthesize scrollView; 
@synthesize composeTextView; 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [scrollView setContentSize:CGSizeMake(540,620)]; 
    composeTextView.delegate = self;  
    [composeTextView becomeFirstResponder]; 


} 

在IB中,我連接了以下內容: textField composeTextView scrollView滾動查看 和textField委託給文件的所有者。

重試並建議。

+0

對我來說還是沒有運氣。你的composeTextView是否在你的scrollView中? – 2010-05-19 10:21:57

+0

另外我使用的是UITextView而不是UITextField – 2010-05-19 10:25:00

+0

謝謝!這段代碼很好用。我意識到我的UITextView沒有連接到IB。小錯誤,造成巨大的頭痛。 – 2010-05-19 11:21:21

0

我做同樣的,添加了一些代碼,它的工作原理確定:

scrollView.contentSize = CGSizeMake(540,620); 
scrollView.showsHorizontalScrollIndicator = NO; 
scrollView.showsVerticalScrollIndicator = NO; 
scrollView.scrollsToTop = NO; 
[composeTextView becomeFirstResponder]; 
+0

我添加了你的代碼,但仍然沒有運氣。它可能是IB嗎?我有一個包含UIScrollView的UIView。裏面是一個UITextView和一些UIButtons。所有控件都以IB連接。 – 2010-05-18 19:34:24

+0

嘿,我已經做了所有的事情,從Scratch,我會附上它作爲一個新的答案,所以我可以標記代碼。 – 2010-05-18 19:52:57

0

文本視圖需要滾動事件,因此滾動事件不會在其響應者鏈中進一步傳遞。如果您對滾動文本視圖不感興趣,現在禁用textView中的滾動(使用nib或scrollEnabled屬性),scrollView將開始滾動事件。

+0

即使我禁用了textview的滾動,它也不會成爲第一響應者 – 2010-05-19 10:17:30

+0

它確實如此,我已經驗證過。我很確定這一點。如果你在textView中禁用滾動,那麼容器 - scrollView將開始接受事件。 – 2010-05-19 10:30:40

+0

我已經修改我的代碼composeTextView.scrollEnabled = NO;和[scrollView becomeFirstResponder];我也嘗試過以前的代碼,並且[composeTextView becomeFirstResponder]沒有運氣。 – 2010-05-19 10:37:17

相關問題