Cannot push to a XIB iOS - ios

In my app I tried to push to a XIB using a long tap event. I did the following code,
In viewDidLoad
UILongPressGestureRecognizer *longPress = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)]autorelease];
longPress.delegate = (id<UIGestureRecognizerDelegate>)self;
[self.view addGestureRecognizer:longPress];
[longPress requireGestureRecognizerToFail:doubleTap];
and longPress method is
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
ProductDetailViewController *vc = [[[ProductDetailViewController alloc] initWithNibName:#"ProductDetailViewController" bundle:nil] autorelease];
[self.navigationController pushViewController:vc animated:YES];
NSLog(#"Long Press");
}
}
I have a XIB named ProductDetails.xib and I set the File's Owner of it as ProductDetailViewController.
When I'm running this code NSLog is working. But it's not going to the ProductDetails.h. And also There are no errors. How can I fix this? Anyone can help me with this.
Thanks in Advance!

You already have the xib hooked up to the controller, you can achieve that with the following.
ProductDetailViewController *vc = [[[ProductDetailViewController alloc] init] autorelease];
[self.navigationController pushViewController:vc animated:YES];

Related

Panel sliding from the bottom

I want to implement panel, that by default appears from the bottom only by 1/4.
And when i tap on this part, or swipe it, it should roll out on full screen.
exactly what i want was implemented in this app
So what i tried already:
Making UIView and trying to apply tap and swipe gestures. UIView didn't catch it. But it caught touchesMoved, touchesEnded etc.
UITableView. Same.
Making UIViewController. Ended without any idea how to handle it to right way.
So any ideas or links are appreciated.
Thanks in advance.
Check PPRevealSideViewController https://github.com/ipup/PPRevealSideViewController, I think you can find what you need.
When you tap on the new viewController you can change the Offset.
AppDelegate.m
MainViewController *main = [[MainViewController alloc] init];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
main = [storyboard instantiateViewControllerWithIdentifier:#"MainViewControllerr"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main];
_revealSideViewController = [[PPRevealSideViewController alloc] initWithRootViewController:nav];
_revealSideViewController.delegate = self;
self.window.rootViewController = _revealSideViewController;
MainViewController.m
-(void)popViewController{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PoppedViewController *pvc = [storyboard instantiateViewControllerWithIdentifier:#"PoppedViewController"];
[self.revealSideViewController pushViewController:pvc onDirection:PPRevealSideDirectionBottom withOffset:_offset animated:_animated completion:nil];
}
PoppedViewController.m
-viewDidLoad{
//...
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(getFullView)];
gesture.delegate = self;
[self.view addGestureRecognizer:gesture];
}
-getFullView{
[self.revealSideViewController changeOffset:0.0f forDirection:PPRevealSideDirectionLeft];
}

UIPopoverController and UITapGestureRecognizer not working in ViewDidLoad

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.

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

Disable UIBarButtonItem when popover is dismissed

I am new in iOS development and I am trying to show a popover when tap a barButtomItem. So far I have this:
-(IBAction)shareButtonPressed :(id)sender{
UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:#"ShareOptionsViewController" bundle:nil];
popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
This code works and when I tap the correct button, the popover is showed. The problem is when the popOver is showed, if I tap again the button, the app crashes. I know is because it is trying to alloc a variable that is already created. I think the solution should be disable the button when the popover loads but I don't know how to activate again once the popover is dismissed.
Am I right? How can I enable the button when the popOver is dismissed?
First you should disabled the button in your shareButtonPressed method.
-(IBAction)shareButtonPressed :(id)sender{
UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:#"ShareOptionsViewController" bundle:nil];
popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
[self.myButton setEnabled:NO];
[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
Then you make your view controller comply with the UIPopoverControllerDelegate protocol. After that you implement the popoverControllerDidDismissPopover method.
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
if (!self.myButton.enabled) // Just to make sure the button is disabled.
[self.myButton setEnabled:YES];
}
Just check if instance is already created.
-(IBAction)shareButtonPressed:(id)sender
{
if (self.popoverController == nil) {
self.popoverController = alloc/init...;
[_popoverController present...];
} else {
if (self.popoverController.popoverVisible)
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
}
There is a possibility, it will be garbage collected before animation completes, yielding invalid user experience. Then, if possible, I would pre-create the instance in viewDidLoad or contructor or so and then just present and dismiss as needed, checking visible property.
Look at this "event --> responsible" list:
1) Button pressed --> MainViewController - IBAction
2) Popover shown --> MainViewController - IBAction
3) Button disabled --> MainViewController - IBAction
4) Popover dissmis --> popoverDelegate - popoverControllerDidDismissPopover
5) Button enabled --> popoverDelegate - popoverControllerDidDismissPopover
So, if the delegate of your popover is the MainViewController, you would have access to the button, if it is a different class you'll probably have to set up a protocol in order to enable the button back.
Tell me if you need any further explanations...
Just modify your code like this...
-(IBAction)shareButtonPressed :(id)sender{
UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:#"ShareOptionsViewController" bundle:nil];
popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);
if(self.popoverController)
{
[self.popovercontroller dismissPopoverAnimated:YES];
self.popovercontroller = nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
you can detect if the popover is shown and dismissed:
- (IBAction)shareButtonPressed :(id)sender{
if (self.popoverController) {
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
} else {
UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:#"ShareOptionsViewController" bundle:nil];
popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
}
or if you just want to disable the button:
- (IBAction)shareButtonPressed :(id)sender{
...
UIButton *button = (UIButton *)sender;
button.enabled = NO;
}
but in this case you will have to detect when the user close the popOver to enable the button again. You can do this adopting the UIPopoverController delegate (see documentation) in your class

UIPickerView inside a UIPopoverController memory leak

I'm relatively new to iOS programing so I'll do my best to describe the problem I'm having. If I can provide some additional info to clear things up please tell me.
I'm currently displaying a UIPickerView within a UIPopoverController within my app. The problem I'm having is when I'm profiling with the leaks tool, every time the popover is dismissed a memory leak pops up. Here is the code for initializing the popover and the picker view.
// Show the popover
CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
UIViewController *vc = [[UIViewController alloc] init];
[vc setContentSizeForViewInPopover:CGSizeMake(320, 216)];
UIPickerView *pv = [[UIPickerView alloc] init];
[pv setShowsSelectionIndicator:YES];
[pv setDataSource:self];
[pv setDelegate:self];
[[vc view] addSubview:pv];
inputPopoverController = [[UIPopoverController alloc] initWithContentViewController:vc];
[inputPopoverController setDelegate:self];
[pv selectRow:selection inComponent:0 animated:NO];
vc = nil;
pv = nil;
[inputPopoverController presentPopoverFromRect:cellRect inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
inputPopoverController is declared as
UIPopoverController *inputPopoverController;
And here's where I release the popover controller and I've confirmed that the function does get called every time the popover is dismissed.
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
inputPopoverController = nil;}
Instruments shows the leak at the following two locations
[pv selectRow:selection inComponent:0 animated:NO];
and
[inputPopoverController presentPopoverFromRect:cellRect inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Thanks in advance.
After a couple days of breaking my head, it turns out I needed to implement one of the UIPickerView delegate methods. Here's where I found my answer here

Resources