UIPopoverController and UITapGestureRecognizer not working in ViewDidLoad - ios

When I add a UIPopoverController in the ViewDidLoad method, it displays an empty popover view. The same thing happens when I add a UITapGestureRecognizer; it doesn't trigger the attached method. If I put the code in my ViewDidAppear it does work, but in some cases ViewDidAppear is never called, depending on where in my storyboard the view is loaded.
Here's my code:
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor whiteColor];
popoverContent.view = popoverView;
pickerPopup = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
pickerPopup.delegate = (id)self;
[pickerPopup setPopoverContentSize:CGSizeMake(320, 240) animated:NO];
And the UITapGestureRecognizer:
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPicker:)];
[_form_from addGestureRecognizer:tap];

I solved it for now using a delay in the ViewDidLoad method like so
[self performSelector:#selector(custom_init) withObject:nil afterDelay:0.1];
Still don't get why the UITapGestureRecognizer doesn't trigger. It does work if I attach it to the view, but not if I attach it to a textfield in a static cell.

Related

Unable to get touch on my UIImageView - iOS

I need to make my every single UIImageView to be single tapped.
I have a viewController, on this view controller there is a view which i push another views on this view with containment.
I am trying to create gallery ,
What might I be doing wrong?
This is my code;
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Custom initialization
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPhotoDetail)];
[self addGestureRecognizer:singleTap];
[self setBackgroundColor:[UIColor lightGrayColor]];
}
return self;
}
-(void)openPhotoDetail
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"SideMenu" bundle:[NSBundle mainBundle]];
PhotoDetailViewController *photoDetailViewController = [storyboard instantiateViewControllerWithIdentifier:#"photoDetailViewController"];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.window.rootViewController.menuContainerViewController.centerViewController pushViewController:photoDetailViewController animated:YES];
}
Initially a UIImageView has user interaction disabled. Enalbe the user interaction of your UIImageView and it should work.
imageView.userInteractionEnabled = YES;
In Viewdidload set
YourImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(openPhotoDetail:)];
// Instead of self ,give your imageView to add gesture event
[YourImageView addGestureRecognizer: singleTap];
Click on the UIImageView, on storyboard,
Go to Attributes Inspector (The button that looks like a tuxedo, top right ,Second button).
Check the attribute "User Interaction Enabled"
Or if you want to do it by code,
in
-(void)viewDidLoad {
myImageView1.userInteractionEnabled = YES;
myImageView2.userInteractionEnabled = YES;
myImageView3.userInteractionEnabled = YES;
}
UIImageView *MyImageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"XYZ.png"]];
UITapGestureRecognizer *TapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPhotoDetail)];
[MyImageview addGestureRecognizer:TapGesture];
[MyImageview setUserInteractionEnabled:YES];
Can u use it like that

How do I make a popover appear over a button inside a collection view protype cell

I created a UpcomingCell.h and .m for the cell. In the button's tap method i tried this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
// cameraSystemMenu = [mainStoryboard instantiateViewControllerWithIdentifier:#"CameraSystemMenuViewController"];
UIView *anchor = sender;
UIViewController *viewControllerForPopover =
[storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
self.popover = [[UIPopoverController alloc]
initWithContentViewController:viewControllerForPopover];
[self.popover presentPopoverFromRect:anchor.frame
inView:anchor.superview
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
and I also tried this
self.popoverContent = [[PopOverViewController alloc]init];
UIView *popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
[popoverView setBackgroundColor:[UIColor purpleColor]];
self.popoverContent.view = popoverView;
self.popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 300);
UIPopoverController *contactPopover =[[UIPopoverController alloc] initWithContentViewController:self.popoverContent];
[contactPopover presentPopoverFromRect:self.remindButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES ];
[contactPopover setDelegate:self];
and countless other popover examples on stackoverflow and I cant get it to work.
Both examples crashes. The first one crashes on
the rect passed in to this method must have non-zero width and height. This will be an exception in a future release
The second one crashes because an UIPopoverController needs to be a property or an instance variable, otherwise it will be deallocated on the end of the method, because the View that's showing it will not retain it

How to display a popover programmatically from a uibutton that is also created programmatically (Not using interface builder)

I have a button I have created programmatically within a view controller. Once the button is pressed I want it to to use a method to create the popover programmatically.
The button which is created in the ViewDidLoad in my view controller.m
UIView *moreFundInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 540, 620)];
[self.view addSubview:moreFundInfoView];
[moreFundInfoView setBackgroundColor:[UIColor RMBColor:#"b"]];
btnContact = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
[btnContact setFrame:CGRectMake(390, 575, contactButton.width, contactButton.height)];
btnContact.hidden = NO;
[btnContact setTitle:#"Contact" forState:(UIControlStateNormal)];
[moreFundInfoView addSubview:btnContact];
[btnContact addTarget:self action:#selector(showContactDetails:) forControlEvents:UIControlEventTouchUpInside];
Then I have the method I use when the button is pressed.
-(void) showContactDetails: (id) sender
{
UIViewController *popoverContent = [[UIViewController alloc]init];
UIView *popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
[popoverView setBackgroundColor:[UIColor RMBColor:#"b"]];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 300);
UIPopoverController *contactPopover =[[UIPopoverController alloc] initWithContentViewController:popoverContent];
[contactPopover presentPopoverFromRect:btnContact.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES ];
[contactPopover setDelegate:self];
}
What am I missing here? Cause it runs fine, but as soon as I click the button the app crashes. I think it is a delegate issue, but I am not sure. Any advice would be appreciated.
I think this code will help you. You are certainly missing delegate methods
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(644, 425); //your custom size.
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
Just make sure you are not forgetting UIPopover Delegate methods or else application will definitely crash. It is mandatory.
UIViewController *controller = [[UIViewController alloc] init];
[view removeFromSuperview]; //view is a view which is displayed in a popover
controller.view = view;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:controller];
popover.delegate = self;
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
All I had to do was change the property from "retain" to "strong" in the .h file and it works, stopped the app from crashing.
Yes, changing property for "retain" to "strong" makes you to hold your picker view object.
I think the problem with your code was, UIPopoverController object gets deallocated automatically when method finishes.
making strong property gets you to strongly point an object.

UIPopoverController - SIGABRT when Presented

I'm attempting to present a popoverView when a barButton is pressed. Unfortunately, the app crashes with the signal SIGABRT every time I try to call it. The "Empty.xib" does have a view designed in it. (As I'm using a barButton, I was unable to use the frame, if you have a workaround for that, please say so too).
- (IBAction)loadPopover:(id)sender
{
UIView *someView = [[UIView alloc] init];
UIViewController *someVC = [[UIViewController alloc] initWithNibName:#"Empty.xib" bundle:[NSBundle mainBundle]];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:someVC];
someView.frame = CGRectMake(0, 44, 320, 372);
someView.backgroundColor = [UIColor lightGrayColor];
popoverController.delegate = self;
self.popoverView = popoverController;
[self.popoverView presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
// CGRect popoverRect = [self.view convertRect:[btn frame] fromView:[btn superview]];
CGRect popoverRect = CGRectMake(0, 88, 320, 311);
popoverRect.size.width = MIN(popoverRect.size.width, 100);
[self.popoverView presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Remove .xib part from initWithNibName:#"Empty.xib". Change line to this:
UIViewController *someVC = [[UIViewController alloc] initWithNibName:#"Empty" bundle:nil];
Edit:
From here -
The nib file name should not contain any leading path information.
Few suggestions here.
Check if the view's outlet is connected
Why do you need to allocate a UIView like the following? UIView *someView = [[UIView alloc] init];
Check if the sender is of type of UIBarButtonItem
What is the retain policy for self.popoverView? Check if it is retain or strong (if ARC)
Why do you present the popover two times?
Said this, here a simple snippet.
UIBarButtonItem* barButton = (UIBarButtonItem*)sender;
UIViewController *someVC = [[UIViewController alloc] initWithNibName:#"Empty" bundle:[NSBundle mainBundle]];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:someVC];
popoverController.delegate = self;
self.popoverView = popoverController;
[self.popoverView setPopoverContentSize:CGSizeMake(300, 200)];
[self.popoverView presentPopoverFromBarButtonItem:barButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
P.S. Make attention to memory if you don't use ARC.
Hope it helps.

Releasing a popover which takes great amount of memory

I am using a popover view to present a large amount of flags of which the your can select.
There is something wrong with my code since soon after I open this popover memory is not released (the viewcontroller "flagsViewController" is ok and clean, it does init and release each and every item inside of it.
What am I doing wrong? How can I free memory as soon as the popover is closed?
-(void)presentFlags
{
[self.popoverController dismissPopoverAnimated:YES];
FlagsViewController *controller = [[FlagsViewController alloc]
initWithNibName:#"FlagsViewController"
bundle:[NSBundle mainBundle]] ;
UINavigationController *container = [[UINavigationController alloc] initWithRootViewController:controller];
UISegmentedControl *ctrl = [[UISegmentedControl alloc] initWithItems:segmentedItems];
ctrl.frame = CGRectMake(0, 6, 500, 30);
[ctrl addTarget:self action:#selector(changeSeg:) forControlEvents:UIControlEventValueChanged];
ctrl.segmentedControlStyle = UISegmentedControlStyleBar;
//ctrl.momentary = YES;
ctrl.tintColor = [UIColor darkGrayColor];
UIImage *theImage = [UIImage imageNamed:#"highlight_country.png"];
[ctrl setImage:theImage forSegmentAtIndex:0];
[container.navigationBar addSubview:ctrl];
[ctrl release];
//
//create a popover controller
self.popoverController = [[[UIPopoverController alloc]
initWithContentViewController:container] autorelease];
[container release];
[popoverController setPopoverContentSize:CGSizeMake(500, 600)];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[popoverController presentPopoverFromRect:CGRectMake(popoverArrowPossition, 0.0, 0.0, 52.0) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[controller release];
}
Working with the UIPopoverController has been pretty difficult but I solved this problem by doing the following setting the Delegate of the Popover Controller to self (popoverController.delegate = self) and adding the UIPopoverControllerDelegate Protocol to your Class Header
Next, I implemented the - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController delegate method and here I released the popoverController and set it to nil.
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
[self.popoverController release];
self.popoverController = nil;
}
Please note: This delegate method won't be called if you dismiss the popover via code (i.e. using dismissPopoverAnimated), it'll only be called if this User dismisses it by tapping outside the popover etc.

Resources