table view not receiving touches when dismissing keyboard - iphone

I need to dismiss keyboard when touching outside of textfield. I have a main view controller view controller and another view controller embedded in the top right corner of the main view. It's embedded using container view (special object in IB).
The problem is that when I add gesture recogniser, the keyboard is dismissed but my another table view (in the center) is not receiving taps (it still scrolling but I can't select row). Table view is also an embedded view controller. This code is in my ViewDidLoad:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
And my dissmisKeyboard method:
-(void)dismissKeyboard
{
NSArray *childVc = [self childViewControllers];
for(id child in childVc)
{
if([child isKindOfClass:[MyTableViewController class]])
{
[child.tableVIew endEditing:YES];
}
}
}

How about?
-(void)dismissKeyboard
{
[self.textField resignFirstResponder];
}

Related

iPhone scroll UITableview by swiping out side the table view

In my case I have UIView and UITableView controller. What I want is to scroll the table view by swiping the UIView. I have added swipe up and swipe down gestures for the UIView. When I swipe on this view, scroll the table view along the direction of swipe. Is it posible?
Call this method from your viewDidLoad.
This method will disable the scrolling of the tableview to prevent manual scrolling.Assuming that you have 100 rows of height 30 and myview is the view on which have to perform the scrolling. Swipe gestures are added on myview,it is important to set the direction of the swipe
-(void)initialSetup
{
[self.tableView setScrollEnabled:NO];
int numberOfRows=100;
int rowHeight=30;
myview=[[UIView alloc]initWithFrame:CGRectMake(280, 0, 30, numberOfRows*rowHeight)];
[myview setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:myview];
UISwipeGestureRecognizer *swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(motion:)];
[swipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[myview addGestureRecognizer:swipeDown];
UISwipeGestureRecognizer *swipeUp=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(motion:)];
[swipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[myview addGestureRecognizer:swipeUp];}
The below method will be called when a swipe gesture occurs either swipeUp or swipeDown.
When the method receives a swipeUp gesture it scrolls the last currently visible row in the tableview to the top
When the method receives a swipeDown it scrolls the first currently visible row to the bottom
-(void)motion:(UISwipeGestureRecognizer *)sender
{
if (sender.direction==UISwipeGestureRecognizerDirectionUp) {
[self.tableView scrollToRowAtIndexPath:[self.tableView.indexPathsForVisibleRows objectAtIndex:[self.tableView.indexPathsForVisibleRows count]-1] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}else if(sender.direction==UISwipeGestureRecognizerDirectionDown)
{
[self.tableView scrollToRowAtIndexPath:[self.tableView.indexPathsForVisibleRows objectAtIndex:0 ] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
//let myView be the view from which you want to scroll your tableview(myTableview)
//now add the following lines in your viewdidload method
UISwipeGestureRecognizer * swipegesture = [[UISwipeGestureRecognizer alloc]init];
swipegesture =[myTableview.gestureRecognizers objectAtIndex:1];
[myView addGestureRecognizer:swipegesture];

How to implement close a subview when user touched its superview in iphone?

I have a view controller and I have created an another view in same view controller and added it as a sub view.When a button clicked the subview will appear and it takes half of the screen.How can I close the subview when user touched the superview(main view in background) ?
You can use a gesture recognizer and on a single tap action on the superview call removeFromSuperview on the subview.
In your viewDidLoad add something like this:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[mainView addGestureRecognizer:singleTap];
And then add the function:
- (void) handleSingleTap: (id) sender
{
[secondView removeFromSuperView];
}
For more control of when you want the subview to be removed, you could override the mainViewController's responder method like so:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[subview removeFromSuperView];
}
This way you can actually see where in the screen the user tapped, and respond accordingly.

touchesBegan to dismiss a view

I have a ViewController with a full screen UIImageView. I've hidden statusBar and NavigationBar, so there is no way to go back but tapping somewhere.
So i was thinking to go back just using this touchesBegan
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self dismissModalViewControllerAnimated:YES];
}
I also tried to use this way
-(void)viewDidLoad
{
[super viewDidLoad];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.navigationController setNavigationBarHidden:YES];
UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissModalViewControllerAnimated:)];
[imageFrame addGestureRecognizer:tapRecognizer];
imageFrame.image = [UIImage imageWithContentsOfFile:image];
[tapRecognizer release];
}
I got stuck there, with this full screen image and i can't go back.
How can I dismiss the viewcontroller?
If you are using a navigation bar I'm assuming that you did a pushViewController to start this view controller? In which case you would want
[self.navigationController popViewControllerAnimated:YES];
to dismiss the controller.
Of course your solution assumes that the image view is in fact a modal view controller presented with presentModalViewController:animated: somewhere.
My suggestion would be to have a UIButton placed on top of your image view and sized the same. Hook up its Touch Up Inside to your go-back method and then set its alpha to 0 or Hide it through IB or properties. The button won't be displayed but it'll still receive touch events.
You should call dismissModalViewControllerAnimated: on the parent view controller, and not on the one you wanna hide.
Also make sure to set the UIImageView's userInteractionEnabled property to yes, because unlike regular UIView's it doesn't answer touch events by default.

Can't Recognize Touch On navigation Bar's Image View.?

I have a View With Navigation Bar
Actually I have taken a Image View and Put it on a navigation bar....
As I want Button Not at right or Left but at somewhere on the navigation bar
By this method I can recognize touch on view but can not recognize touch on Navigation Bar ?
Can I change anything...
In this method...
Why?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
CGPoint p;
p.x=0;
p.y=0;
p = [[touches anyObject] locationInView:self.nvbrCalculateBar];
NSLog(#" %f",p.x);
if(p.x==0 && q.x==0)
{
}
else
{
//Load View
}
}
You can add a UITapGestureRecognizer to your UIImageView and that should do the trick
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapEvent:)];
[yourImageView addGestureRecognizer:tap];
[tap release];
- (void)tapEvent:(UITapGestureRecognizer *)gesture {
//Do whatever you need when user taps the imageView
}
Also set the userInteractionEnabled of the imageView to YES to be able to receive the gesture recognizer events.
Hope this helps.
The UINavigationBar itself is not set to return touches, since I believe its not part of the actual UIView (for which the touchesBegan) method applies. Instead, you can push various items onto the nav bar (e.g. buttons) which you can define interactions for.
See its reference here.
USer Interaction Must be enabled of UINavigation Bar Property, And then Apply touch method..
Can you try:
[imageView setUserInteractionEnabled:NO];
This should prevent your imageview to cancel the touches.

How to swap views using a swipe gesture XCode

I am using XCode to develop a Cocoa touch application for the iOS platform but have had trouble finding out how to get a swipe gesture implemented that would allow the user to swipe their finger left or right to change to a new ViewController (nib/xib file). I have done a swapView IBAction using a button and modal transitioning and I have read about Apple's TouchGestureRecognizer but I don't know how to implement a swipe action that would allow a view change.
I do NOT want to use a scroll view, as I have several dozen view controllers, that I want the user to be able to swipe through.
Here is an example:
First View Controller.xib:
SwipeRight- Go to second View Controller.xib
Second View Controller.xib:
SwipeLeft- Go to first View Controller.xib
SwipeRight- Go to third View Controller.xib
etc, etc
I have not used UISwipe/Touch Gestures before but I have used an IBAction method to switch views using a button with Modal Transitioning (see below):
-(IBAction)swapViews; {
SecondViewController *second2 =[[SecondViewController alloc initWithNibName:#"SecondViewController" bundle:nil];
second2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:second2 animated:YES];
[second2 release];
}
Is using a swipe to do a similar method formatted differently? If so, how do I sort this out and format it.
Thank You
Edit - Answer as Per Comment on Question
Place this in your viewDidLoad
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeftDetected:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
Then add a selector as by pasting the following code into your main...
- (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender {
NC2ViewController *second2 =[[NC2ViewController alloc] initWithNibName:#"NC2ViewController" bundle:nil];
second2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:second2 animated:YES];
[second2 release];
}
Then just make sure you import the otherViewController you are swapping to using
#import "SecondViewController"
at the top of your main file. Hope this helps.
End Edit
This sounds like a perfect time to use UIGestureRecognizer or, more specifically, UISwipeGestureRecognizer.
For more info on how to use them, read up in the Gesture Recognizers section of the Event Handling Guide.
Lets assume you want to swipe left to bring up another view from the right.
In the storyboard, drag and drop a swipe gesture recognizer. It will make an icon below the view controller; drag this icon and drop onto the ViewController you want to navigate to. This will add a segue, select custom segue. Then create a UIStoryboardSegue class. Add the following code:
- (void)perform {
UIViewController* source = (UIViewController *)self.sourceViewController;
UIViewController* destination = (UIViewController *)self.destinationViewController;
CGRect sourceFrame = source.view.frame;
sourceFrame.origin.x = -sourceFrame.size.width;
CGRect destFrame = destination.view.frame;
destFrame.origin.x = destination.view.frame.size.width;
destination.view.frame = destFrame;
destFrame.origin.x = 0;
[source.view.superview addSubview:destination.view];
[UIView animateWithDuration:0.5
animations:^{
source.view.frame = sourceFrame;
destination.view.frame = destFrame;
}
completion:^(BOOL finished) {
UIWindow *window = source.view.window;
[window setRootViewController:destination];
}];
}