当用户按回车键时在 UITextView 中插入日期/时间
每当用户按下返回键或开始在 Xcode 中的 UITextView 上键入时,我都会尝试插入当前日期/时间,但不太确定从哪里开始。
我知道这个方法适用于单击 TextView 时,但似乎不起作用:
- (void)textViewDidBeginEditing:(UITextView *)textView {
self.txtDetail.text = [self.txtDetail.text stringByAppendingString:@"Hello!"];
}
谢谢
更新:
DetailViewController.h
@interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate> {
UITextView *txtDetail;
}
@property (nonatomic, retain) IBOutlet UITextView *txtDetail;
- (void)textViewDidBeginEditing:(UITextView *)textView;
DetailViewController.m
@synthesize txtDetail;
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(@"Hello");
self.txtDetail.text = [self.txtDetail.text stringByAppendingString:@"Hello!"];
}
Update02:
我已将其添加到我的 .m 文件中:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
BOOL shouldChangeText = YES;
if ([text isEqualToString:@"\n"]) {
// Find the next entry field
txtDetail.text = [txtDetail.text stringByAppendingString:@"\nhey\n"]; }
textView.editable = YES;
shouldChangeText = NO;
[textView becomeFirstResponder];
return shouldChangeText;
}
我得到了所需的效果(每当我按键盘上的 return 时就会添加嘿)但我现在无法输入任何内容...有什么想法吗?
Im trying to Insert the current date/time whenever the users hits the return key or starts typing on a UITextView in Xcode but not quite sure where to start.
I know this method is for when clicking into the TextView but doesn't seem to work:
- (void)textViewDidBeginEditing:(UITextView *)textView {
self.txtDetail.text = [self.txtDetail.text stringByAppendingString:@"Hello!"];
}
Thanks
Update:
DetailViewController.h
@interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate> {
UITextView *txtDetail;
}
@property (nonatomic, retain) IBOutlet UITextView *txtDetail;
- (void)textViewDidBeginEditing:(UITextView *)textView;
DetailViewController.m
@synthesize txtDetail;
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(@"Hello");
self.txtDetail.text = [self.txtDetail.text stringByAppendingString:@"Hello!"];
}
Update02:
Ive added this to my .m files:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
BOOL shouldChangeText = YES;
if ([text isEqualToString:@"\n"]) {
// Find the next entry field
txtDetail.text = [txtDetail.text stringByAppendingString:@"\nhey\n"]; }
textView.editable = YES;
shouldChangeText = NO;
[textView becomeFirstResponder];
return shouldChangeText;
}
I get the desired effect (Hey is added in whenever i press return on the keyboard) but i now can't type anything... any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须将控制器声明为实现 UITextViewDelegate 协议,然后将其连接为文本视图的委托。否则它根本不会收到委托消息。
然后你必须实现:
... 返回 YES/TRUE 否则不会添加任何文本。
Update02:
您需要实现所有具有返回值的 UITextViewDelegate 协议方法。如果不这样做,文本视图默认假定为“否”。
请参阅:文本和 Web 编程指南:管理 TextFields 和 TextView
您还需要了解 委托模式。
You have to declare you controller as implementing the UITextViewDelegate Protocol and then connect it as the text view's delegate. Otherwise it never receives the delegate messages at all.
Then you have to implement:
... to return YES/TRUE otherwise no text gets added.
Update02:
You need to implement all the UITextViewDelegate protocol methods that have a return. If you don't, the text view assumes a NO by default.
See: Text and Web Programming Guide: Managing TextFields and TextViews
You also need to understand the Delegate Pattern.
将您的方法替换为以下方法
Replace your method with the below method
委托调用机制是 iOS 编程中非常重要的一部分。 Apple 图书馆中有一个关于它的文档,并且许多好书中也对此进行了讨论。 Apress 的书《iPhone 编程入门》非常好,涵盖了委托方法。
The mechanics of delegate calls is a very important part of iOS programming. There is a doc on it in the Apple libraries, and it is also discussed in many good books. The Apress book "Beginning iPhone Programming" is very good and covers delegate methods.