Insert date / time in UITextView when user calls return

Im trying to insert the current date / time whenever users click on the return key or start entering a UITextView in Xcode, but are not quite sure where to start.

I know this method is used when clicked in a TextView, but it doesn't seem to work:

- (void)textViewDidBeginEditing:(UITextView *)textView {
    self.txtDetail.text = [self.txtDetail.text stringByAppendingString:@"Hello!"];
}

thank

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:

I 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 whenever I press return on the keyboard), but now I can not type anything ... any ideas?

+3
source share
3 answers

UITextViewDelegate Protocol, . .

:

textView:shouldChangeTextInRange:replacementText:

... YES/TRUE .

Update02:

UITextViewDelegate, . , NO.

: -:

.

0

.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{   

   BOOL shouldChangeText = YES;   

   if ([text isEqualToString:@"\n"] && textView == detailText) 
   {   
     // Find the next entry field   

       txtDetail.text = [txtDetail.text stringByAppendingString:@"\nhey\n"];          
       textView.editable = YES; 

       shouldChangeText = NO;  
       [textView becomeFirstResponder]; 
   } 
   return shouldChangeText;   
} 
0

- iOS. Apple , . Apress " iPhone" .

0
source

Source: https://habr.com/ru/post/1759752/


All Articles