PDA

View Full Version : Xcode iPhone/iPad - Insert date/time on hitting return




GregD
Aug 15, 2010, 06:47 AM
Im trying to insert the current date/time in a UITextView once the user hits the return key but without success.

Can anyone help point me in the right direction ? :)



robbieduncan
Aug 15, 2010, 07:05 AM
With which part?

GregD
Aug 15, 2010, 07:13 AM
With which part?

How do you mean?

I have a textview on interface builder hooked up to an IBOutlet and a property synthesized in DetailViewController.m (txtDetail)...

I was trying to see to start off with (before even thinking about adding a date) I could just add some text in when the user clicks onto UITextView however that doesn't work either:

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

Unless i've got the wrong end of the stick completely on how to run the method?

robbieduncan
Aug 15, 2010, 07:16 AM
How do you mean?

I have a textview on interface builder hooked up to an IBOutlet and a property synthesized in DetailViewController.m (txtDetail)...

I was trying to see to start off with (before even thinking about adding a date) I could just add some text in when the user clicks onto UITextView however that doesn't work either:

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

Unless i've got the wrong end of the stick completely on how to run the method?

OK, so that's a sensible start. What have you done to debug it "not working"? Have you checked that the method is being called with an NSLog statement? Have you even set the instance that contains that method as the text view delegate?

GregD
Aug 15, 2010, 07:45 AM
OK, so that's a sensible start. What have you done to debug it "not working"? Have you checked that the method is being called with an NSLog statement? Have you even set the instance that contains that method as the text view delegate?

Well tried an NSLog(@"Hello"); within the method but nothing... Here's the code in case ive overlooked something obvious:


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!"];
}


On interface builder ive connected the textview to to file owner txtDetail. Have I missed anything?

Appreciate the help :)

robbieduncan
Aug 15, 2010, 07:58 AM
On interface builder ive connected the textview to to file owner txtDetail. Have I missed anything?

Appreciate the help :)

You need to give as much detail as possible: what connection. Have you connected the delegate property of the text view? If not then you cannot expect to receive delegate messages.

GregD
Aug 15, 2010, 08:19 AM
You need to give as much detail as possible: what connection. Have you connected the delegate property of the text view? If not then you cannot expect to receive delegate messages.

Yes I seem to be missing that. So i've added this to the .h:

@interface DetailViewController : UIViewController <UITextViewDelegate, UIPopoverControllerDelegate, UISplitViewControllerDelegate> {


Connected textview so it now says "Referencing outlets: delegate textview" in interface builder.

However not too sure how to integrate this with the method...

I found this example but not sure how to implement:


myAppDelegate *appDelegate
= (myAppDelegate *)[[UIApplication sharedApplication] delegate];
[[[appDelegate rootViewController] flipsideViewController] myMethod];

robbieduncan
Aug 15, 2010, 08:23 AM
I don't see what the code you posted has to do with this at all. That is dealing the an application delegate, not a text view delegate.

GregD
Aug 15, 2010, 01:17 PM
I don't see what the code you posted has to do with this at all. That is dealing the an application delegate, not a text view delegate.

Got it working... as it's a splitviewui template I had to put it in DetailViewController.m and RootViewController.m

Now just wondering how I insert the date once the user presses return? :)

pflau
Aug 15, 2010, 03:09 PM
Usually the way to insert some content into an edit box when the user move the cursor into that box is not through the begin edit event, but through some type of focus event.