Get touch events in a UITextView - objective-c

Apple does not provide touch event handling for UITextView. The project I'm developing includes notes that can be readable and editable, and I am using a UITextView. Please help me with this problem -- how can I get touch events for my project?

According to the Apple documentation, you may set a delegate for UITextView and implement the UITextViewDelegate.
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html
Specifically, you can determine that a UITextView was touched by implementing textViewShouldBeginEditing.
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
Be sure to set the delegate property on the UITextView in order to catch the event.

Just wanted to add to Kevin's answer. UITextView inherits from UIScrollView. You should be getting all the events declared in UIScrollViewDelegate protocol for the text view.
I got:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
when I declared my class as the delegate of UITextView.

Create custom textview and provide touch events inside the custom class. then you can create methods in delegate of that custom class.

I resolved the issue by using my own logic.
While reading we are adding a Custom View over the textview and working with those methods:
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
But when we are in the editing mode it is not possible anymore. Anyhow my requirement is only in readable mode.
Thanks for all your support to resolve the issue.

Related

Logging touches' coordinates on ViewController

I have this newly created Empty project using XCode5. Added a storyboard with one blank View Controller. Created a custom view controller class.
Here's the question. How could I log or display the touches on this project?
You can use method - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

Affecting UIViewController from a child UIView

My question might sound rather strange, but still I didn't find any reference and I really need help.
so my point is, I have a ViewController which has a collection of UIViews, to be specific a subclass of UIView with
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
overridden
from this event i want to load a specific data (according on the UIView touched) on the UIViewController's child UIWebView
Is there a way to address parent ViewControllers children from the UIView that receives the action
Or am I looking into a completely wrong direction.
Any hel will be greatly appreciated, thanks in advance.
You can use delegate for example:
#protocol someProtocol
- (void)recivedTouch:(UITouch *)touch fromUIView:(UIView *)uiView;
in you view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
[self.delegate recivedTouch:touch fromUIView:self];
}
View controller implemed method from delegate:
- (void)recivedTouch:(UITouch *)touch fromUIView:(UIView *)uiView
{
if (uiView == self.someView)
{
// do some stuff with uiWebView
}
}
try to look at it differently. Your touchesBegan shouldn't do anything which is not connected with the view. Your view is just a UI component, it shouldn't control application flow (data loading). That's the job of the view controller.
Look how UIButton is implemented. The button does not do anything when clicked. The controller has to tell it "this is the method that should be called when the user clicks you"
There are two ways how to tell the view what it should do for a specific action - creating a delegate or passing a selector that should be called.
Consider also extending UIControl, which has most of the actions already implemented.
Alternatively use NSNotificationCenter: Send and receive messages through NSNotificationCenter in Objective-C?
Send a message from your view, hook the message from your controller. Notificationas can get a bit messy though, the delegate is probably a cleaner solution

touchesEnded method doesn't get called?

I am developing an application in which I use
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
}
-(void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
}
Now my problem in iphone 3 after touch move it is calling handleSingleTap function and touchesEnded didn't get called. This happen only on some lower part of the screen when I move there touch end didn't get called.If I move long then touchesEnded get called.
But this thing is working properly in IPhone4 and IPad.
In IPhone4 and IPad after touchsesMoved it is calling touchesEnded.
You're not the only one who has encountered this problem. I found a similar thread on Apple Support forum list : https://discussions.apple.com/thread/1507669?start=0&tstart=0
This thread dates from 2008 which coincides with the iPhone 3, so I guess there might be a chance this is an unfixed bug on iOS running on iPhone 3.
Otherwise if it's not the case, may be it's due to the fact that you are using a UIScrollView (whose controller is implementing the touch methods you listed above). By default, it has a method called :
delaysContentTouches
That is set to YES. May be you should change it to no and say how it behaves.
If you are not using a UIScrollView, may be you are using two view and the second view containing the "dead area" is not connected to your controller ?

UIViewController and UIScrollView

I have a custom UIViewController class whose view (hooked up in IB) is a UIScrollView. I want to know when the UIScrollView gets "touch up inside".
I overrode the
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
method but it doesn't get called. What am I doing wrong?
Did you override touchesBegan:: in the UIViewController or the UIScrollView? If you did it on the UIViewController, it needs to be in the UIScrollView (or whatever it contains).
The UIScrollView is intercepting the touch events before it "bubbles" to your UIViewController.
like ryan said, you need to override your touchesBegan:: method in the UIScrollView. you may need to create a custom scrollView for this purpose and set the scrollView you use (in IB) to that class. also, be sure to set the scrollView delegate to your view controller. that way, when touches are intercepted, it knows where to send them

Capture Touch Event on UITableViewCell and after enter didSelectRowAdTindePath method

I want to personalize my UITableView changing background when user tap on a specific cell.
I've a dedicated ViewController for each cell and if I implement touchesBegan method in this viewController i can change my cell background without any problem. The problem is that the method "didSelectRowAtIndexPath" of the UITableView is no longer called. How can I call it manually? I am in another viewController and I have no access to that method. or how can propagate to the touch tableView?
Thanks a lot
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//do your operations
//send it to super class
[super touchesBegan:touch withEvent:event];
}
[super touchesBegan:touch withEvent:event];
Are you sending your action to super class? If not then add this line.