I've implemented 2 ImageViews with images and tapGestures, this works fine, now I want to segue to another ViewController dependent on which image I've tapped. I don't know how I have to declare my ImageView which ViewController it should call.
so my Code is:
-(void) initImageViews{
self.navigationItem.title = #"Menu";
UIImage *serviceImage = [UIImage imageNamed:#"Service.png"];
self.serviceImageView = [[UIImageView alloc] initWithImage:serviceImage];
[self.serviceImageView setFrame:CGRectMake(20, 20, 48,48 )];
[self.view addSubview:self.serviceImageView];
[self.view setBackgroundColor:[UIColor blackColor]];
UIImage *industryImage = [UIImage imageNamed:#"Industry.png"];
self.industryImageView = [[UIImageView alloc] initWithImage:industryImage];
[self.industryImageView setFrame:CGRectMake(73,20,48,48)];
[self.view addSubview:industryImageView];
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[self.industryImageView setUserInteractionEnabled:TRUE];
[self.serviceImageView setUserInteractionEnabled:TRUE];
[self.serviceImageView addGestureRecognizer:singleTap];
[self.industryImageView addGestureRecognizer:singleTap];
}
- (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer {
NSLog(#"Tapped!");
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Service"]) {
SelectOptionViewController *controller = (SelectOptionViewController*)segue.destinationViewController;
}if ([segue.identifier isEqualToString:#"Industry"]) {
SelectOptionViewController *controller = (SelectOptionViewController*)segue.destinationViewController;
}
}
}
Thanks in advance :)
[self performSegueWithIdentifier:IdentifierForTheSegueiFromStoryBoard sender:nil];
EDIT: the seque must exist in the storyboard and must have an identifier.
If you don't want/need to use segues you could use:
if (i want controller A) {
[self presentViewController:ControllerA animated:YES completion:nil];
} else if (i want controller B)
[self presentViewController:ControllerB animated:YES completion:nil];
}
Create a seague in the storyboard with the identifier.then programatically call a segue like this
[self performSegueWithIdentifier: #"myidentifier" sender: self];
Related
I have a View which is the subView of the main view in iphone app I want that when subView is shown and user taps on part of the screen except the subView then subView should hide.
Here is the code which I got but it does not hide it :
UITapGestureRecognizer *tapGR;
tapGR = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)] autorelease];
tapGR.numberOfTapsRequired = 1;
[self.View addGestureRecognizer:tapGR];
// Add a delegate method to handle the tap and do something with it.
-(void)handleTap:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded) {
// handling code
[myViewSheet removeFromSuperView];
}
}
implement UIGestureRecognizerDelegate protocol:
.h:
#interface YourView : UIView<UIGestureRecognizerDelegate>
.m:
#implementation YourView{
UIView * subview;
}
...
subview = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 320, 200)];
[self addSubview:subview];
UITapGestureRecognizer *tapGR;
tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tapGR.numberOfTapsRequired = 1;
tapGR.delegate = self;
[self addGestureRecognizer:tapGR];
...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view == subView)
{
return NO;
}
else
{
return YES;
}
}
You probably need to set userInteractionEnabled = YES on your main view (The one to which you are attaching the gesture recognizer.)
I am adding a UIImageView as a subview to a UIScrollView then i set the image.
Im trying to to use UITapGestureRecognizer.
The selector of the UITapGestureRecognizer is never called in iOS 5 (in iOS 6 it DOES!).
Tried many variations. this is my code:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(index*(IMAGE_WIDTH+10), 10.0, IMAGE_WIDTH, IMAGE_HEIGHT)];
[imgView setImageWithURL:[NSURL URLWithString:meal.RecommendedImageURL] placeholderImage:[UIImage imageNamed:#""]];
imgView.layer.cornerRadius = 4;
[imgView.layer setMasksToBounds:YES];
[imgView setUserInteractionEnabled:YES];
[imgView setMultipleTouchEnabled:YES];
[scrollView addSubview:imgView];
if (IOS_NEWER_OR_EQUAL_TO_5)
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[imgView addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.enabled = YES;
tapGestureRecognizer.delegate = self;
[tapGestureRecognizer setCancelsTouchesInView:NO];
}
this is my selector which is only called in iOS5:
- (void) imageTapped: (UITapGestureRecognizer *)recognizer
{
//Code to handle the gesture
UIImageView *tappedImageView = (UIImageView*)recognizer.view;
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = tappedImageView;
vc.liftedImageView.contentMode = UIViewContentModeScaleAspectFit;
if (IOS_NEWER_OR_EQUAL_TO_5) {
[self.parentViewController presentViewController:vc animated:YES completion:nil];
}
else
{
[self.parentViewController presentModalViewController:vc animated:YES];
}
}
In addition to that, i tried to setCancelsTouchesInView to YES in my UIScrollView but it doesn't work either.
Thanks for your help!
try to add Gesture in Scrollview then check iskind of class image view
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[scrollview addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.enabled = YES;
tapGestureRecognizer.delegate = self;
[tapGestureRecognizer setCancelsTouchesInView:NO];
- (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if ([touch.view isKindOfClass:[UIImageView class]]) {
// we touched a button, slider, or other UIControl
return YES;
}return NO;
}
Implement the 3 methods of UIGestureRecognizerDelegate & return default values:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
Ok i solved it very EASILY!
the problem was setting the delegate of the UITapGestureRecognizer to self.
when i removed the delegate = self, it started working :)
Thanks for your assistance
I'm trying to add a UIImageView to my ViewController after I dismiss a modal view controller.
But for some reason [self.view addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Logo.png"]; isn't adding the UIImageView to the display, or at least it's not visible. The example code that I'm calling in the modal view controller is below.
/* Modal View Controller */
- (IBAction)hideModal:(id)sender {
[self dismissViewControllerAnimated:YES completion:^() {
TestViewController *gv = [self.storyboard instantiateViewControllerWithIdentifier:#"testView"];
[gv view];
[gv addLogo];
}];
}
/* TestViewController */
-(void)addLogo {
[self.view addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Logo.png"];
}
Change the method call from completion block
/* Modal View Controller */
- (IBAction)hideModal:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
TestViewController *gv = [self.storyboard instantiateViewControllerWithIdentifier:#"testView"];
[gv view];
[gv addLogo];
}
/* TestViewController */
-(void)addLogo {
UIImage *myImage = [UIImage imageNamed:#"Logo.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImageView];
myImageView.frame = CGRectMake(0,0,100,100);
if(myImage)
{
[self.view addSubview:myImageView];
[self.view bringSubViewToFront:myImageView];
}
else
{
NSLog(#"no image found");
}
}
Change
[self addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Logo.png"]];
to
[self.view addSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Logo.png"] autorelease]];
The other way to do it, is to call addSubview method from viewDidAppear in your TestViewController
The completion block was not the same as the initial view controller being displayed.
When trying to reference the view controller being displayed under the modal view controller call the below code:
[self dismissViewControllerAnimated:YES completion:^() {
UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
gv = (TestViewController *)nav.topViewController;
[gv displayLogo];
}];
There is a basic form, pressing button call method performSegueWithIdentifier which shows the popover window. How could I black out (dim) the main view window until the popover is active?
I've tried to use the library SVProgressHUD like this:
- (IBAction)publishButtonAction:(id)sender {
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
[self performSegueWithIdentifier:#"fbshareSigue" sender:self];
[SVProgressHUD dismiss];
}
Obtained for a split second - until there is a popover-window.
Where must I insert this code, if I try this?:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"fbshareSigue"]) {
OFAFBShareViewController *svc = (OFAFBShareViewController *) [segue destinationViewController];
UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
svc.postBlock = self.postInitBlock;
[svc setClosePopWindow:[popoverSegue popoverController]];
}
}
- (IBAction)publishButtonAction:(id)sender {
UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.5f;
dimView.tag = 1111;
dimView.userInteractionEnabled = NO;
[self.view addSubview:dimView];
[self performSegueWithIdentifier:#"fbshareSigue" sender:self];
}
/* This code should be put in the other method where you are removing the popup.. Because it will remove the dim view. here is the wrong place for this code*/
/* for (UIView *view in [self.view subviews]) {
if (view.tag == 1111) {
[view removeFromSuperview];
}
}*/
Dim doesn't work...
====================
I have View OFAResultViewController that calling Popover (OFAFBShareViewController):
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"fbshareSigue"]) {
OFAFBShareViewController *svc = (OFAFBShareViewController *) [segue destinationViewController];
UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
svc.postBlock = self.postInitBlock;
[svc setClosePopWindow:[popoverSegue popoverController]];
}
}
- (IBAction)publishButtonAction:(id)sender {
UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.5f;
dimView.tag = 1111;
dimView.userInteractionEnabled = NO;
[self.view addSubview:dimView];
[self performSegueWithIdentifier:#"fbshareSigue" sender:self];
}
...
In OFAFBShareViewController I'm trying to close the dim view:
...
- (IBAction)cancelButtonAction:(id)sender {
[closePopWindow dismissPopoverAnimated:YES];
for (UIView *view in [self.view subviews]) {
if (view.tag == 1111) {
[view removeFromSuperview];
}
}
...
But it doesn't work again...
You can use the following code, when you display the popup.
In OFAFBShareViewController
- (void) loadView
{
[super loadView];
UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.5f;
dimView.tag = 1111;
dimView.userInteractionEnabled = NO;
[self.view addSubview:dimView];
}
After popup removed, delete this view from super view using the tag.
After iOS 5.0 there is a public API method also introduced:
======================================================
To remove the view you can use this code:
for (UIView *view in [self.view subviews]) {
if ([view.tag == 1111]) {
[view removeFromSuperview];
}
}
Leave your Remove Dim View code as it is. Only Change super.view to self.view again.
It'll work for you then.
Hope it works for you...
When you create the popover, add a dim view to the current view, and add the current view as a UIPopoverControllerDelegate of the popover controller:
Add the dimView:
let dimView = UIView(frame: view.frame)
view.addSubview(dimView)
dimView.backgroundColor = UIColor.blackColor()
dimView.alpha = 0.5
dimView.tag = 1234
dimView.userInteractionEnabled = false
view.addSubview(dimView)
Add the origin view as delegate of the popoverController:
popoverViewController!.delegate = self
Then, in order to remove the dimView when the popover is dismissed, set the current view as implementing the UIPopoverControllerDelegate, and implement the popoverControllerDidDismissPopover func. Inside this function, remove the dimView:
extension MyOriginViewController: UIPopoverControllerDelegate {
func popoverControllerDidDismissPopover(popoverController: UIPopoverController) {
for subView in view.subviews {
if subView.tag == 1234 {
subView.removeFromSuperview()
}
}
}
}
I have a view which shows an image from the web. The view only has an UIImageView. I want to know how to hide the navigationBar when the user taps and show it again when the user re-taps the view again. (Just like the native iPhone photo app)
I know i can use this
[self.navigationController setNavigationBarHidden:YES animated:YES];
but i am not sure where to use this,where to put in this code.
Help would be appreciated
Initialize a new UITapGestureRecognizer:
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(toggleNavigationBar:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.numberOfTouchesRequired = 1;
[self.imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
You also must make sure the UIImageView has userInteractionEnabled set to YES because by default it is set to NO on UIImageView's.
self.imageView.userInteractionEnabled = YES;
Finally, write the method that is called when the gesture recognizer recognizes. This is the method selector that is passed in the action: argument in the gesture recognizer's initializer method:
- (void)toggleNavigationBar:(UITapGestureRecognizer *)tapGestureRecognizer
{
[self.navigationController setNavigationBarHidden:![self.navigationController isNavigationBarHidden] animated:YES];
}
Put a UITapGestureRecognizer on your UIImageView and in the delegate just call the method you mentioned. Something like this:
UITapGestureRecognizer* g = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
[img addGestureRecognizer:g];
[g release];
Then your delegate:
-(void) imageTapped:(UITapGestureRecognizer*)tg
{
if(self.navigationController.toolbarHidden)
[self.navigationController setNavigationBarHidden:YES animated:YES];
else
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
If you can't figure out the other answers you can cheat a little bit. You can throw on a button set it to transparent and link a IBAction to it with the code:
UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake( x,y,0,0)];
imageButton.backgroundColor = [UIColor clearColor];
[imageButton addTarget:self action:#selector(navBarHide:)
forControlEvents:UIControlEventTouchUpInside];
-(IBAction)navBarHide {
if (!navBarHidden) {
[self.navigationController.navigationBar removeFromSuperView];
}
else {
[YourUIView addSubview: yourNavigationBar];
}
}