How to make Segue from TableViewCell image in another View? http://i.stack.imgur.com/4WOGx.png
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
self.CommentAvatarImageView.userInteractionEnabled = YES;
[self.CommentAvatarImageView addGestureRecognizer:tap];
- (void )imageTapped:(UITapGestureRecognizer *) gestureRecognizer
{
NSLog(#"AVA Tapped");
}
Connect your cell to the view that you want it to segue to when selected. For example, if you want to segue to "nextViewController", then ctrl drag from the respective Table View Cell - Cell in your source Table View to nextViewController. Change the storyboard segue identifier to "nextView".
Then in your didSelectRowAtIndexPath
[self performSegueWithIdentifier:#"nextView" sender:self];
Related
I have two views, both are UIViewControllers, the first one contains a tableview with a button that segues to a camera view which is the second one.
So The first ViewController has been done in storyboard, including the segue to the second ViewController which is done by a "Show". The second ViewController has been done mostly in code, I have a UIImageView which I have added a selector method too which I want to use to navigate back to the first ViewController when pressed.
How do I do this in code as currently the UIImageView overlay I have created for the second ViewController which presents the camera view entirely in code.
Currently this is what I have created in code to initiate the method
//.....
UIImage * myImage = [UIImage imageNamed: #"Avatar_Mel"];
UIImageView *avatar = [[UIImageView alloc] initWithImage: myImage];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mySequeMethod)];
avatar.userInteractionEnabled = YES;
[avatar addGestureRecognizer:tap];
//.....
- (void)mySequeMethod {
// what should I do here? How to I show the first view controller again?
}
If second controller is modally presented
[self dismissViewControllerAnimated:YES completion:nil];
In case of navigation stack
[self.navigationController popViewControllerAnimated:YES];
I have a TabBarController having 3 viewcontrollers in it (namely VCa, VCb, VCc) and at VCa the tabbar is set as hidden.
Now whenever we tap(using UITapGestureRecognizer) the view at VCa, what should be the selector method to be implemented in order to view another ViewController (either VCb or VCc).
"At viewdidLoad of VCa"
[self.tabBarController.tabBar setHidden:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapmethod)];
[self.view addGestureRecognizer:tap];
-(void)tap{
// code to be written in order to show VCb(or any other ViewController in same TabBarController)
}
Everything is explained in the docs : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/#//apple_ref/occ/instp/UITabBarController/selectedViewController
In short :
- (void)tap {
self.tabBarController.selectedIndex = 1;
// or
self.tabBarController.selectedViewController = self.tabBarController.viewControllers[1];
}
The correct tab bar item will be selected.
Set the selectedIndex of UITabBarViewController to the VC that you want to show.
I got a search bar and a table view setup in my project. Now I want a nice way of dismissing the keyboard just when the user touches the screen anywhere outside of the search bar.
//Search bar tap recognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[tap setCancelsTouchesInView:NO];
[self.tableView addGestureRecognizer:tap];
This works so far - if I leave the following line out:
[tap setCancelsTouchesInView:NO];
The tap recognizer kind of overlaps the didSelectRowAtIndexPath method so if I wanted to select a row, I had to sort of slide across the cell to trigger the didSelectRowAtIndexPath method.
Anyways with that line if you tap outside of the search bar, the keyboard disappears but the didSelectRowAtIndexPath is also triggered - since most of the time the user would just tap in the middle of the screen - right at a cell of the table view.
How can I keep the table view method didSelectRowAtIndexPath from being called the first time if the search bar is still "active"? Because I think it's pretty annoying if you just want to dismiss the keyboard and you then the detail view of the tapped cell shows up.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if([self.view isFirstResponder])
{
[self.view endEditing:YES];
cell.userInteractionEnabled = NO;
} else {
cell.userInteractionEnabled = YES;
//if you have to do some actions when cell is selected
}
}
In order to dismiss the keyboard just when the user touches the screen anywhere outside of the search bar, you can call "dismiss keyboard" method inside table view didSelectRowAtIndexPath delegate. So it will work as needed.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self dismissKeyboard];
// you can write detail page navigation code here.
}
I'm trying to use an UITextView as a button, and after clicking on the UITextView it should pop to to another UIViewController (not the root one, next one).
Using an UIButton works fine (by dragging to next UIViewController and choosing "Push" as the segue type, without doing any coding). But, doing the same thing with a UITextView does not work!
So, my question is how to navigate to the next view controller using an UITextField?
Add Tap Gesture to UITextView
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mySelector)];
tapRecognizer.numberOfTapsRequired = 1;
[_textView addGestureRecognizer:tapRecognizer];
Push or Present UIVieWController in UITapGestureRecognizer action method.
-(void)mySelector
{
// Navigate to next view controller
}
Try adding a UITapGestureRecognizer to the text view and using that callback to trigger a segue.
I don't think you can do this without code.
Set the userInteractionEnabled attribute?
textView.userInteractionEnabled = YES;
You should be able to use the textView like a button...
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myTapped:)];
tapRecognizer.numberOfTapsRequired = 1;
[txtView addGestureRecognizer:tapRecognizer];
-(void)myTapped::(UIGestureRecognizer *)gesture
{
// Do what you want here
}
try this code
Right now I am using SWRevealViewController class in my project.
The basic functionality allows me to swap front view by pressing navigation bar button. But I want to add gesture to entire view.
I can add this code and it works for my button.
[self.startTestButton addGestureRecognizer:self.revealViewController.panGestureRecognizer];
But it just works for the one UI element. So I can't add, for example, other UI element to this gesture.
This code below shows how panGestureRecognizer method has been written:
- (UIPanGestureRecognizer*)panGestureRecognizer
{
if ( _panGestureRecognizer == nil )
{
SWDirectionPanGestureRecognizer *customRecognizer =
[[SWDirectionPanGestureRecognizer alloc] initWithTarget:self action:#selector(_handleRevealGesture:)];
customRecognizer.direction = SWDirectionPanGestureRecognizerHorizontal;
customRecognizer.delegate = self;
_panGestureRecognizer = customRecognizer ;
}
return _panGestureRecognizer;
}
To add the gesture recognizer to the whole view just add it to the whole view instead of just a single button. I'm using SWRevealViewController and my main view is a UITableView so to get the gesture recognizer to work on the whole view I have this in the viewDidLoad method of my UIViewController:
[self.tableView addGestureRecognizer: self.revealViewController.panGestureRecognizer];
So, just add the recognizer to the view you like. For most UIViewContollers this will be self.view.
Obviously if any controls or subviews of the view have their own gesture recognisers, these will take precedence of the one on the top level view, such that the panning will only work in the areas not occupied by those subviews.
//UPD:
Seems my previous solution is a overkill, you just could call getter to get the default behaviour. I.e.
[revealViewController panGestureRecognizer];
// old solution
You could also add SWRevealViewController's panGestureRecognizer to it's own view, for example in a subclass of SWRevealViewController you may have:
- (void)viewDidLoad
{
[super viewDidLoad];
// adds gesture recognizer to the whole thing
[self.view addGestureRecognizer:self.panGestureRecognizer];
}
If you're using a navigation controller and try to add the gesture recognizer to self.view, the gesture won't respond when swiping on the navigation bar. What I did was go into SWReaveal Classes and add a second gesture recognizer. So now I add one recognizer to self.view and one to the navigation bar. Works perfectly. I can share code if you need it but it shouldn't be too hard
To add Gesture on entire front view using SWRevealViewController using storyboard, we have to add
SWRevealViewController *revealController = [self revealViewController];
[revealController panGestureRecognizer];
[revealController tapGestureRecognizer];
in the the FirstViewController of the app. Add Action and Target to the Navigation bar button like
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = #selector(revealToggle:);
// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
In the SWRevealViewController.h add following method like
- (UITapGestureRecognizer*)tapGestureRecognizer;
- (BOOL)revealControllerTapGestureShouldBegin:(SWRevealViewController *)revealController;
In the SWRevealViewController.m add
- (UITapGestureRecognizer*)tapGestureRecognizer
{
if ( _tapGestureRecognizer == nil )
{
UITapGestureRecognizer *tapRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(_handleTapGesture:)];
tapRecognizer.delegate = self;
[_contentView.frontView addGestureRecognizer:tapRecognizer];
_tapGestureRecognizer = tapRecognizer ;
}
return _tapGestureRecognizer;
}
- (void)_handleTapGesture:(UITapGestureRecognizer *)recognizer
{
NSTimeInterval duration = _toggleAnimationDuration;
[self _setFrontViewPosition:FrontViewPositionLeft withDuration:duration];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ( _animationQueue.count == 0 )
{
if ( gestureRecognizer == _panGestureRecognizer )
// return [self _panGestureShouldBegin];
return ( gestureRecognizer == _panGestureRecognizer && _animationQueue.count == 0) ;
if ( gestureRecognizer == _tapGestureRecognizer )
return [self _tapGestureShouldBegin];
}
return NO;
}
In case of xib you can direct use
https://github.com/John-Lluch/SWRevealViewController
On Swift
self.revealViewController().frontViewController.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
If you are using a navigation controller for your front view you can add the pan gesture to the nav controller's view like this
[self.navigationController.view addGestureRecognizer: self.revealViewController.panGestureRecognizer];
That way you can also swipe on the navigation bar without altering any classes of the SWReveal controller.