I have a small iPhone Project with a UITextView on my View, designed in the Interface Builder. There's a IBAction Method in my Viewcontroller and I connected the UITextView to that IBAction. I also added in my controller .h the <UITextViewDelegate>
.
我有一个小的iPhone项目,在我的视图上有一个UITextView,它是在接口构建器中设计的。在我的Viewcontroller中有一个IBAction方法我把UITextView连接到那个IBAction。我还在我的控制器.h中添加了
In my .m File I added the Method:
在我的.m文件中,我添加了方法:
- (void)textViewDidChange:(UITextView *)textView{
int count = [textView.text length];
charCount.text = (NSString *)count;
}
But when the App is running and I type something into the textView, the Method textViewDidChange will never be reached. Why is that? I also tried to add textView.delegate = self in the ViewDidLoad Method, but then the App crashes without any Message in the Debugger.
但是当应用程序运行时,我在textView中输入一些东西时,textViewDidChange方法将永远无法到达。这是为什么呢?我还尝试在ViewDidLoad方法中添加textView.delegate = self,但随后该应用程序在调试器中没有任何消息就崩溃了。
Has anyone a Tip what I'm doing wrong?
有人知道我做错了什么吗?
Thank you so much
非常感谢你
twickl
twickl
2 个解决方案
#1
9
You're on the right track - the reason the method isn't getting called is that you're not setting the text view's delegate before you change its text. I notice in your question you say you tried to set testView.delegate = self;
- did you mean textView
? Often typos like that will crash the program without a debugger message.
您走在正确的道路上——这个方法没有被调用的原因是您在更改文本视图的文本之前没有设置文本视图的委托。我注意到在你的问题中你说你试图设置testview。delegate = self;-你是说textView吗?通常这样的拼写错误会在没有调试器消息的情况下导致程序崩溃。
Also, the textFieldDidChange:
method isn't defined in the UITextFieldDelegate protocol. You may have meant textField:shouldChangeCharactersInRange:replacementString:
- this is the delegate method that actually gets called whenever a text field changes its contents. Just connecting your own method to an IBAction doesn't guarantee what I think you want.
同样,textFieldDidChange:方法在UITextFieldDelegate协议中没有定义。您可能指的是textField:shouldChangeCharactersInRange:replacementString: -这是一个委托方法,当一个文本字段改变其内容时,它实际上会被调用。仅仅将自己的方法连接到IBAction并不保证我认为您想要什么。
If neither of those are your problem, then you need to go back and double-check all your various connections, both in IB and in your class header file. Your header should look something like this:
如果这两个都不是您的问题,那么您需要返回并再次检查所有各种连接,包括IB和类头文件。你的标题应该是这样的:
// MyViewController.h
@interface MyViewController : UIViewController {
UITextField *textView;
}
@property(nonatomic,retain) IBOutlet UITextField *textView;
- (IBAction)myAction:(id)sender;
@end
And your implementation:
和你的实现:
// MyViewController.m
@implementation MyViewController
@synthesize textView;
- (IBAction)myAction:(id)sender {
// Do something
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
int count = [textView.text length];
charCount.text = (NSString *)count;
}
@end
The important document in this case is the UITextFieldDelegate protocol reference.
本例中的重要文档是UITextFieldDelegate协议引用。
#2
1
Oh, I realised what´s wrong!
哦,我意识到´s错了!
charCount musst be set in this way:
查穆斯特伯爵这样说:
charCount.text = [[NSNumber numberWithInt:count] stringValue];
Now it works!
现在它工作!
#1
9
You're on the right track - the reason the method isn't getting called is that you're not setting the text view's delegate before you change its text. I notice in your question you say you tried to set testView.delegate = self;
- did you mean textView
? Often typos like that will crash the program without a debugger message.
您走在正确的道路上——这个方法没有被调用的原因是您在更改文本视图的文本之前没有设置文本视图的委托。我注意到在你的问题中你说你试图设置testview。delegate = self;-你是说textView吗?通常这样的拼写错误会在没有调试器消息的情况下导致程序崩溃。
Also, the textFieldDidChange:
method isn't defined in the UITextFieldDelegate protocol. You may have meant textField:shouldChangeCharactersInRange:replacementString:
- this is the delegate method that actually gets called whenever a text field changes its contents. Just connecting your own method to an IBAction doesn't guarantee what I think you want.
同样,textFieldDidChange:方法在UITextFieldDelegate协议中没有定义。您可能指的是textField:shouldChangeCharactersInRange:replacementString: -这是一个委托方法,当一个文本字段改变其内容时,它实际上会被调用。仅仅将自己的方法连接到IBAction并不保证我认为您想要什么。
If neither of those are your problem, then you need to go back and double-check all your various connections, both in IB and in your class header file. Your header should look something like this:
如果这两个都不是您的问题,那么您需要返回并再次检查所有各种连接,包括IB和类头文件。你的标题应该是这样的:
// MyViewController.h
@interface MyViewController : UIViewController {
UITextField *textView;
}
@property(nonatomic,retain) IBOutlet UITextField *textView;
- (IBAction)myAction:(id)sender;
@end
And your implementation:
和你的实现:
// MyViewController.m
@implementation MyViewController
@synthesize textView;
- (IBAction)myAction:(id)sender {
// Do something
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
int count = [textView.text length];
charCount.text = (NSString *)count;
}
@end
The important document in this case is the UITextFieldDelegate protocol reference.
本例中的重要文档是UITextFieldDelegate协议引用。
#2
1
Oh, I realised what´s wrong!
哦,我意识到´s错了!
charCount musst be set in this way:
查穆斯特伯爵这样说:
charCount.text = [[NSNumber numberWithInt:count] stringValue];
Now it works!
现在它工作!