I have a button that you can press and the text of that button will change. I want to be able to detect when the user clicks the button again, so that the button text will change back to its original text. How would I be able to do that? Here is the code I have so far.
//Set Text and alignment for the buttons
[nocavities setTitle:#"No cavities\n5 points" forState:UIControlStateNormal];
nocavities.titleLabel.textAlignment = NSTextAlignmentCenter;
-(IBAction)nocavitiesaction:(id)sender {
[sender setTitle:#"Whenever you visit our office cavity-free, you will receive 5 points!" forState:UIControlStateNormal];
}
I feel like in the IBAction I should change the state to
UIControlStateNormalPressed
but I'm not sure.
You can set a counter inside the IBAction
Something like this:
-(IBAction)nocavitiesaction:(id)sender {
static int count = 0;
count ++;
if(count % 2 != 0){
//Set title 1
}else{
//Set title 2
}
}
Open xib file and select the button.
Under the attributes inspector you will find "State Config", select "Default" from dropdown.
Change the title of button to "No cavities\n5 points"
Change State Config to "Selected"
Change title to "Whenever you visit our office cavity-free, you will receive 5 points!"
Now toggle these titles by.
- (IBAction)nocavitiesaction:(id)sender
{
UIButton * button = sender;
button.selected = !button.selected;
}
like this.
declare global int flag=1;
-(IBAction)methodName:(id)sender
{
if(flag==1)
{
//set title 1
flag=2;
}
else
{
//set title 2
flag=1
}
}
There's no UIControlStateNormalPressed state, but there is a UIControlStateSelected. You can set a title for both the selected and normal states, then manually set the UIButton to its selected state like so:
- (void)viewDidLoad {
[self.button setTitle:#"Whenever you visit our office cavity-free, you will receive 5 points!" forState:UIControlStateNormal];
[self.button setTitle:#"No cavities\n5 points" forState:UIControlStateSelected];
}
- (IBAction)nocavitiesaction:(UIButton*)sender {
sender.selected = !sender.selected;
}
Note: I've changed the parameter from id to UIButton to avoid having to convert id to UIButton to check its selected state. And the UIButton should be of type "Custom" if you'd like to prevent the background tint while selected.
Related
I have a UIButton with a different image for normal and selected state. Also I need to change the tint of the button depending on the app's theme.
But when I set the button to selected State to switch the image, it inverts its colours.
- (void)setLike:(BOOL)selected {
self.likeButton.selected = selected;
if (selected) {
self.likeButton.tintColor = [Theme getTintColor];
} else {
self.likeButton.tintColor = [Theme getLightColor];
}
}
Normal State
Actual Selected
Desired Selected
Note: I can't change the image because this code is used in another places in the app with different selected and unselected images.
- (void)setLike:(BOOL)selected {
if (selected) {
[self.likeButton setImage:[UIImage imageNamed:#"Liked"] forState:UIControlStateNormal];
self.likeButton.tintColor = [Theme getTintColor];
} else {
[self.likeButton setImage:[UIImage imageNamed:#"Like"] forState:UIControlStateNormal];
self.likeButton.tintColor = [Theme getLightBaseColor];
}
}
In Interface Builder find your button, and change it's Type from System to Custom. This should remove the effect of enlarging&reversing image for selected status.
Open your Assets file where both images for Normal and Selected statuses are defined. Set the Render As for Normal image to Original Image, and for Selected image to Template Image. This should result in applying your app's default tint only to selected status.
I would try to keep track of the state in a property and then have an additional properties for returning the correct image for the current state. You can then set these upon viewDidLoad or viewDidAppear.
I don't know your exact situation so here is an example.
Example:
#property (nonatomic) BOOL isLiked;
...
- (void)viewDidAppear {
// likeButton
[self.likeButton setBackgroundImage:[self imageForCurrentState] forState:UIControlStateNormal];
}
...
- (UIImage)imageForCurrentState {
if (isLiked) {
return [UIImage imageNamed:#"Liked"];
} else {
return [UIImage imageNamed:#"Like"];
}
}
I am creating an iOS app with a seating plan layout.
Trying to use an object-oriented approach, I created a class for TableLayoutObjects as they have different properties.
And to lay these TableLayoutObjects out on the screen I am representing them as UIButtons that are created as I loop through the array of TableLayoutObjects.
- (void) loadTables
{
for (TableLayoutObjects *layoutObjs in arrTableLayoutObjects)
{
if ([layoutObjs.shape isEqualToString:#"r"]) {
// rectangle
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
......
if(layoutObjs.isInteractable) {
[button addTarget:self action:#selector(tableTouchedDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(tableTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside];
}
} else {
// text only. use label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(layoutObjs.posX, layoutObjs.posY, layoutObjs.width, layoutObjs.height)];
......
}
}
}
My event handlers look like the below for now.
// reverts back to original color and perform other instructions
- (void) tableTouchedUpInside:(UIButton *) button
{
button.layer.backgroundColor = [UIColor colorWithWhite:0.2f alpha:.5f].CGColor;
}
My question is: how do I identify the UIButtons to their TableLayoutObjects? In the event handler after I change the colour of the button, I will also want to get or set some properties of the selected TableLayoutObjects. How can I do that?
I think your example is a perfect fit for implementing a UICollectionView. Solution with the buttons is less clean and more complex.
You can set the tag of the button to the index into the arrTableLayoutObjects array of the associated item.
Alternatively, create a custom class which takes the table as a parameter and is the target of the button. This object now has direct access to the button and the table item.
I have a UIButton that is able to change locations on the screen after it has been clicked. The user then clicks a different button to change the image of the button and I want it to stay in its new location. Currently with my code it is reverting back to the original position after changing the image. Here is the code:
First move the Button (newLocation is a location of a UIImageView):
movableButton.center = [newLocation center];
Then Change the button image by clicking a different button:
[movableButton setImage:[UIImage imageNamed: #"differentImage.png"] forState:UIControlStateNormal];
I dont understand why it moves. I just tested it, and it does move. I need to look into why it moves and will get back to you.
But this will fix the problem of it moving.
CGPoint currentLoc = self.imageButton.center;
[self.imageButton setImage:[UIImage imageNamed:#"face"] forState:UIControlStateNormal];
self.imageButton.center = currentLoc;
Try this
-(IBAction) movingButtonClicked
{
[movingButton setFrame:CGRectMake(movingButton.frame.origin.x+10,movingButton.frame.origin.y+20,50,50)];
}
-(IBAction) changeImage
{
[movingButton setImage:[UIImage imageNamed: #"differentImage.png"] forState:UIControlStateNormal];
}
Ok now i got your question , what you can do is start a counter in 2nd button's action:
-(void)secondButtonClick
{
static int counter = 1;
if(counter %2 == 0)
{
firstButton.center =cgpoint a; // a gain some point where you wan't to place the center
}
if(counter %2 != 0)
{
[firstButton setBackgroundImage:[UIImage imageNamed:#"logout.png"] forState:UIControlStateNormal];
}
counter ++;
}
you can modify if condition whether you want position changed before changing image..... Also uou can change the center alsolike this
a.x =a.x+10;
a.y =a.y +10;
myButton.center = a;
How can I change the image of the UIButton three times and also get the state or set a value that later i can use as my reference.
For example, The initial image of UIButton is white when first click it will turn to yellow then third clicked it will be green then if its green and clicked again it will return to white. i also need to set a value from different states.
And this will be applied to 10 individual 'UIButton'.
Scenario, UIButton will be used to display the state of the watches, initial value is white which mean -new when clicked it will turn to yellow which mean used and when clicked again it will turn to green which mean first class watch.
just a sample scenario only.
you could do something like this:
-(void) buttonPressed: (id) sender {
if([sender isKindOfClass:[UIButton class]]){
UIButton *button = (UIButton*)sender;
if(button.backgroundColor == [UIColor White])
button.backgroundColor = [UIColor Yellow];
else if(button.backgroundColor == [UIColor Yellow])
button.backgroundColor = [UIColor Green];
else if(button.backgroundColor == [UIColor Green])
button.backgroundColor = [UIColor White];
}
}
havnt tested that at all, but something along those lines should work. treat this as pseudo code
I have two buttons in each row of a tableview. One is labeled "have it" the other "want it" Each button starts off at 20% opacity when the app starts. When one button is tapped the opacity is set to 100% . I need logic so that if one button is set to 100% opacity and the other one set at 20% is tapped, the first button needs to be set to 20% and the second button to 100% (so the opacity needs to be reversed).
Each button has it's own action that is run when pressed. I can access the button that is pressed and set the opacity with (UIButton *senderButton = (UIButton *)sender). However I need to set the opacity of the other button as well. How can access the other button (the one that was not pressed) inside of my action/function that is called when one is pressed? Thanks!
You can create an outlet for each button. So that you can set its property from any where within its container class.
if I correct understand your question, you can declare your buttons in header-file like this:
#interface myController : UIViewController
{
UIButton *b1;
UIButton *b2;
}
tmen in m-file (in viewDidLoad) you can set this buttons with one selector and different tags: (for more information about creation buttons: How do I create a basic UIButton programmatically?)
-(void)viewDidLoad
{
[super viewDidLoad];
b1 = [UIButton buttonwithType:UIButtonTypeCustom];
[b1 addTarget:self withAction:#selector(clickINMyButtons:) forState:UIControlTouchUPInside]; // sorry, I don't remember correct syntax, i'll correct this some later if you needed in it.
b1.tag = 1;
b1.frame = CGRectMake(0,0,12,12); //example
[self.view addSubView:b1];
}
alike declare b2 with different:
b2.tag = 2;
So, then you implement your selector with changing opacity:
-(void)clickINMyButtons:(UIButton *)sender
{
if (sender.tag == 1)
{
sender.alpha = 1; // or b1.alpha = 1;
b2.alpha = 0.2;
}
else if (sender.tag == 2)
{
sender.alpha = 1; // or b2.alpha = 1;
b1.alpha = 0.2;
}
}