I have 5 UIImageView in a UIView, and I have named the UIImageView as follows:
#property (retain, nonatomic) IBOutlet UIImageView *imgPic1;
#property (retain, nonatomic) IBOutlet UIImageView *imgPic2;
#property (retain, nonatomic) IBOutlet UIImageView *imgPic3;
#property (retain, nonatomic) IBOutlet UIImageView *imgPic4;
#property (retain, nonatomic) IBOutlet UIImageView *imgPic5;
Let's say all UIImageViews are filled with an images, and the user can delete any UIImageView in runtime, I want to know how can I achieve the following scenario?
I want an intelligent loop so if the user delete imgPic3, the imgPic4 image will be moved to imgPic3, and imgPic5 will move to imgPic4, and imgPic5 assign to nil
imgPic3.img = imgPic4.img;
imgPic4.img = imgPic5.img;
imgPic5.img = nil;
I can hard code the 5 logical scenarios for each UIImageView if got deleted, but I want to do it in an intelligent way so it will work even if I have 100 UIImageViews.
Use an NSMutableArray to contain the 5 UIImageView pointers. Then when image view x is removed, do:
for (int i = imgViewNumber; i < [myImageViewArray count]-1; i++) {
[[myImageViewArray objectAtIndex:i] setImage:[[myImageViewArray objectAtIndex:i+1] image]];
}
[[myImageViewArray lastObject] setImage:nil];
Edit:
To add your UIImageViews to the array:
Don't have each one defined as a property nor an iVar - only have a retained property for the array.
Use a for (int i = 0; i < numImageViews; i++) loop to add all of your image views, and at the same time as calling addSubview:, call [myImageViewArray addObject:imageView].
Then if you want to set the image for a particular image view, call [[myImageViewArray objectAtIndex:imageViewNumber] setImage:image] (bear in mind that for image view 1, imageViewNumber should be 0)
Hope this is now clear
Related
# of Images are placed in a View using Storyboard.
I want to manipulate them programmatically using a For loop. But couldn't find how to?
Currently i am using the following code:
#property (weak, nonatomic) IBOutlet UIImageView *img_level2;
#property (weak, nonatomic) IBOutlet UIImageView *img_level3;
#property (weak, nonatomic) IBOutlet UIImageView *img_level4;
#property (weak, nonatomic) IBOutlet UIImageView *img_level5;
#property (weak, nonatomic) IBOutlet UIImageView *img_level6;
#property (weak, nonatomic) IBOutlet UIImageView *img_level7;
...
...
...
_img_level2.image = [_img_level2.image convertToGrayscale];
_img_level3.image = [_img_level3.image convertToGrayscale];
_img_level4.image = [_img_level4.image convertToGrayscale];
_img_level5.image = [_img_level5.image convertToGrayscale];
_img_level6.image = [_img_level6.image convertToGrayscale];
_img_level7.image = [_img_level7.image convertToGrayscale];
Now instead of writing them line by line i want to use a for loop. Because this procedure might be called multiple times for sizing and positioning.
Please advice, thanks!
Instead of having each of the buttons as its own UIButton property, you can define an outlet collection, which is essentially an array that you'll be able to treat exactly as you want (for example, loop over all of its items).
How do you do that? Simple:
In your code, replace all of the IBOutlet UIImageView... lines with:
IBOutletCollection(UIImageView) NSMutableArray *imgLevels;
On InterfaceBuilder, give each of your UIImageViews a different tag that will match its index in the IBOutletCollection.
And, finally, when you connect the objects on your .xib or storyboard file, you'll notice it will appear on the Referencing Outlet Collections section instead of the Referencing Outlets as you're used to.
Now you have an array that you can use to loop over your IBOutlets exactly as you would loop a "regular" NSArray. Good Luck!
I have an array of UIButton objects (I have 15 buttons so I wanted to store them in an array for easy processing). I want to be able to iterate through the array and change the background image/image that the button uses. This is how I currently have it set up:
#property (nonatomic, weak) UIImage *greenLight;
#property (nonatomic, weak) UIImage *yellowLight;
#property (nonatomic, weak) UIImage *redLight;
#property (weak, nonatomic) IBOutlet UIButton *task1Button;
#property (weak, nonatomic) IBOutlet UIButton *task2Button;
#property (weak, nonatomic) IBOutlet UIButton *task3Button;
#property (nonatomic, copy) NSMutableArray *buttonArray;
- (instancetype)init
{
self = [super init];
if(self){
[self.buttonArray addObject:self.task1Button];
[self.buttonArray addObject:self.task2Button];
[self.buttonArray addObject:self.task3Button];
self.greenLight = [UIImage imageNamed:#"greenlight.ICO"];
self.yellowLight = [UIImage imageNamed:#"yellowlight.ICO"];
self.redLight = [UIImage imageNamed:#"redlight.ICO"];
NSLog(#"init complete...");
}
return self;
}
Then I have the following in viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.task1Button setImage:self.redLight forState:UIControlStateNormal];
[(UIButton *)self.buttonArray[1] setImage:self.greenLight forState:UIControlStateNormal];
}
The second line in viewWillAppear works and changes the button's image. The third line, however, does not. The debugger shows the code reaching that line and not throwing any errors, so I don't know why setting the button image when the button is in an array would not work.
Any ideas?
Try replacing last line in your answer with this :
[(UIButton *)[self.buttonArray objectAtIndex:1] setImage:self.greenLight forState:UIControlStateNormal];
But i'm doing this without compiler so i'm not really sure about where to place the first bracket [
Tell me if it works, i'm not totally sure about it.
I am trying to multiply the integer values in three different text fields.
#property (nonatomic,strong) IBOutlet UITextField *textField;
#property (nonatomic,strong) IBOutlet UITextField *textField2;
#property (nonatomic,strong) IBOutlet UITextField *textField3;
#property (nonatomic,strong) IBOutlet UILabel *total;
I am trying to display the product in a UILabel. Can somebody please help with the implementation? Thanks, so much!
to display the product in the UILabel, set the label text this way (assuming the textFields all contain ints):
total.text = [NSString stringWithFormat: #"%i", (textField.text.intValue * textField2.text.intValue * textField3.text.intValue)];
you might want to put this in a separate method so you can call it from anywhere, like whenever the user updates the textFields with new numbers
I am having trouble testing equality on the Views that are set up in my project.
This is a long post, please bear with me. I have 3 separate views set up as part of the same XIB.
Each view is connected to File Owner (UIViewController) via an IBOutlet
The owner itself is a UIViewController
#interface PuzzleViewController : UIViewController {
Inside the FileOwner views are declared as:
IBOutlet Puzzle1 *p1;
IBOutlet Puzzle2 *p2;
IBOutlet Puzzle3 *p3;
#property (nonatomic, retain) IBOutlet Puzzle1 *p1;
#property (nonatomic, retain) IBOutlet Puzzle2 *p2;
#property (nonatomic, retain) IBOutlet Puzzle3 *p3;
Additionally i have
UIView *currentPuzzleView;
#property (nonatomic, retain) UIView *currentPuzzleView;
In .m file, upon initialization, i declare current view to be p1
[self setCurrentPuzzleView:p1];
Later, i would like to check if current view is in fact p1. This comparison fails.
if ([[self currentPuzzleView] isEqual:p1]) {
Additionally this fails as well
if ([self currentPuzzleView] == p1) {
Why is this?
Because you are using different classes for the equality test (UIView and Puzzle1). Your currentPuzzleView should be a pointer to one of those three classes Puzzle1 Puzzle2 Puzzle3. If you definitely need to use three different classes for those puzzle views, try adding tags to them and change
#property (nonatomic, retain) UIView *currentPuzzleView;
to
int currentPuzzleViewTag;
set it to 1 upon viewDidLoad: and change the tag to appropriate when different views are selected.
It's just a hunch but perhaps p1 is nil 'upon initialization'. It really depends on where you set currentPuzzleView to p1. It could be that the outlet points to nothing (yet).
In my app, I have a Settings view, where the user can use UISlider to set value from 50 to 1000. The selected value is then shown in a UILabel.
The problem is that the label does not display at all. The Action and the IBOutlets are connected in IB. The NSLog displays the value correctly on the console.
I can't figure out what is going on....help is needed :)
I have the following:
IBOutlet UILabel *sliderLabel;
IBOutlet UISlider *slider;
...
#property (nonatomic, retain) UILabel *sliderLabel;
#property (nonatomic, retain) UISlider *slider;
-(IBAction) sliderValueChanged:(id) sender;
and
#synthesize sliderlabel, slider;
- (IBAction) sliderValueChanged:(UISlider *) sender {
NSString *sliderValue = [NSString stringWithFormat:#"%d feet",(int)[sender value]];
NSLog(#"%#", sliderValue);
[[self sliderLabel] setText: sliderValue];
}
Thanks, and regards.