IBOutletCollection objects indexed wrong - ios

i have 10 uibuttons on story board and i've linked them with iboutletcollections. Then i set buttons backgroundimage from array of uiimage using loop. When i run my project button indexes differs from image indexes
Here is my code
-(void)setButtonImages
{
for (int i=0; i<imageArray.count; i++)
{
UIButton *b =[self.myButtons objectAtIndex:i];
[b setBackgroundImage:[imageArray objectAtIndex:i] forState:UIControlStateNormal];
}
}

create your buttons manually then tag them one by one and you can use your buttons with tag

Related

How can I achieve a 30 button grid using autolayout storyboards?

So I had this working for a 12 button (4x3) grid of buttons.
I'd like all of the buttons to be equal size, common distances above and to the side of each other, and the entire grid to be centered on the device, like so:
The problem is, it looks like a jumbled mess when I build the project.
I have no problem getting the segmented control, score, or reset buttons positioned correctly, but the grid just messes everything up.
I've been using the middle tool to set up the constraints on the grid, which worked fine for the 12 button grid:
However, using this only creates infinite conflicting constraints that cannot be resolved by xcode.
I am very new to iOS and could be missing something simple, but I've been trying my best to match up to the blue auto suggested lines as much as possible here.
Thanks for any advice.
It would be a lot simpler just to use a UICollectionView with a UICollectionViewFlowLayout and let the flow layout create the grid for you.
But even if you're not going to do that, then still, my advice is: don't set this up in Xcode / Interface Builder; make the entire grid (and constraints if you want them) in code. It's much simpler (and more fun and less boring and, of course, less error-prone).
1.) Instead of setting each button up in the interface builder just create the container (a UIView) that the whole grid should fit inside. Add constraints to that container for how you would want that to expand and contract with screen size.
2.) Link that container UIView to your .h view controller class and name it gridContainer or whatever.
3.) Create a property in your .h class:
#property (strong, nonatomic) NSMutableArray *twoDimensionalArrayContainingRowsOfCardButtons;
4.) Then:
- (void)viewDidLoad {
// other stuff you're doing to set up your app
self.twoDimensionalArrayContainingRowsOfCardButtons = [NSMutableArray new];
//Do this inside the main thread to make sure all your other views are laid out before this starts
//Sometimes when you do layout stuff before the rest of the view is set up from Interface Builder you will get weird results.
dispatch_async(dispatch_get_main_queue(), ^{
[self createTwoMentionalArrayHoldingCardButtons];
[self displayCardGrid];
});
}
- (void)createTwoMentionalArrayHoldingCardButtons {
NSMutableArray *arrayWithRowsOfButtons= [NSMutableArray new];
for (int x = 0; x < 6; x++) {
NSMutableArray *arrayOfButtonsAtRowX = [NSMutableArray new];
for (int i = 0; i < 6; i++) {
CGRect rect = self.gridContainer.bounds;
CGSize cellSize = CGSizeMake(rect.size.width / 6, rect.size.height / 6);
UIButton *buttonInColumnI = [UIButton alloc] initWithFrame:CGRectMake(cellSize.width * i, cellSize.height * x, cellSize.width, cellSize.height);
[buttonInColumnI setImage:[UIImage imageNamed:#"yourCardImage"] forState:UIControlStateNormal];
[buttonInColumnI addTarget:self action:#selector(yourButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[arrayOfButtonsAtRowX addObject:buttonInColumnI];
}
[arrayOfRowsOfButtons addObject:arrayOfButtonsAtRowX];
}
self.twoDimensionalArrayContainingRowsOfCardButtons = arrayWithRowsOfButtons;
}
- (void)displayCardGrid {
for (int x = 0; x < self.twoDimensionalArrayContainingRowsOfCardButtons.count; x++) {
NSMutableArray *arrayOfButtonsAtColumnsAtRowX = self.twoDimensionalArrayContainingRowsOfCardButtons[x];
for (int i = 0; i < arrayOfButtonsAtColumnsAtRowX.count; i++) {
UIButton *buttonAtColumnI = arrayOfButtonsAtColumnsAtRowX[i];
[self.gridContainer addSubview:buttonAtColumnI];
}
}
}
- (void)yourButtonAction:(UIButton *)tappedCard {
//To swap the card image on your tapped button
for (int x = 0; x < self.twoDimensionalArrayContainingRowsOfCardButtons.count; x++) {
NSMutableArray *arrayOfButtonsAtColumnsAtRowX = self.twoDimensionalArrayContainingRowsOfCardButtons[x];
for (int i = 0; i < arrayOfButtonsAtColumnsAtRowX.count; i++) {
UIButton *buttonAtColumnI = arrayOfButtonsAtColumnsAtRowX[i];
if (tappedCard == buttonAtColumnI) {
int row = x;
int column = i;
//Now you can save that the user has tapped something at this row and column.
//While you're here, you can update the card image.
[tappedCard setImage:[UIImage imageNamed:#"CardExposedImage"];
}
}
}
}
I'm writing this all in the box here without running it, so hopefully that works for you. Ended up being a few more lines than expected.
Edit: forgot to add that I separated the building of the card buttons and the displaying of them so that you could call the display method separately. With the property, you also have a retained source of all the cards so you can just fetch them out of the array and change what you need, as needed.

Create a few UIButton programmatically and reconstruct the object later using tag?

I am creating a few buttons programmatically, similar to:
for( int i = 0; i < 5; i++ ) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aView addSubview:aButton];
}
And that I can implement the function of the button this way so that I know witch one was tapped:
- (void)buttonClicked:(UIButton*)button
{
NSLog(#"Button %ld clicked.", (long int)[button tag]);
}
But my question is a little bit trickier than that, I don't want just to access the button inside it's button method but also outside of it so that I can change the UIButton frame using an animation.
I was thinking that I should somehow be able to recreate the pointer to any of the buttons previously created by using the tags that I assign initially.
Can anyone point me in the right direction?
The correct way to do this is by using the next line of code:
UIButton *aButtonReconstruct = (UIButton *)[self.view viewWithTag:aTag];
where aTag is an int and is greater than 0, because all the views have the tag 0 by default, so in the for loop used in the first place, the count should start at minimum 1. In our case aTag having values from 1 to 6 after we change the for loop.
Also there shouldn't be more view with the same tag.

Why can't I access to nested UIView from viewWithTag method using StoryBoard?

I created some UIImageViews inside a nested UIView by Storyboard and I created a different TAG for each UIImageView. This is how the tree of the ViewController looks according to Storyboard:
ViewController
View
ViewNested
UIImageView1
UIImageView1
UIImageView1
UIImageView1
I have to change programmatically these ImageViews so, to get the images I use the method viewWithTag but it doesn't work because it returns NIL.
This happens even if I add to my class the ViewNested IBOutlet and getting the views using the following code:
// View is the top View with Tag:40
UIView * view = [self.view viewWithTag:40]; //This works
// The nestedView with Tag:44
UIView * viewNested = [view viewWithTag:44]; //DOESN'T work it returns NIL even if the TAG is exact
Then if I try to access to the imageView using the same method of course, it returns NIL. I don't know why, I also tried to use this code to view all the recursive nested view but it seems that they don't exist even if they are present in the storyboard.
- (void)showAllSubView:(UIView *)view
{ int i = 0;
for (UIView * subView in [view subviews]) {
NSLog(#"%#, tag:%ld", subView, (long)subView.tag) ;
[self showAllSubView:subView] ;
i++;
}
NSLog(#"Numb of Views %d", i) ;
}
The TAGS are 40 for the root View, 44 for the nested and for the images are 1,2,3,4. So the TAGS are all different.
Any help will be appreciate :). Thanks in advance
UIImageView *imageView=(UIImageView *)[self.view viewWithTag:yourTag];
[imageView setImage:[UIImage imageNamed:#"yourImageName"]];
replace yourTag with UIImageView tag;
replace yourImageName with some Image name
and if you want change only images of ImageViews - you don't need
UIView * view = [self.view viewWithTag:40]; //This works
// The nestedView with Tag:44
UIView * viewNested = [view viewWithTag:44];
Also you can change all images:
for (int i=0; i<18; i++)
{
UIImageView *imageView=(UIImageView *)[self.view viewWithTag:i];
NSString *imageName = [NSString stringWithFormat:#"imageName_%d", i];
[imageView setImage:[UIImage imageNamed:imageName]];
}

Make changes to a number of buttons programatically

I have a view with 10 buttons and I want to use information from an array to set how many of the buttons are visible and what their title is.
The buttons are named option1BTN...option10BTN. The array has different data and size depending on what the user selects and I want to buttons to also reflect the changes.
The code below shows a for-do loop, that sets which button is visible and the button title
for (int i=0; i == [optionsArray count]; i++) {
self.option1BTN.hidden = NO;
[self.option1BTN setTitle:[optionsArray objectAtIndex:i] forState:UIControlStateNormal];
}
How can I change the button name (programatically) in the loop so that depending on the size of the array it changes to option1BTN then option2BTN...optionXBTN and so on?
NSArray *buttons = #[self.option1BTN, self.option2BTN]; // add all the buttons here
for (int i=0; i < buttons.count; i++) {
UIButton *button = buttons[i];
button.hidden = NO;
[button setTitle:[optionsArray objectAtIndex:i] forState:UIControlStateNormal];
// the previous line can be re-written as
//[button setTitle:optionsArray[i] forState:UIControlStateNormal];
}
The difference between this approach and using an IBOutletCollection (as Jeff suggests in his answer) is that the outlet collection does not guarantee the order of its items. If the order is important to you, you need to specify it yourself, like in my code snippet above.
You have two main options, one is to use an IBOutletCollection (if your buttons are created in Interface Builder). Each UIButton will be stored in the collection and you can retrieve the right button in the loop from the collection.
Example:
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *buttons;
In IB, you will need to link each button to the IBOutletCollection. Then in your code you will be able to do:
for (int i = 0; i < optionsArray.count; i++) {
UIButton *tempButton = self.buttons[i];
tempButton.hidden = NO;
[tempButton setTitle:[optionsArray objectAtIndex:i] forState:UIControlStateNormal];
tempButton.hidden = NO;
}
The ordering of an IBOutletCollection is not always guaranteed so you may need to use a combination of IBOutletCollection and view tagging (see end of post). For more information on IBOutletCollection's see NSHipster's article
Second option is if your buttons are created programmatically, store each button in an NSMutableArray as you create it:
#property (nonatomic, strong) NSMutableArray *buttons;
UIButton *myButton;
// Do something...
[self.buttons addObject:option1BTN];
Then use the same loop as before.
A third (not recommended option), would be to tag each UIButton and then you can use the viewWithTag: method to retrieve the button. E.g. using the above code again but getting the button with:
UIButton *tempButton = [self.view viewWithTag:i];

Check button backgroundColor in if statement

I'm trying to set if condition on the basis of button background color. I've found a solution on stackoverflow but it's not for me because what I'm doing different is my buttons are in an array and if condition is in a for loop. It's like checking all the buttons in the array every time a button is tapped and if any button have background color as black don't show the next interface.
This method is setting those color which is doing good.
for (int i=0; i<10; i++)
{
[buttonsArray[i] setColor:[UIColor blackColor]];
}
Now the problem is in this method:
-(void)myFunction
{
for (int i=0; i<10; i++)
{
if([buttonsArray[i] backgroundColor:[UIColor blackColor]])
{
//code I want to use if the condition is true
}}}
I've also tried the following conditions in if:
if([buttonsArray[i].backgroundColor isequal:[UIColor blackColor]])
//or
if([[buttons[i] background] isEqual:myBlack])
//or
if([buttonsArray[i] backgroundColor]==[UIColor blackColor])
Please not that I'm using setColor to set the color not setBackgroundColor and I want to keep it that way because this is a requirement.
You shouldn't be using the colour of the button, you should have another array which contains an NSNumber (or a custom class) that holds the state of each button. This will be easy to use and test. Trying to use the UI to hold your model state will always lead to issues like this.
Instead of checking button using color, go for Tags.
Use tags to set a specific numeric id for a button and you can use the same throughout the code. Like this,
myButton.tag=1;
myButton.tag=2;
OR
for (int i=0; i<10; i++)
{
[buttonsArray[i] setTag:i];
}
or you can set tag in interface builder properties of button.

Resources