How to manipulate IBOutlet UIImageView objects in series using For Loop - ios7

# 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!

Related

Copy Class From Another Project and Removing the Connections in XCode

I have copied the contents of SignView.h and SignView.m from an existing project.
However, the connections were included in the following attributes:
#property (weak, nonatomic) IBOutlet UILabel *signHereLabel;
#property (weak, nonatomic) IBOutlet UIButton *clearBtn;
#property (weak, nonatomic) IBOutlet UIButton *doneBtn;
#property (weak, nonatomic) IBOutlet UITextField *textfield;
#property (weak, nonatomic) IBOutlet SignatureView *signView;
#property(strong, nonatomic)IBOutlet UIView *contentView;
- (IBAction)clearBtnAction:(id)sender;
- (IBAction)doneBtnAction:(id)sender;
- (IBAction)backBtnAction:(id)sender;
- (IBAction)gestureAction:(id)sender;
Every time I check the UIView in the connection, it shows that it is connected to the other project xib file where the UIView class came from (which is weird).
If I remove the connections, it affects the other project instead of the current project I'm working on.
I'm pretty sure I created the file, copied the contents only and not the entire file, so the file is not a reference (supposed to be), but a real file.
Question
So, how do I remove the connection inside the contents of my copied SignView.h file. And how do I connect SignView.h in my current project properly?
Maybe the file you added are referenced from the source try to copy them when you add them to your project by :-
Edit
Another thing you can do is change the name of your IBActions and IBOutlets like nameLabel to nameLabelMyVc(according to your requirement) and then remove the reference from storyboard or nib whatever you're using and connect the fresh outlets that you've renamed.

Changing label's data using textField inout

I want to change the label's text (which is embedded in a tableViewCell) using a textField data (which is embedded outside the tableViewCell).
Can anyone share the code please using objective-C.
Here is my TableViewCell.h file.
#import <UIKit/UIKit.h>
#interface TableViewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *label1;
#property (strong, nonatomic) IBOutlet UILabel *label2;
#end
Here is my storyboard controller file
#import <UIKit/UIKit.h>
#interface ViewController:UIViewController<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *contacts;
}
- (IBAction)leftBtn:(id)sender;
- (IBAction)rightBtn:(id)sender;
- (IBAction)nextBtn:(id)sender;
- (IBAction)SendBtn:(id)sender;
#property (strong, nonatomic) IBOutlet UITableView *tabV1;
#property (strong, nonatomic) IBOutlet UITextField *txtField;
#end
Here I have the button action connection where I should I write the actual code to change the label's data using a text field.
- (IBAction)leftBtn:(id)sender {
}

Multiplication of Integers in Textfield - Xcode 4.4

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

Access many UIImageView in a for loop

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

Testing equality of UIViews, clarification needed

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).