How to call a method on UIButton in NSArray? - objective-c

I'm trying to enable a button but the button that I would enable in this function changes. I have an array of the buttons but when I use the .enabled on the array index I want it says that this doesn't work for IDs.
I have used this array to set the text of each button before using:
[[ButtonArray objectAtIndex: Index] setTitle:(#"blahblahblah") forState: UIControlStateNormal];
is there any way to use a similar function call to enable and disable?

If you know for sure that everything in that array is a UIButton you can cast it to make the compiler happy:
[(UIButton *)[ButtonArray objectAtIndex: Index] setTitle:(#"blahblahblah") forState: UIControlStateNormal];

It's OK to split code up into multiple lines. It makes it easier to read, debug, and maintain.
UIButton *button = ButtonArray[Index]; // new, modern array syntax
button.enabled = YES;
[button setTitle:#"blah" forState:UIControlStateNormal];

Related

Easiest Way to Combine Two Statements for UIButton

I'm trying to condense my code, and I know there's an easier way than writing out two separate lines, but I can't seem to get the syntax just right. What I'm essentially doing is assigning an image to my UIButton, passing it to a method to automatically resize it, and then setting the title to NULL. Any ideas?
My code :
[self.btnA setBackgroundColor:[UIColor colorWithPatternImage:[self resizeImageWithImage:[UIImage
imageNamed:#"A_3rdStroke.png"] toSize:CGSizeMake(100, 100)]]];
[self.btnA setTitle:#"" forState:UIControlStateNormal];
maybe:
- (void)setButton:(UIButton *)button withImageName:(NSString *)imageName {
[button setBackgroundColor:[UIColor colorWithPatternImage:[self resizeImageWithImage:[UIImage imageNamed:imageName] toSize:CGSizeMake(100, 100)]]];
[button setTitle:#"" forState:UIControlStateNormal];
}
and you can call it from your class like this:
[self setButton:self.btnA withImageName:#"A_3rdStroke.png"];
optionally, you could put such method into a category for the UIButton class (which needs to include your -resizeImageWithImage:toSize: method as well).

Can't change the caption of a UIButton from its own Touch Up Inside event

I just noticed this crazy behavior in UIButton:
If I try to set the caption of a UIButton from an IBAction that gets fired on the button's touch up inside event, the caption changes, but is quickly reverted to the old value.
If I do it in some other button's touch up inside event, it works as expected.
What monkey business is going on???
- (IBAction)removeText:(id)sender {
[[sender titleLabel] setText:#"New Text"];
}
titleLabel is a read-only property. You want to use:
[sender setTitle:#"title" forState:UIControlStateNormal];
Edit: Actually, looking it up the titleLabel's own properties are still accessible, but nevertheless the setTitle solution is the way to go....
Correct way would be :
- (IBAction)removeText:(id)sender {
[(UIButton*)sender setTitle:#"New Text" forState: UIControlStateNormal];
}

How to move an image with a button?

I have an image(clubcow.png) that is passed from an nsarray string to make a picture. Then facedowncow represents the picture. I was wondering how to make a button that will move facedowncow to position (100,100) when button is tapped. Any tips will be appreciated. Also, there is more to this code, I just posted the important parts to give an idea on what is going on.
cardKeys = [[NSArray alloc] initWithObjects:#"clubcow", nil];
currentName = [NSMutableString stringWithFormat:#"%#.png", [cowsShuffled objectAtIndex:currentcow]];
faceDowncow = (UIImageView *)[self.view viewWithTag:1];
faceDowncow.userInteractionEnabled = YES;
I would start by creating a UIButton and adding it to your view controller's view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 20);
[button setTitle:#"Tap Me" forState:UIControlStateNormal];
[button addTarget:self action:#selector(animateImage:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Then, link this button to a function that will animate your faceDowncow object. You could add your faceDowncow as a property of the view controller so the following function can easily reference it:
- (void)animateImage:(UIButton *)sender {
[UIView animateWithDuration:0.2
animations:^{
// change origin of frame
faceDowncow.frame = CGRectMake(100, 100, faceDowncow.frame.size.width, faceDowncow.frame.size.height);
} completion:^(BOOL finished){
// do something after animation
}];
}
First of all, it looks like this code is from a view controller subclass, and the cow is a subview. In that case, you should probably have a property for it rather than obtaining it by its tag all the time. If it's instantiated in a storyboard scene/nib then you can hook up an outlet to a property/ivar in your subclass fairly easily.
The easiest way to do what you want is to create the button and use target action so that when it is tapped, it calls a method in your view controller. In the method body, obtain a reference to your cow and set it's frame property, like so:
[faceDowncow setFrame: CGRectMake(100,100,faceDowncow.bounds.size.width,faceDowncow.bounds.size.height)];
If you don't know how target action works, I suggest reading Apple's documentation on the matter. It's as simple as getting a button, calling one method to tell it what events should make a certain method get called, and then implementing that method.

Xcode/ Printing dot

I am trying to print "." when I press on a button that shows "."
Basically I wanna grab "." on NSString format
So if I do
NSString *dec = [sender currentTitle];
it just crashes when I try to run.
In Cocoa, all controls send notifications that they have been operated, by using a target-action mechanism. The 'target' is any other object and the 'action' is any selector that that object responds to. Buttons are no different.
So you can, for example, define:
-(void)buttonClicked:(id)sender {
NSLog(#"Button was clicked!");
}
You'd hook that up to the button's target-action, by invoking -setTarget: and -setAction: accordingly. The target will be self, if you're doing this from inside the class that handles the action:
[button setTarget:self];
[button setAction:#selector(buttonClicked:)]
Now when the button is pressed, you'll get a NSLog() output in the console.
To update the value of a label instead of printing something with NSLog(), you can probably figure that out, but:
-(void)buttonClicked:(id)sender {
[label setText:#"."];
}
You should read Apple's documentation, which covers this stuff in great detail.
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW14
PS: stackoverflow is for all programming languages, so make sure to tag your questions with the relevant programming language.
Try this.
UIButton *resultButton = (UIButton *)sender;
NSString *dec = resultButton.currentTitle;
Try Something like this
NSString *strdec = [sender titleLabel].text;

Get UIButton from view programmatically

Im creating and adding a grid of buttons to my custom view keyboardView as follows:
int offset = 0;
for (int row = 0; row<4; row++){
for (int col = 0; col<13;col++) {
offset +=1;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(5+col*65+offset,5+row*65, 60, 60);
[aButton setTitle:myarray[row][col] forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[keyboardView addSubview: aButton];
}
}
I need certain buttons to be of different sizes, like the return key or space bar. How can i get a reference to a particular button programmatically, later on in the same method? Is there an easier way than setting the tag and then calling [keyboardView viewWithTag:t]? Becauseint's are going to get confusing.
Thanks.
You could make instance variables like UIButton *spaceBar. if you reach the Button in the two for-Iretations which is thought to be the spacebar just do spacebar = aButton.
So you can later in the method just use this instance Variable which refers to the specified button. ;-)
I hope it's more or less understandable. ^^
You can either do it with UIView tags (which don't have to get confusing, just create an enum), or if you have only a few "special" UIButtons, you can create ivars to keep references to them.