To get view object from UITableviewCell - iphone

I have added a textfield in oe of the cell in my tableview.
Now I want to compare if the touched object is textfield or not. For that I am using -
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
UITouch *touch = [[event allTouches] anyObject];
}
method. Here I have to get the view touched in UITableview cell is textfield.
How can I get that? I am struct at this. Please help.

Use TextField Delegate method :
-(void)textFieldDidBeginEditing:(UITextField *)textField

I think the best way to achieve this would be:
Create a UITableViewCell subclass with a UITextField as a subview. Define it as an IBOutlet and hook it up via interface builder.
Create a delegate protocol to notify a delegate (in your case it could be the same class handling the UITableViewDataSource) of this event.
Declare the UITableViewCell subclass as a UITextFieldDelegate and hook it up to the UITextField you've created. via IB
Implement
-(void)textFieldDidBeginEditing:(UITextField *)textField {
[self.delegate textFieldWasTapped];
}
Now you'll be notified in the main controller of a textFieldWasTapped event. There's no need to check if it indeed came from a UITextField object because only instances if this type will be able to trigger this call.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
UITouch *touch = [[event allTouches] anyObject];
if([[touch view] isKindOfClass:[UITextField class]])
{
UITextField *txtField = (UITextField *) [touch view];
//UITextField object
}
}

Related

touchesBegan is not triggered

I used the codes below to detect touch on the object
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *allTouches = [touches allObjects];
for (UITouch *touch in allTouches)
{
NSLog(#"TOUCH DETECT");
}
}
but it is not triggered
Welcome any comment
What kind of object? touchesBegan:withEvent: is only called for UIResponder subclasses.
Also, if it's a UIView, make sure userInteractionEnabled is YES.

how to set touch event only on specified View

how to get a touch event only on specified view.Not on other view
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:drawImage];
}
In this code touch is on anyObject.
is there any why i can give touches to only specifies view.
You can use the property view on the UITouch, to find which view it occurred on
#property(nonatomic, readonly, retain) UIView *view
UITouch
Or you can find all the touched for a particular view, from the UIEvent param
- (NSSet *)touchesForView:(UIView *)view
UIEvent

how to move a image by touching and place in another location in iphone

I'm developing a "match the correct pair" answer app.
Here there will be two sections.(for example animal and legs).
In animal section there will hen,dog,cow,fox etc.(These options will be static.)
Now under the legs section there will be 2,4,5,6 etc.
The 2,4,5,6 etc will be random order.
The user will suppose select "2" and move to "hen" then the match is correct and will award a point.
If user select "2" and moved to "dog" the match is wrong and no point is awarded.
So how can i implement the moving function and also to check the correct match.
I hope i have explained my doubt in detail and if not please forgive me and im ready to explain more if needed.
Can anyone please help me.
Thanks in advance
Since it would be static, you will have the frame positions of each label (leg) I guess. You can find which label has been selected by using CGRectContainsPoint in touchesBegan: withEvent: method.
You can move the label by setting its frame position continuously in touchesMoved: withEvent: method.
Again you have to find whether this label intersects any label in animal side.
Edit:
For moving a view, pl. refer Listing 2-4 in this link
if you don't need to drag it, just set some buttons for your legs section, and set title (or any image) for them useing your random variables. In callbacks for this buttons "remember" what of them was pressed (optional, set other buttons userIntractionAnabled property to "NO"). then, make more buttons for your animal section. when user pressed one of tham, just move your legs-buttons to any position:
[UIView animateWithDuration:1 animations:^{
[button setCenter:CGPointMake(X, Y)];
}];
just in case:
in your buttons callback you have a "sender" value. this is pressed button. so you can use it:
[UIView animateWithDuration:1 animations:^{
[sender setCenter:CGPointMake(X, Y)];
}];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
touchDone=YES;
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];
[self loadingView];
}
-(void) loadingView{
if (touchDone){
drawImage.frame = CGRectMake(lastPoint.x, lastPoint.y, 70, 70);
}
}
.h viewController
#interface TouchViewController : UIViewController {
IBOutlet UIView *viewImage;
IBOutlet UILabel *touchesLabel;
CGPoint lastPoint;
}
#property (nonatomic, retain) IBOutlet UIView *viewImage;
#property (nonatomic, retain) IBOutlet UILabel *touchesLabel;
-(void) redrawView:(CGPoint) lpoint;
#end
in .m Implementation file
#implementation TouchViewController
#synthesize touchesLabel,viewImage;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
[self redrawView:lastPoint];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
[self redrawView:lastPoint];
}
-(void) redrawView:(CGPoint) lpoint
{
viewImage.frame=CGRectMake(lpoint.x, lpoint.y, 40, 30);
}
- (void)dealloc {
[viewImage release];
[touchesLabel release];
[super dealloc];
}
#end
*Note ViewImage is UIView Type but contains the image...can you how to upload on stackoverflow so that i will upload my project here if you want.

self.viewDelegate = 0x0 why?

In the following code the
self.viewDelegate is 0x0 and I don't know how to solve
this problem. Should I have to go to the interface Builder?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch tapCount] == 2) {
if ([self.viewDelegate respondsToSelector:#selector(openFlowView:doubleTapOnIndex:itemView:)])
[self.viewDelegate openFlowView:self doubleTapOnIndex:selectedCoverView.number itemView:selectedCoverView];
}
viewDelegate is not defined.
Usually, in Apple class, view delegate is just call delegate. You should have taken this code from somewhere.
You have to defined viewDelegate in your code (not IB)
self.viewDelegate=xxxx;
xxxx must be your delegate. Delegate could be self !
See Apple doc for delegate explanation

How to get keyboard to disappear?

I have a text field that is being shown in a UITableViewCell and I want to be able to hide the keyboard when the user touches anywhere else on the screen aside from the text field. I know about [field resignFirstResponder];, but I don't know how to intercept touches on the background of the UITableView in order to call "resignFirstResponder".
Any help would be greatly appreciated.
The only way to do this is to subclass the UITableView, implement the touchesBegan method in your subclassed UITableView and send your UITextField objects to the UITableView. Here's how it should look like -
// GRTableView.h
// StackOverflow Example
//
// Created by Raphael Caixeta on 8/13/10.
// Copyright 2010 Raphael Caixeta. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface GRTableView : UITableView <UITableViewDelegate> {
UITextField *textField;
}
#property(nonatomic, retain) UITextField *textField;
#end
//
// GRTableView.m
// StackOverflow Example
//
// Created by Raphael Caixeta on 8/13/10.
// Copyright 2010 Raphael Caixeta. All rights reserved.
//
#import "GRTableView.h"
#implementation GRTableView
#synthesize textField;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[textField resignFirstResponder];
[super touchesBegan:touches withEvent:event];
}
- (void)dealloc {
[super dealloc];
}
#end
And then in your regular file where you'll allocate a UITableView, just allocate the subclassed view and pass your textfield to the subclass. Hope that helps.
Try implementing the - (void)textFieldDidEndEditing:(UITextField *)textField method of the UITextFieldDelegate.
It's very simple: create a transparent UIView object that receives touches in the area you want, and when the touch is in the bounds of that view, call resignFirstResponder.
// somewhere in a view controller
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:backgroundView];
// in the touchesBegan:withEvent: method, for example
UITouch *touch = [[event allTouches] anyObject];
if ([field isFirstResponder] && [touch view] == backgroundView) {
[field resignFirstResponder];
}
Alternatively, you could skip the backgroundView stuff and just add a conditional statement like the following in your touchesBegan:withEvent: method:
UITouch *touch = [[event allTouches] anyObject];
if ([field isFirstResponder] && [touch view] != field) {
[field resignFirstResponder];
}
If the touch is ! (not) in the bounds of field, then you want to remove the keyboard.
You can hide the key board in following conditions,
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }
(void)TextField:(UITextField *)TxtFld textDidChange:(NSString *)searchText{ }
In this two condition you should place [TxtFld resignFirstResponder]; And also when your work for textfield is over you are performing some actions on that event also you should write [TxtFld resignFirstResponder]; where TxtFld is an object for UIText field.
By using this thing the keyboard will dismissed. The touch method you are writing may not work on the UITableviewCell. so this will be the way to hide the key board.
still it not work then specify the exact problem occurs.
There's an easier way to do this than the standard "subclass UITableView" or "add a subclass of UIView and handle touches". I create a UIButton with a clear background color over the area that I want to handle touches. When I add it to the view, I set its hidden property to YES.
When the text field begins editing, I set the button's hidden property to NO.
Then the button simply responds to UITouchUpInside control event, in a method which calls [textField resignFirstResponder] and button.hidden = YES.