iOS click on dynamic UIButton crashes - ios

I'm creating programmatically a UIButton with an Image.
UIButton * bericht = [[UIButton alloc] initWithFrame:CGRectMake(tableView.bounds.size.width- height, 0, height, height)];
[bericht setBackgroundImage:[UIImage imageNamed:#"bericht.png"] forState:UIControlStateNormal];
[bericht addTarget:self action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:bericht];
- (void) imageClicked:(UIButton *) sender {
NSLog(#"test message");
}
When I click on this button I got the following error code:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSSetM goToFirstTrailer:]: unrecognized selector sent to instance 0x7fdaf1e73fe0'
Does anyone know a solution?

Change your selector method to imageClicked.
UIButton * bericht = [[UIButton alloc] initWithFrame:CGRectMake(tableView.bounds.size.width- height, 0, height, height)];
[bericht setBackgroundImage:[UIImage imageNamed:#"bericht.png"] forState:UIControlStateNormal];
[bericht addTarget:self action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:bericht];
- (void) imageClicked:(UIButton *) sender {
NSLog(#"iaksdkjas");
}

Related

Getting crash when move from on viewcontroller to VC

I have one home screen. And i programatically create two button with action. One will go to sign up screen , and another will go to sign in screen.
I added the both sign in and sign up storyboard identity. But when i move from that home VC to sign in VC and Sign Up VC i am getting crash:
-[ViewController pressSignUp:]: unrecognized selector sent to instance 0x7fbb3a72e3f0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController pressSignUp:]: unrecognized selector sent to instance 0x7fbb3a72e3f0'
*** First throw call stack:
Here is my code:
// create button 1
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-50, self.view.frame.size.width/2 , 50.0)];
[button setBackgroundColor:[UIColor colorWithRed:188/255.0f green:155/255.0f blue:211/255.0f alpha:0.6f]];
[button setTitle:#"Sign Up" forState:UIControlStateNormal];
[button addTarget:self action:#selector(pressSignUp:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // title color
[self.view addSubview:button];
// Button Two
UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height-50, self.view.frame.size.width/2 , 50.0)];
[button1 setBackgroundColor:[UIColor colorWithRed:188/255.0f green:155/255.0f blue:211/255.0f alpha:0.6f]];
[button1 setTitle:#"Sign In" forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(pressSignIn:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // title color
[self.view addSubview:button1];
// Border color for one button
UIView *leftBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 1, 1, button.frame.size.height)];
leftBorder.backgroundColor = [UIColor colorWithRed:237/255.0f green:211/255.0f blue:246/255.0f alpha:0.6f];
[button1 addSubview:leftBorder];
-(void)pressSignUp
{
UIViewController *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"SignUpViewController"];
[self presentModalViewController:secondViewController animated:YES];
// Here You can write functionality
}
-(void)pressSignIn
{
UIViewController *secondViewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"SignInViewController"];
[self presentModalViewController:secondViewController animated:YES];
// Here You can write functionality
}
Why i am getting that crash.It any possible to move to Another Vc.
Please help thanks
You forget to implement the parameter for the sender:
-(void)pressSignUp:(UIButton*) sender {
}
-(void)pressSignIn:(UIButton*) sender {
}
And maybe you did not set the identifier in the interface builder:
It's under the Identity Inspector tab in IB. It's called "Storyboard ID". E.g.: SignUpViewController

Error with customized button

I have an error:
2013-08-01 01:09:18.433 VstupHelper[28332:11303] -[VHMasterViewController popViewController:]: unrecognized selector sent to instance 0x7566410
2013-08-01 01:09:18.434 VstupHelper[28332:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[VHMasterViewController popViewController:]:
Here is my code:
UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(0, 0, 50, 30)];
[bt setImage:[UIImage imageNamed:#"menu.png"] forState:UIControlStateNormal];
[bt addTarget:self action:#selector(popViewController:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton=[[UIBarButtonItem alloc] initWithCustomView:bt];
self.navigationItem.leftBarButtonItem=leftButton;
self.navigationItem.leftBarButtonItem.target = self.revealViewController;
self.navigationItem.leftBarButtonItem.action = #selector(revealToggle:);
I'm not good at English, so I apologize if I'm hard to understand.
You have several problems:
1) You are calling popViewController: on what I asssume is a UIViewController subclass, when you should be calling it on your UINavigationController.
2) popViewController: takes a bool value for whether to animate that transition. With the action, it gets passed the sender by default.
3) You then reassign the target and action for the button (but these don't get called when you create the UIBarButtonItem using the initWithCustomView: initializer: See docs).
How to fix this:
1) Call a method you create for the button handler:
UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(0, 0, 50, 30)];
[bt setImage:[UIImage imageNamed:#"menu.png"] forState:UIControlStateNormal];
[bt addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton=[[UIBarButtonItem alloc] initWithCustomView:bt];
self.navigationItem.leftBarButtonItem=leftButton;
2) In that method, pop the view controller, or do whatever you actually want to do:
- (void)buttonTouched:(id)sender
{
[self.navigationController popViewController:YES];
//Or what you assigned to the button action:
[self.revealViewController revealToggle:(not sure what you're supposed to have here)];
}

Why send addTarget Method have the : to work

Hoping someone can help me with why I should add a : to invoke the method.
Here is the viewDidLoad method
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor= [UIColor yellowColor];
//Add Button
CGRect buttonRect= CGRectMake(100, 100, 100, 44);
UIButton *clickButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[clickButton setFrame: buttonRect];
[clickButton setTitle:#"Click Me" forState: UIControlStateNormal];
[clickButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickButton];
[self buttonPressed:clickButton];
//Add UILabel
CGRect labelViewRect= CGRectMake(50, 30, 200, 44);
UILabel *labelview= [[UILabel alloc]initWithFrame:labelViewRect];
[labelview setText:#"Hello welcome to my app!"];
//Clear the UIView button's background
[labelview setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:labelview];
}
Here is the Button Pressed method
-(void)buttonPressed:(UIButton *)sender{
NSLog(#"This button was pressed");
self.view.alpha=((double)arc4random()/0x100000000);
}
When I remove the : from
[clickButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
i.e. am changing this above line to something like this
[clickButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
An exception is thrown:
2012-12-29 17:56:18.209 AlphaSchoolColor[11414:c07] -[com_skminfotekViewController buttonPressed]: unrecognized selector sent to instance 0xde2f050
2012-12-29 17:56:18.210 AlphaSchoolColor[11414:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[com_skminfotekViewController buttonPressed]: unrecognized selector sent to instance 0xde2f050'
Question to community is what is happening with that : and where are some more information I can learn about its importance.
Thanks.
Your method has an arg. If you want to send action without the ":" then change your method.
-(void)buttonPressed{
}

UIButton and selectors iOS

I have a button constructor class and in that class i have this method to design my buttons
- (void)setupWithPosition:(CGPoint)point withImage:(UIImage *)image withImageFloat:(NSString *)imageFloat withTitle:(NSString *)title withLineWidth:(CGFloat)lineWidth respondsToSelector:(NSString*)selector
{
// create button
ButtonWithImage* button = [[ButtonWithImage alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
//[button setTitle:[title uppercaseString] forState:UIControlStateNormal];
[button setupViewWithText:title andImage:image andImageFloat:imageFloat];
[button addTarget:self
action:NSSelectorFromString(selector)
forControlEvents:UIControlEventTouchUpInside];
CGRect buttonFrame = button.frame;
CGFloat lineX = 0.0f;
// setup image
if ([[imageFloat lowercaseString] isEqualToString:#"right"]) {
lineX = buttonFrame.origin.x + buttonFrame.size.width;
} else {
lineX = 0.0f;
buttonFrame.origin.x = lineWidth;
button.frame = buttonFrame;
}
self.line = [[UIView alloc] initWithFrame:CGRectMake(lineX, buttonFrame.size.height/2, lineWidth, LINE_HEIGHT)];
self.line.backgroundColor = [UIColor lineNormalColor];
[self addSubview:self.line];
[self addSubview:button];
}
and i'm calling that using:
homepageButtons = [[NSMutableArray alloc] init];
CategoryButtonView* za = [[CategoryButtonView alloc] initWithFrame:CGRectMake(200, 250, 300, 46)];
[za setupWithPosition:CGPointMake(100, 100) withImage:[UIImage imageNamed:#"zas.png"] withImageFloat:#"right" withTitle:#"za" withLineWidth:80.0f respondsToSelector:#"buttonClicked"];
the buttons are placed correctly but when i click on them i get that error
CategoryButtonView buttonClicked]: unrecognized selector sent to instance 0xa1a46a0
2012-11-02 17:53:43.844 Man[3432:14c03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CategoryButtonView buttonClicked]: unrecognized selector sent to instance 0xa1a46a0'
i guess its a SEL problem how can i solve that?
You have this line:
[button addTarget:self
action:NSSelectorFromString(selector)
forControlEvents:UIControlEventTouchUpInside];
Do you really want 'self' here? This means that whatever selector you pass in must be a method in the CategoryButtonView class, not whatever class is used to create the CategoryButtonView.
Most likely you want to add a target parameter along with the selector parameter.
had to add that to make it work
NSString *temp = [NSString stringWithFormat:#"%#:",selector];
[button addTarget:sender
action:NSSelectorFromString(temp)
forControlEvents:UIControlEventTouchUpInside];

Making custom UIView with two UIButtons for UITableViewCell accessoryView member causes unrecognized selector sent to instance error

I'm trying to make a view with two buttons for a table cell accessory view to do two (obviously) different things to the object at that cell index. I made two basic rounded rect UIButtons with a selector in the RootViewController (where the UITableView resides). Here is the code I use to initialize this view in a cell that is found in the cellForRowAtIndexPath method:
UIButton* minus = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[minus setFrame:CGRectMake(0, 0, 30, 30)];
[minus setTitle:#"-" forState:UIControlStateNormal];
[minus addTarget:self action:#selector(subtractOne:event:) forControlEvents:UIControlEventTouchDown];
UIButton* plus = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[plus setFrame:CGRectMake(30, 0, 30, 30)];
[plus setTitle:#"+" forState:UIControlStateNormal];
[plus addTarget:self action:#selector(addOne:event:) forControlEvents:UIControlEventTouchDown];
UIView* customAccessory = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
[customAccessory addSubview:minus];
[customAccessory addSubview:plus];
cell.accessoryView = customAccessory;
[customAccessory release];
And the two methods they call are defined:
- (void)subtractOne:(id)sender forEvent:(UIEvent *)event;
- (void)addOne:(id)sender forEvent:(UIEvent *)event;
Any ideas why this would throw unrecognized sender sent to instance "RootViewController"?
Here is the full error:
2011-03-20 20:34:35.493 MyApp[23262:207] -[RootViewController subtractOne:event:]: unrecognized selector sent to instance 0x573c350
2011-03-20 20:34:35.496 MyApp[23262:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController subtractOne:event:]: unrecognized selector sent to instance 0x573c350'
Realized my own stupid mistake: Was trying to call on subtractOne:event: when I wrote method for subtractOne:forEvent:

Resources