Why send addTarget Method have the : to work - ios

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

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

Creating a UIButton in a subclass of UIButton

I am trying to create a UIButton in a class that is a subclass of UIButton. The reason for this is that it would be easier for my app to do this. I am wondering if it is possible. I am able to create the button, but the action is not working. My selector exists, but is not getting called.
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, kThumbSide - 350, kThumbSide, 16)];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button2 setTitle:[NSString stringWithFormat:#"%#", [data objectForKey:#"username"]] forState:UIControlStateNormal];
[button2 sizeToFit];
button2.backgroundColor = [UIColor clearColor];
button2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button2.font = [UIFont fontWithName:#"Arial" size:15];
[button2 addTarget:delegate action:#selector(showUserViews) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button2];
Hey you are creating an Object of UIButton class not any Class clear???
and your target method are getting wrong Delegate.
try it
[button2 addTarget:self action:#selector(showUserViews) forControlEvents:UIControlEventTouchUpInside];
replace delegate with self for getting showUserViews method in same class
I haven't see any UIButton subclass here, perhaps you've asked your question wrong.
Any way you should do this:
- (void) viewDidLoad {
[super viewDidLoad]
UIButton *b = [[UIButton alloc]initWithFrame:YOUR_DESIRED_FRAME];
[b setBackgroundColor:[UIColor redColor]]
[b addTarget:self action:#selector(bPressed:) forControlEvents:UIControlEventTouchUpInside]
}
- (void)bPressed:(UIButton *)sender {
NSLog(#"b button pressed")
}

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

ACTION for UIBUTTON in iOS

I have one custom class with subclass of UIView, inside this I created one button dynamically.
Now I want to set Action for button method. I set normal like UIviewController.
But its not working.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *but=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:but];
// Initialization code
}
return self;
}
-(void)aMethod
{
NextEyeViewController *nexteye=[[NextEyeViewController alloc]initWithNibName:#"NextEyeViewController" bundle:nil];
[self presentViewController:nexteye animated:YES completion:nil];
}
-(void)aMethod is my method name for button
If you want to set an action to your button created programmatically you need to do like this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 160.0, 40.0);
[view addSubview:button];
Create UIButton:
UIButton *yourButton = [[UIButton alloc] initWithFrame:frameButton];
// Here code to set button properties: color, label...
[yourButton addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:yourButton];
Your method has to receive the sender of the action:
-(void)aMethod:(id)sender {
// your code
}
Update:
You also have to call your method suitably with #selector(aMethod:) instead of #selector(aMethod).

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

Resources