UIButton in a custom UIView trouble - iphone

I have a simple (view-based) application. I want on tapping on custom UIView my button moved somewhere inside that view (for example to point 10,10).
My custom UIView is DrawView (DrawView.h and DrawView.m).
RotatorViewController (h. and .m).
I add to my DrawView an UIButton, connect with outlets my DrawView and UIButton. I add UITapGestureRecognizer in RotatorViewController and #selector(tap:).
Here is code of UITapGestureRecognizer
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:drawView action:#selector(tap:)];
[drawView addGestureRecognizer:tapGR];
[tapGR release];
}
#selector(tap:)
- (void) tap:(UITapGestureRecognizer *)gesture {
myButton.transform = CGAffineTransformMakeTranslation(10, 10);
}
But when i tap anywhere in DrawView application crashes. Here is log from console
2011-02-23 20:59:24.897 Rotator[7345:207] -[DrawView tap:]: unrecognized selector sent to instance 0x4d0fa80
2011-02-23 20:59:24.900 Rotator[7345:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DrawView tap:]: unrecognized selector sent to instance 0x4d0fa80'
I need your help

You said :
UITapGestureRecognizer in
RotatorViewController and
#selector(tap:)
and you wrote :
UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:drawView action:#selector(tap:)];
It means the actions is performed in the drawView delegate but you defined the selector tap: in the RotatorViewController.
I think you just have to replace the target drawView by self
UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];

The error indicates that instances of the DrawView class do not respond to tap: messages. I see that you have defined a tap: method in DrawView.m, but have you declared that method in the header? Like so:
DrawView.h
#class UITapGestureRecognizer;
#interface DrawView {
UIButton *myButton;
}
- (void) tap:(UITapGestureRecognizer *)gesture;
#end

Related

Unrecognized selector sent to instance (with UIBarButtonItem)

In my iPhone application I have such code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Instruction Controller";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(menuButtonClicked)];
}
#pragma mark private methodes
-(void) menuButtonClicked{
NSLog(#"menuButtonClicked");
}
But when I click on this button it raise an exeption: "Unrecognized selector sent to instance". How cam I resolve this issue?
UPDATE
2013-05-23 12:21:33.182 CICDP[3091:11603] -[__NSCFString menuButtonClicked]: unrecognized selector sent to instance 0x8128eb0
2013-05-23 12:21:33.188 CICDP[3091:11603] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString menuButtonClicked]: unrecognized selector sent to instance 0x8128eb0'
It looks like your controller class (the target of the button action) is being released before the button fires. The button is likely retained by its superview.
You need to do something to keep the controller class alive (some other instance needs to hold a strong reference to it) for the whole time the button is on display.
I have got the same issue but it wasn't a reference issue. The log 'Unknown class MyViewController in Interface Builder file.' helps me a lot.
I have solved this issue with the answer : Xcode 6 Strange Bug: Unknown class in Interface Builder file
To resolve this issue, be sure the module associated to your view is correctly set :
If you see Module None, there is the issue
Go on Module and just tap enter to see this :
You need to add a : to the selector and change the method
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Instruction Controller";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:#selector(menuButtonClicked:)];
}
-(void) menuButtonClicked:(id)sender {
NSLog(#"menuButtonClicked");
}

IOS. Unable to close view after clicking my Info button (newbie)

I have found some code for my infoButton which shows up my new credits.xib but I can't manage to come back to my RootViewController.
On my Credits.xib, I have linked my "Done" button with ToucheDown-FirstResponder-ToggleCredits Close.
Here is my code for the infoButton in my RootViewController.m in ViewDidLoad
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:#selector(toggleCreditsOpen:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:modalButton animated:YES];
//[button release];
[modalButton release];
and my code just after my ViewDidLoad
- (IBAction) toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"Credits" bundle:nil];
[self.navigationController presentModalViewController:theController animated:YES];
}
- (IBAction) toggleCreditsClosed:(id)inSender
{
NSLog(#"Button Pressed!");
//[self.navigationController dismissModalViewControllerAnimated:YES];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
I think I am missing something should I create a Credits.h and put the toggleCreditsClosed in it ?
Here is the stack trace
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7c67610> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key DoneButton.'
*** First throw call stack:
Here is my Credits.h
#import <UIKit/UIKit.h>
#interface Credits : UIViewController
{
IBOutlet UIButton *DoneButton;
}
#property (nonatomic, retain) UIButton *DoneButton;
#end
and my Credits.m
#import "Credits.h"
#implementation Credits
#synthesize DoneButton;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
}
- (IBAction) toggleCreditsClosed:(id)inSender
{
NSLog(#"Button Pressed!");
[self dismissModalViewControllerAnimated:YES];
}
#end
If a delete the DoneButton link, the Credits view shows up but I it's when I press the Done button that I have a problem
Costumes[402:11f03] -[UIViewController toggleCreditsClosed:]: unrecognized selector sent to instance 0x7a4c460
2012-10-24 22:19:33.271 Costumes[402:11f03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController toggleCreditsClosed:]:
Sorry but I don't understand what I have to do and I cannot upload an image to show you but
in Outlets the link is (view<->View) and in Received Actions (toggleCreditsClosed:<->Button-Done Touch Down)
Welcome on SO !
Yes, you should create a separate .h / .m for your Credits. Then tell Inteface Builder that your .xib is a Credits class. Then link your button to this .h with the action you want. Basically, your last method should be in Credits.m :
- (IBAction) toggleCreditsClosed:(id)inSender
{
NSLog(#"Button Pressed!");
[self dismissModalViewControllerAnimated:YES];
}
Watch out, you dismiss the modal view with self instead of self.parentViewController in your code !
(PS : answers you'll get may not always work. Don't hesitate to comment, telling us what (didn't) work !)

Is it possible to attach UIGestureRecognizer to subclassed UIImageView?

Getting a crash with "unrecognized selector sent to instance" message, when attaching a UIPanGestureRecognizer to subclassed UIImageView. I need to subclass UIImageView to hold custom properties, so I just did that and added those properties. I have tested panning on UIImageView directly, and it works as expected. However, it doesn't work with subclassed UIImageView. Here's the code (self.myImageView is subclassed UIImageView and self.defaultImageView is UIImageView ):
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer * pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveCustomImageView:)];
[self.myImageView addGestureRecognizer:pgr]; //nop, getting error during runtime on start panning
[pgr release];
...
pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveUIImageView:)];
[self.defaultImageView addGestureRecognizer:pgr]; //yep, that works
[pgr release];
The handler methods declarations are of the same structure
-(void)moveUIImageView:(UIPanGestureRecognizer *)pan{
...
}
-(void)moveCustomImageView:(UIPanGestureRecognizer *)pan{
...
}
UIImageView by default has setUserInteractionEnabled flag set to NO & setMultipleTouchEnabled set to NO. Did you try turning these flags to YES?
You can change this like so -
[self.myImageView setMultipleTouchEnabled:YES];
[self.myImageView setUserInteractionEnabled:YES];

Hide UIPopoverController with double tap

I've got splite-view application and of course there is a popover controller in the vertical DetailView, how can I hide with double-tap? thanks
You have to add a doubleTap gesture recognizer and call dismissPopoverAnimated:
First declare a gesture recognizer and configure it to your view:
UITapGestureRecognizer * doubleTapGesture = [[UIGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapCallback:)];
doubleTapGesture.numberOfTapsRequired = 2;
[yourView addGestureRecognizer:doubleTapGesture];
[doubleTapGesture release];
Then implement the callback:
- (IBAction) doubleTapCallback: (UITapGestureRecognizer *) sender
{
[yourPopOverController dismissPopoverAnimated:YES]
}

is Gesture Recognization working on buttons or not?

In my app, I wrote these lines of code :
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[mybutton1 addGestureRecognizer:tapper];
[mybutton2 addGestureRecognizer:tapper];
[mybutton3 addGestureRecognizer:tapper];
[tapper release];
}
-(void)tapped:(UIGestureRecognizer *)sender{
NSLog(#"I'am in tapped");
}
but nothing happened. why ? and if I need to get the button's currentTitle inside tapped, can I ?
Thanks
You're missing the recognizer delegate. Implement that. You should also add the UIGestureRecognizerDelegate protocol to your header file and make recognizer.delegate = self.
re: getting the title - you can get the title. in your tap event handler you can extract that information from the sender object. i'll post some code later...
You don't need to use a gesture recogniser just to detect when a button is pressed. A button knows when it's being pressed!
Try:
{
// Blah...
[myButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
// Other stuff
}
-(void)tapped:(id)sender {
NSLog (#"I'm in tapped!");
}
A gesture recognizer can only be attached to one view at a time. Handling single taps on buttons can just as easily be done using IBAction. You can create one IBAction and connect all three buttons to it.
- (IBAction)tapped:(id)sender {
UIButton *button = (UIButton *)sender;
NSLog(#"%#", button.titleLabel.text);
}
A similar question: UITapGestureRecognizer on a UIButton