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
Related
Confession: I have been through every possible link I could find, but unfortunately none work for me.
I am creating buttons dynamically. When a button is selected, it should change the color of the selected button and the rest should remain the default color. This way, the user can identify which button is selected.
Suppose there are 3 buttons: all blue in color, and when I select the first one, it should change to white and other two should remain blue. When I select the second, the first one should go back to blue and the second should now be white.
btnCounts data I am fetching from server and would vary.
-(void)createButtons{
for (int i=0; i<btnCounts; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTag:i];
[btn setFrame:CGRectMake(xVal, 0, width/btnCounts, 40)];
// [btn setImage:[UIImage imageNamed:#"btn_image.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"blue.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"white.png"] forState:UIControlStateSelected];
[btn setTitle:name forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrV addSubview:btn];
xVal += self.view.frame.size.width/btnCounts+1;
}
}
Now, in btnClicked: method I am passing the button as a parameter, so that I can use selected button.
-(void)btnClicked:(UIButton *)button
{
int tag = (int)button.tag;
//...
}
Please help me to find what I am missing here.
Thanks.
Create an instance UIButton variable to house the selected button so that you can deselect it when the next button is tapped. If you only pass the currently-tapped button to your btnClicked: method, you won't know which button to deselect (if any).
#implementation ClassName {
UIButton *previousButton;
}
...
- (void)btnClicked:(UIButton *)button
{
if(previousButton)
[previousButton setBackgroundColor:[UIColor blueColor]];
previousButton = button;
[button setBackgroundColor:[UIColor whiteColor]];
// do whatever else you need to do here
}
For that you need to store selected button in temporary object.
Create button in interface(.h) file:
UIButton *selectedButton;
Than add following line in -(void)createButtons method before for loop.
selectedButton = nil;
for() {...}
Here selectedButton is set to nil because initially none of the buttons are selected.
And finally replace your -(void)btnClicked:(UIButton *)button method with following method.
-(void)btnClicked:(UIButton *)button {
if(selectedButton) {
selectedButton.selected = NO;
}
button.selected = YES;
selectedButton = button;
}
If you want to make any button initially selected than assign that button to selectedButton like in btnClicked method.
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];
}
}
So I have two gray UIButton, button1 and button2. I want to change the color of the button when I press it to red and back to gray when I press the other button.
I used this code:
UIButton *btn = (UIButton *)sender;
[btn setBackgroundColor:[UIColor redColor]];
But it makes both buttons red.
How do I make it so that if button1 is red, it will change back to gray when I press button2?
Create two buttons btn1 & btn2 .
#property (weak, nonatomic) IBOutlet UIButton *btn1;
#property (weak, nonatomic) IBOutlet UIButton *btn2;
- (IBAction)btnClick:(id)sender;
Both btn1 & btn2 should be connected to btnClick . Below code will do the rest of your work
- (IBAction)btnClick:(id)sender {
if (self.btn1 == sender) {
[self.btn1 setBackgroundColor:[UIColor redColor]];
[self.btn2 setBackgroundColor:[UIColor grayColor]];
}else{
[self.btn1 setBackgroundColor:[UIColor grayColor]];
[self.btn2 setBackgroundColor:[UIColor redColor]];
}
}
You are going to want to make two separate buttons. By the look of your code you only declared only one button.
Declare both buttons in your interface, initialize them in the ViewDidLoad() method.
Set 2 different targets for each of the buttons so when one is clicked you can set the other buttons colour.
What I am getting from this is that both buttons are connected to the same action/method, which contains the code you gave. The easiest way to do what you specified is to have two different actions/methods, and connect each button to a different method. Connect button1 to button1Pressed and connect button2 to button2Pressed.
In .h put:
{
//connect these buttons up either via interface builder or through code
IBOutlet UIButton* button1;
IBOutlet UIButton* button2;
}
- (IBAction)button1Pressed:(id)sender;
- (IBAction)button2Pressed:(id)sender;
In .m put:
//connect this method to "touch up inside" on button1
- (IBAction)button1Pressed:(id)sender
{
//makes button1 red
[button1 setBackgroundColor:[UIColor redColor]];
}
//connect this method to "touch up inside" on button2
- (IBAction)button2Pressed:(id)sender
{
//changes button1 back to gray
[button1 setBackgroundColor:[UIColor grayColor]];
}
Hope this helps!
There is many way to solve such issue, in your case we do not really know how you are instantiating your UIButton. The following purpose you one of the possible solution using tag:
First you should define your button in your header:
#implementation _4644518ViewController {
UIButton *btn1;
UIButton *btn2;
}
Then, you should implement them in your view and attach to them the tapped method:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
btn1 = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
[btn1 setTag:101];
[btn1 setTitle:#"btn1" forState:UIControlStateNormal];
btn1.backgroundColor = [UIColor lightGrayColor];
[btn1 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
btn2 = [[UIButton alloc] initWithFrame:CGRectMake(5, 40, 100, 30)];
[btn2 setTag:102];
[btn2 setTitle:#"btn2" forState:UIControlStateNormal];
btn2.backgroundColor = [UIColor lightGrayColor];
[btn2 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
}
The last thing to do is to implement your btnTapped method:
- (void)btnTapped:(id)sender
{
if ([sender tag] == 101) {
btn1.backgroundColor = [UIColor redColor];
btn2.backgroundColor = [UIColor lightGrayColor];
} else if([sender tag] == 102) {
btn1.backgroundColor = [UIColor lightGrayColor];
btn2.backgroundColor = [UIColor redColor];
}
}
As I mentioned earlier, there is many way to perform such algorithm, but I like this one since we don't know many things about your background application and this one will be easy to custom for you.
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;
Currently I have 14 buttons being created programmatically using a for loop, code below:
int buttonCount = 14;
for (int i=0; i< buttonCount; i++) {
//Create titleString from array object
NSString *stringFromInt = [NSString stringWithFormat:#"%#",[arrayForRound objectAtIndex:i]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(buttonSelected:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:stringFromInt forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"helvetica" size:19];
button.tag = i;
[self.view addSubview:button];
}
This works great to create the buttons, I can then populate the Answer Box with the value of the selected button:
-(void)buttonSelected: (UIButton *)sender
{
[_buttonOne setTitle:sender.titleLabel.text forState:UIControlStateNormal];
}
However after the button has been populated I would like to remove it from the screen. If i call button.hidden it simply hides the last button that was programmatically created. I am aware of button.tag and have tried to use this, but it feels like I almost need to do something like:
//Hide button for tag (i know this is incorrect syntax)
button for buttonTag: 3 setHidden;
Is there something similar or a way of doing this?
Update
The button that I am trying to hide is the one that was created programatically. So I want _buttonOne to take on the title of the create button (lets call that letterButton), and then hide letterButton from the view,
UIButton *yourBtn = (UIButton *)[self.button viewWithTag:3];
[yourBtn setHidden:YES];
(code posted by Oh Seung Kwon)
This code work perfectly but it hides the wrong set of buttons. (Hides _buttonOne and not letterButton).
I wonder if it would not be better to create the 12 buttons in nib and name them manually...There will never be more or less that 12.
When your button is tapped, you can set the hidden property on the action method's sender argument, which is the button that actually got tapped. This will hide the button which was tapped.
- (void)buttonSelected:(UIButton *)sender {
[_buttonOne setTitle:sender.titleLabel.text forState:UIControlStateNormal];
[sender setHidden:YES];
}
If you meant to retrieve the button with the tag of 3, you can use this code instead:
[[self.view viewWithTag:3] setHidden:YES];
I don't recommend that you use the tag property - you should use Interface Builder and an IBOutletCollection instead.
Like this
UIButton *yourBtn = (UIButton *)[self.view viewWithTag:3];
[yourBtn setHidden:YES];
You could get the view by tag use this message.
[self.view viewWithTag:3];
We always specific tag by macro just like
#define kFirstButtonTag (100)
or use
#define kButtonBeginTag (100)
Then use the macro to get tag.
And in a special number - Case 0, 1 or 2 is always use, begin your tag in a special number could avoid some problems