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];
}
}
Related
I have a method that add button on top-left of my collection view cell, and longPress recognizer. The problem is, when i set longPressRecognizer minimumPressDuration to something like 0.0001, i cant tap a button, because, instead of tapping button activates longPressRecognizer method. Please take a look:
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[[RACObserve(self, shouldEdit) deliverOnMainThread] subscribeNext:^(id x) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
if (self.shouldEdit){
self.layout.longPressGestureRecognizer.minimumPressDuration = 0.1;
int ind = indexPath.row;
NSLog(#"1 blk called");
button = [UIButton buttonWithType:UIButtonTypeContactAdd];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTag:indexPath.row];
[cell addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(cell.mas_left).with.offset(0);
make.top.equalTo(cell.mas_top).with.offset(0);
make.width.height.equalTo(#(20));
}];
}
I use reactive cocoa and masonry, but this actually doesn't matter, what i want is exclude button area from area, that i can use for longGestureRecognizer.
self.layout.longPressGestureRecognizer.minimumPressDuration = 0.1;
you can use self.myview.longPressGestureRecognizer.minimumPressDuration = 0.1; and in myview don't place button in myview.by this you can able to tap button
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...
}
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.
}
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
Hey I am writing a function that handles favorites within my app, as well as changing the image of the favorite button I want to change the image of a corresponding button in another UIScrollView DoubleScrollLeft.
The below code works HOWEVER I get the following eror when trying to favorite my first button with the tag '0' is there a reason for this? (the rest work).
-[UIScrollView setBackgroundImage:forState:]: unrecognized selector sent to instance 0x14e3b0
Also once the images do change it pushes the title of the button off to the right, do i need to reset the frame etc.. when i change the background for state?
-(void)favButtons:(id)sender {
int i = [sender tag];
NSString *fav = [NSString stringWithFormat:#"%i", i];
if ([[Favinsults objectForKey:fav] isEqualToString:#"0"]){
[sender setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
UIButton* button = (UIButton *)[DoubleScrollLeft viewWithTag:i];
[button setBackgroundImage:[UIImage imageNamed:#"buttonD1.png"] forState:UIControlStateNormal];
} else {
[sender setImage:[UIImage imageNamed:#"favButton0.png"] forState:UIControlStateNormal];
UIButton* button = (UIButton *)[DoubleScrollLeft viewWithTag:i];
[button setBackgroundImage:[UIImage imageNamed:#"buttonD0.png"] forState:UIControlStateNormal];
}
}
There seems to be a mis-connection where a UIScrollview is connected to the IBAction method. Check your IB connections carefully.
Your sender is somehow UIScrollview.
Change this line as, This should avoid the warning message.
[sender setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
[((UIButton *)sender) setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
You have to cast the sender to a UIButton before you can call any of the UIButton methods on it...
UIButton * newbutton = (UIButton*)sender;
[newbutton setImage:[UIImage imageNamed:#"favButton1.png"] forState:UIControlStateNormal];
etc.
[UIScrollView setBackgroundImage:forState:]
means that your sender is interpreted to be of type UIScrollView (not UIButton), and there is no method setBackgroundImage:forState: defined in UIScrollView.