Why isEqual: does not work for a property UIButton? - ios

I have got a problem:
I have set up a property:
#property (strong, nonatomic) UIButton *buttonX;
Also I have a method to create a button:
-(void)createChooseButton : (UIButton *) button
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 50, 50);
button.layer.borderWidth = 1;
button.layer.borderColor = [[UIColor greenColor] CGColor];
if ([button isEqual:self.buttonX]) {
NSLog(#"Hello");
[button setTitle:#"X" forState:UIControlStateNormal];
button.center = CGPointMake(self.view.frame.size.width/3, self.view.frame.size.height*2/3);
}
[self.view addSubview:button];
}
When I call this method from viewDidLoad, it does not work, but if I don't use If statement (with isEqual) everything is ok.
Please could you help me, what am I doing wrong?

isEqual will not work here, because you have two pointer here one point to button & second to buttonX. if you need to check the button , give a tag property to buttonX in didload & then check it in createChooseButton method.

When you pass thebuttonX in first place it's nil.
You have toalloc it and then call the method.
you should call
_buttonX = [UIButton buttonWithType:UIButtonTypeRoundedRect];
and then call
[createChooseButton:self.buttonX]
don't call buttonWithType:UIButtonTypeRoundedRect in the method

Related

Create a function that is able to check the title of the button pressed

I have a loop for creating buttons dynamically.
for (int b = 0; b < _currentPlayerTeams.count; b++)
{
HNTeamObject *team = _currentPlayerTeams[b];
CGFloat buttonY = 168 + ((b + 1) * distanceBetweenButtons) - 23;
NSString *buttonTitle = [NSString stringWithFormat:#"%# %#",team.teamName, team.seriesName];
UIButton *button = [[UIButton alloc] init];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"images.png"] forState:UIControlStateNormal];
button.titleLabel.textColor = [UIColor blackColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[button addTarget:self
action:#selector(chooseTeamOne:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle: buttonTitle forState:UIControlStateNormal];
button.titleLabel.text = (#"%#", buttonTitle);
button.frame = CGRectMake(labelAndButtonX, buttonY, 268.0, 46.0);
[self.view addSubview:button];
}
I need to make a function that can act as the selector that for each button that is created and look at the button's title. Since I am making the buttons in the loop they are local and do not appear in another function that I create. Any help would be appreciated. I apologize in advance because I am very new to coding and am not quite up to speed with what is going on. Thanks.
Each button will call the chooseTeamOne: function when it is pressed so I assume you want to get the buttons title in that method.
To do so, use the UIButton's title property:
NSLog(#"%#", button.currentTitle);
This will log the button's current title. What is important to note is that I am referencing "button" which is the instance of the UIButton which called the chooseTeamOne method. I am assuming chooseTeamOne takes a UIButton button as a parameter.
If your method takes an 'id' as a parameter you would need to cast the sent object to a UIButton like this:
- (IBAction)chooseTeamOne:(id)sender {
UIButton *button = (UIButton *)sender;
NSString *buttonTitle = button.currentTitle;
}
Do you need a separate selector to return the title? If not then..
I imagine that your chooseTeamOne function is something along the lines of:
-(IBAction)chooseTeamOne:(id)sender {
...do things...
}
in which case, just add
-(IBAction)chooseTeamOne:(id)sender {
UIButton *button = sender;
NSString *stringThatYouWant = button.titleLabel.text; //get the text on the button
...do things...
}

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

Cannot hide programmatically created UIButton

This issue is baffling me for a few days now.
I have 2 UIButtons, one created using the Storyboard and one created programmatically.
When I press one of the buttons, I am moved to a method that hides both the programatically built and storyboard buttons, but only the storyboard button disappears, whereas the programmatically built button remains. I have tried not only hiding but also changing the frame of the programmatically built button, but to no avail.
Here is the code used:
#property (strong, nonatomic) UIButton *catBut;
#property (weak, nonatomic) IBOutlet UIButton *whenButton;
....
....
-(void)viewDidLoad {
....
[self customizeButton:self.catBut withFrame:CGRectMake(165, 113, 135, 50)
andAction:#selector(selectedCat) andColor:[UIColor belizeHoleColor]
andTag:2 andImage:#"coffee-64.png" andText:#"Cafes" andAlignmentLeft:YES];
....
}
- (void)customizeButton:(UIButton *)but withFrame:(CGRect)rect andAction:(SEL)action
andColor:(UIColor *)col andTag:(NSUInteger)tag
andImage:(NSString *)imageName
andText:(NSString *)buttonText
andAlignmentLeft:(BOOL)alignLeft {
but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setFrame:rect];
[but setTitle:buttonText forState:UIControlStateNormal];
but.tag = tag;
but.titleLabel.numberOfLines = 0;
but.titleLabel.font = [UIFont boldFlatFontOfSize:18];
[but setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
CGFloat spacing = 10; // the amount of spacing to appear between image and title
but.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
but.imageEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
but.titleEdgeInsets = UIEdgeInsetsMake(0, spacing*2, 0, 0);
[but setTitleColor:[UIColor cloudsColor] forState:UIControlStateNormal];
[but setTitleColor:[UIColor cloudsColor] forState:UIControlStateHighlighted];
[but addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[self.buttonsView addSubview:but];
}
....
- (void)selectedCat {
self.catBut.hidden = YES;
self.whenButton.hidden = YES;
}
Why is only the Storyboard button compliant to UI changes? Is there something I am missing? I have tried cancelling the AutoLayout but that didn't change a thing.
The issue is with the passing for argument in your method (customizeButton:(UIButton *)but).
Change your code to this and check
but = [UIButton buttonWithType:UIButtonTypeCustom];
to
self.catBut = [UIButton buttonWithType:UIButtonTypeCustom];
......
[self.buttonsView addSubview:self.catBut];
OR You should pass the address customizeButton:(UIButton **)but in the method
and while calling use &self.catBut
It is BOOL type, not an bool type.
try this
self.catBut.hidden = YES;
self.whenButton.hidden = YES;
I can see this property catBut, But I cann't see creation for this, as well I didn't give Outlet too. Check this. You've tried something but simply fix by following line.
[self.buttonsView addSubview:but];
self.catBut = but;

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

Resources