Seeing the UIButton in another void - iOS - ios

I have set a UIButton like this :
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
and I have
[learnmorebutton addTarget:self action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside];
and i have a void
- (void)myAction
{
}
but i cannot see learnmorebutton in the void, and thats because #property.
is this correct ?

All UIControls pass themselves as a "sender" to their target action methods IF the selector you give them takes 1 parameter.
Make these changes:(note the colon after the myAction selector on the first line)
[learnmorebutton addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
- (void)myAction:(UIButton *)sender
{
//sender is the button that was tapped
sender.backgroundColor = [UIColor redColor]; //or whatever color you wanted here.
}

Related

How to remove UIButton in iOS7

I want to create and remove uibutton. (iOS7)
My viewDidLoad function creates uibutton in the following way:
for (char a = 'A'; a <= 'Z'; a++)
{
position = position+10;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(getByCharacter:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"%c", a] forState:UIControlStateNormal];
button.frame = CGRectMake(position, self.view.frame.size.height-40, 10.0, 10.0);
button.tintColor = [UIColor whiteColor];
[self.view addSubview:button];
}
Now I want to remove specific uibutton based on button text.
- (IBAction)getByCharacter:(id)sender{
}
I don't know how to do this. Can anyone provide some sample code?
You can remove the button by calling
[sender removeFromSuperview];
in your callback (getByCharacter:). When you attach the selector to the button, the sender parameter will be the button which triggered the event. That means that you have an instance of it. With this instance you can now calll the removeFromSuperview method.
First you need to get the button's title text and then remove it from superView if it matches to the specific character,
- (IBAction)getByCharacter:(id)sender{
UIButton *myButton = (UIButton *) sender;
if([myButton.titleLabel.text isEqualToString:#"C"]{
[myButton removeFromSuperView];
}
}

Change properties of programmatically created UIButton in a different IBAction

I have one method that creates a number of UIButton instances programmatically and positions them in the view. Another IBAction needs to change the background colour of one of these UIButton when fired however when I try to action it, it tells me that the Property not found on object of type UIView *
The button is created using:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[view addSubview:button];
When I try to access it from the other IBAction like:
button.backgroundColor = [UIColor blackColor];
It fails with the error noted above.
Any help is much appreciated. I saw another answer which suggested to create an array as an IBOutlet and access it that way but I'm wondering if there's another way.
Thanks!
declare this button in globally and identify each button in tag, just like or assume this
UIButton * button, *button2; // declare in your view controller.h
- (IBAction)butaction:(id)sender; //this is your color change method;
in your view controller.m wherever u add this button
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=10; //this is important
[self.view addSubview:button];
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.tag=20; //this is important
[self.view addSubview:button1];
//here ur color change method
- (IBAction) butaction:(UIButton*)sender {
switch (sender.tag)
{
case 10:
[button setBackgroundColor:[UIColor blackColor]];
break;
case 20:
[button1 setBackgroundColor:[UIColor blackColor]];
break;
default:
break;
}
}
Try this, it may be helpful
for (int index=1; index<=5; index++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(ButtonClickedFunction:) forControlEvents:UIControlEventTouchUpInside];
button.tag= index;
[view addSubview:button];
};
implement this in IBAction
-(IBAction)ButtonClickedFunction:(UIButton *)button{
button.backgroundColor = [UIColor blackColor];
}
Note: In this I have created 5 buttons but not set frame. Please set frame according to your requirements.
Thanks

Setting selectors for global UIButton

I want to return a global UIButton from a global class.
+(UIBarButtonItem *) globalButton {
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0.0, 0.0, 40, 40);
[myButton addTarget:self action:#selector(???)
forControlEvents:UIControlEventTouchUpInside];
return myButton;
}
So if I call [Global globalButton] from my viewController, I can get this button. The problem I'm having is how to set the selector, if I'm calling if from my view controller?
You should change your code as follow, assuming you would be calling it from UIViewController
+(UIButton *) globalButton:(UIViewController*)delegate {
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0.0, 0.0, 40, 40);
if([delegate respondsToSelector:#selector(gloBalButtonTapped:)])
[myButton addTarget:delegate action:#selector(gloBalButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
return myButton;
}
//Below method should be present in your view controller than only it will be added to myButton click event.
-(IBAction)gloBalButtonTapped:(id)sender{
}
You should return UIButton only if you are creating UIButton, if you need to return UIBarButtonItem than you should create UIBarButtonItem or Xcode will give warning, just a suggestion though.
Just dont add target in Global Class wherever you want to use it just use it like this.
UIBarButtonItem *btn = [Global globalButton];
btn addTarget:self action:#selector(yourSelector)
forControlEvents:UIControlEventTouchUpInside];
Or you can make globalButton a property in GlobalClass and write this method in setGlobalButton and then you can use it as a property in any viewController.
Or if you want to control it in Global only then you can mention your selector mentioned in this class only. And in that method you can control the behaviour.
Try this in your controller:
UIBarButtonItem* button = [UIBarButtonItem globalButton];
[button addTarget:self action:#selector(yourDidTapAction:) forControlEvents:UIControlEventTouchUpInside];
or this in globalButton:
+(UIBarButtonItem*)globalButtonWithTarget:(id)target andAction:(SEL)action
{
// ...
[myButton addTarget:target action:action
forControlEvents:UIControlEventTouchUpInside];
// ...
}
I found the easiest way to do this is below:
+(UIBarButtonItem *) buttonWithSelector:(SEL)selector andSender:(id)sender {
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(-40.0, 10.0, 40, 40);
[myButton addTarget:sender action:selector forControlEvents:UIControlEventTouchUpInside];
return myButton;
}

Passing selector to a custom method to create UIButton programmatically

I am using XCode 5 under ios 7 to do a simple project. And I get the following error when I press a button that is created programmatically:
2013-11-10 09:16:02.969 Project2[446:70b] +[KNSunDynamicUIButton buttonSavePressed:]: unrecognized selector sent to class 0x221424
Details:
I create a custom method that is used to create a UIButton object programmatically. That custom method is used in any view controller. That custom method needs passed-in parameters like x, y, width, height, button title, and selector that is an event handler and passed in like "(SEL)selector".
Custom method (belongs to a helper class):
+(UIButton*)kNSunSetupWithSelector:(SEL)selector withX:(int)x y:(int)y width:(int)width height:(int)height
buttonTitle:(NSString*)buttonTitle backGroundColor:(CGColorRef) backGroundColor
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Save" forState:UIControlStateNormal];
button.frame = CGRectMake(x, y, width, height);
button.backgroundColor = [UIColor greenColor];
// selector is used over here
[button addTarget:self
action:selector
forControlEvents:UIControlEventTouchDown];
return button;
}
And then in a view controller .m file, I call that custom method like:
-(void)setUpButtons
{
SEL selector = #selector(buttonSavePressed:);
_buttonSave = [KNSunDynamicUIButton kNSunSetupWithSelector:selector withX:470 y:410 width:160 height:40 buttonTitle:#"Save" backGroundColor:(_buttonSaveColor)];
[self.view addSubview:_buttonSave];
}
#interface MyViewController ()
{
...
// buttons
UIButton* _buttonSave;
}
#end
And the definition of button's event handler in the same view controller .m file like:
- (void)buttonSavePressed:(UIButton*)button
{
NSLog(#"Button Save clicked.");
}
When I run my code and hit over the button, I see the exception as mentioned above. Please help thank you.
P.S. If I rewrite the custom method as an alternative that does not have "(SEL)selector" parameter in its signature, and let controller view, who calls that custom method, does the job of coding selector, then there is no exception:
-(void)setUpButtons
{
//Note: the codes are in my view controller .m file
_buttonSave = [KNSunDynamicUIButton kNSunSetupWithX:470 y:410 width:160 height:40 buttonTitle:#"Save" backGroundColor:_buttonSaveColor];
// Note: selector coding is taken care by codes of my view controller instead of by custom method
[_buttonSave addTarget:self
action:#selector(buttonSavePressed:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:_buttonSave];
[_buttonSave setupView];
}
And the alternative custom method (I do not like the method because it does not take care of dynamic passed-in selector):
+(UIButton*)kNSunSetupWithX:(int)x y:(int)y width:(int)width height:(int)height
buttonTitle:(NSString*)buttonTitle backGroundColor:(CGColorRef) backGroundColor
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Save" forState:UIControlStateNormal];
button.frame = CGRectMake(x, y, width, height);
button.backgroundColor = [UIColor greenColor];
return button;
}
The problem is line in helper class:
[button addTarget:self action:selector forControlEvents:UIControlEventTouchDown];
Here you said that the helper class will have method you're passing through selector, in you're case buttonSavePressed: method.
You should add inController parameter in you're helper method to tell in which controller is selector method placed.
In your particular case, the helper method should looks something like this (Note that inController parameter is added right after selector parameter):
+(UIButton*)kNSunSetupWithSelector:(SEL)selector
inController:(UIViewController*)controller
withX:(int)x
y:(int)y
width:(int)width
height:(int)height
buttonTitle:(NSString*)buttonTitle
backGroundColor:(CGColorRef) backGroundColor
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Save" forState:UIControlStateNormal];
button.frame = CGRectMake(x, y, width, height);
button.backgroundColor = [UIColor greenColor];
// selector is used over here
[button addTarget:controller
action:selector
forControlEvents:UIControlEventTouchDown];
return button;
}
and you should call it, from your controller this way:
-(void) setupButtons
{
SEL selector = #selector(buttonSavePressed:);
_buttonSaveColor = [UIColor orangeColor].CGColor;
_buttonSave = [KNSunDynamicUIButton kNSunSetupWithSelector:selector
inController:self
withX:470
y:410
width:160
height:40
buttonTitle:#"Save"
backGroundColor:(_buttonSaveColor)];
[self.view addSubview:_buttonSave];
}

iPhone , Convert #Selector call whith IBAction

I have this code:
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *imgIcon = imageNamed(#"icon_1.png");
[btn1 setImage:imgIcon forState:UIControlStateNormal];
CGPointMake(textField.frame.size.width+textField.frame.origin.x+btnTalk.frame.size.width/2, textField.frame.origin.y+textField.frame.size.height/2);
[btnTalk addTarget:textField action:#selector(Hello:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTalk];
i can tap in UIbutton and it works
but i want to call with other button
i want create one IBAction for call this UIButton
i writes this code but doesn't work & textfield is (objectOfAnotherClass)
-(IBAction)call1
{
[textField Hello];
}
I think you should do like this:
[btnTalk addTarget:self action:#selector(Hello:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)call1
{
[self Hello:nil];
}
I think that you are looking for sendActionsForControlEvents that helps you to 'simulate' the action of touching the button... Here the method description.

Resources