Error with customized button - ios

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)];
}

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

UIButton programatically is not working in iOS

When I click on the UIButton nothing happened
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
You have to create button like below.
UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom]; // this is important
checkbox.frame = CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
You have to create the button first using button property and then give its frame.
If still its not working then self.background addSubview should be self.view addSubview.
What is self.background?
Your code is perfectly fine. You need to check the following .
check whether self.background is placed properly in your view heirarchy, I tried your code with self.view instead of self.background like below and it works perfectly.
self.checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 50, 50)];
[self.checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox1.png"] forState:UIControlStateNormal];
[self.checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox2.png"] forState:UIControlStateSelected];
[self.checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.checkbox];
From the error message that you have given
"
Warning: Attempt to present <enter_the_den: 0x7fbf1bd4c4e0> on <ViewController: 0x7fbf1bc771c0> whose view is not in the window hierarchy!
I suspect the issue is in your code inside the method autologin, just comment all the code in your method and just try a log , it should work.
refer this : whose view is not in the window hierarchy
If you read the Apple Documentation for UIButton and look under the heading marked as Creating Button you'll find that the way to create a UIButton is to use buttonWithType: and not initWithFrame:.
Return Value
A newly created button.
Discussion
This method is a convenience constructor for creating button objects with specific configurations. If you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.
When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.
The only time when you should use alloc/init with a UIButton is when you have subclassed UIButton however your init method should then implement the buttonWithType: and pass in UIButtonTypeCustom.
So change:
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
To
// Create a button of type custom
checkbox = [UIButton buttoWithType:UIButtonTypeCustom];
// Now set the frame for your button
[checkbox setFrame:CGRectMake(73, 324, 15, 15)];
// Continue with the rest of your button code
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
Check userInteractionEnabled property of your background property.
But i see why you have no feedback on button if rest is fine - you use UIControlStateSelected for setting image when button has touch, but you must use UIControlStateHighlighted

iOS click on dynamic UIButton crashes

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");
}

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{
}

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