Multiplication of Integers in Textfield - Xcode 4.4 - objective-c

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

Related

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 {
}

How to manipulate IBOutlet UIImageView objects in series using For Loop

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

How to add a UIView to an UIAlertView?

I have to create a custom AlertView with a view that contain some label. I create the .h and .m of CustomAlert
this is the CustomAlert.h.
#import <Foundation/Foundation.h>
#interface CustomAlert: UIView
#property (nonatomic, weak) IBOutlet UILabel *ok;
#property (nonatomic, weak) IBOutlet UILabel *ok1;
#property (nonatomic, weak) IBOutlet UILabel *ok2;
#property (nonatomic, weak) IBOutlet UILabel *ok3;
#property (nonatomic, weak) IBOutlet UILabel *ok4;
#property (nonatomic, weak) IBOutlet UILabel *ok5;
#end
and this is the CustomAlert.m.
#import "CustomAlert.h"
#implementation CustomAlert
#synthesize ok = _ok;
#synthesize ok1 = _ok1;
#synthesize ok2 = _ok2;
#synthesize ok3 = _ok3;
#synthesize ok4 = _ok4;
#synthesize ok5 = _ok5;;
#end
I also create the xib with just a view and i connect the label with all the "ok".
Now, i want to add this View to an AlertView with just an Ok button. How can i do it? I want to use it with ios 7 and 6.
Thanks!!
In iOS 7 you should not munge a UIAlertView like this - and there is no need, because thanks to custom view transitions you can make your own small presented view in front of your interface, so that it looks and acts like a UIAlertView but can contain anything you like.
I've written an online tutorial on how to do this:
http://programming.oreilly.com/2014/01/transcending-uialertview-on-ios-7.html
And there's a downloadable github project that goes with it:
https://github.com/mattneub/custom-alert-view-iOS7

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

specifying textField using UITextField delegate

Hi i'm new to software for iphone/ipad apps.
I am using the textField delegate method:
"(BOOL)textFieldShouldReturn:(UITextField *)textField".
I have gotten to the point where when the user enter a name and presses 'enter' and a label displays what the user just wrote. This is just two lines of code like so:
labelChange.text = textField.text; //labelChange is my label in IB
return YES;
Since i have several textfields in IB, this code works for all of them. I'm not sure how to be specific with the code and have this label change for only ONE of my textFields.
My .h files looks like this.
#interface FirstViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UILabel *labelChange;
IBOutlet UITextField *userName;
IBOutlet UITextField *homeValue;
IBOutlet UITextField *downPayment;
IBOutlet UITextField *textField;
}
#property (nonatomic,retain) UILabel *labelChange;
#property (nonatomic,retain) UITextField *userName;
#property (nonatomic,retain) UITextField *homeValue;
#property (nonatomic,retain) UITextField *downPayment;
#property (nonatomic,retain) UITextField *textField;
#end
I want my label to change only when user types in the text field labeled "userName".I'm not sure how to do this, am I missing something?In IB, I connected all my textfield delegates to 'Files Owner'. Any advice would be really helpful. thanks!
Assign each of your text fields a tag number i.e.
textField1.tag = 1
textField2.tag = 2
etc...
Then in your -(BOOL)textFieldShouldReturn:(UITextField *)textField you can do a:
switch (textField.tag) {
case 1:
labelChange1.text = textField.text;
break;
case 2:
labelChange2.text = textField.text;
break;
etc... etc...
}