Setting the value of a UITextField in an IBAction - iphone

I'm trying to use a UITextField as an output for a simple encryption. The field starts out with a nil string (its default behavior), and I try to update it in the encryptButtonPressed: action. I know I'm getting a good NSString into ciphertext, and I'm getting no errors or warnings or anything that the method doesn't exist, but the field remains blank. Any ideas?
Here's the header:
#interface FirstViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *affineKeyField;
IBOutlet UITextField *shiftKeyField;
IBOutlet UITextField *messageField;
IBOutlet UITextField *ciphertextField;
MAAffineMachine* machine;
}
#property (nonatomic, retain) UITextField *affineKeyField;
#property (nonatomic, retain) UITextField *shiftKeyField;
#property (nonatomic, retain) UITextField *messageField;
#property (nonatomic, retain) UITextField *ciphertextField;
#property (nonatomic, retain) UIButton *encryptButton;
#property (nonatomic, retain) UIButton *clipboardButton;
- (IBAction)encryptButtonPressed:(id)sender;
- (IBAction)clipboardButtonPressed:(id)sender;
#end
And the action, defined in the controller's implementation:
- (IBAction)encryptButtonPressed:(id)sender {
NSLog(#"Encrypt Button pushed.\n");
int affine = [[affineKeyField text] intValue];
int shift = [[shiftKeyField text] intValue];
NSString* message = [messageField text];
NSLog(#"%d, %d, %#", affine, shift, message);
NSString* ciphertext = [machine encryptedStringFromString:message
ForAffine:affine
Shift:shift];
NSLog(#"%#\n", ciphertext);
[[self ciphertextField] setText: ciphertext];
}

If ciphertext has a proper value, you should check that ciphertextField is linked properly in the interface builder.

Try [[self ciphertextField] setText: ciphertext] -> [ciphertextField setText: ciphertext] or cipherTextField.text = ciphertext;

Related

Property not found on ViewController

I am beginning in iOS development. I've been searching a solution for days and I can't get it. I got the following code:
#interface SAProfileViewController : UITableViewController {
#public NSNumber *userId;
}
#property (weak, nonatomic) IBOutlet UIView *awesomeProfileHeader;
#property (nonatomic, assign) NSNumber *userId;
#end
As you can see I am declaring my property, and after that I do this :
#implementation SAProfileViewController
#synthesize userId = _userId;
...
On an other ViewController I import SAProfileViewController.
#import "SAProfileViewController.h"
...
-(void)goToUserProfile :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
SAProfileViewController *controller = [[SAProfileViewController alloc] init];
controller.userId = gesture.view.tag; // << HERE THE ERROR APPEARS
[self.navigationController pushViewController:controller animated:YES];
}
That's it, this is my code and I get the following error:
"Property 'userId' not found on object of type 'SAProfileViewController *'
Thanks in advance.
You have done a mistake with the property type.
Change
#property (nonatomic, assign) NSNumber *userId;
to
#property (nonatomic, retain) NSNumber *userId;
Than you have to create a NSNumber instance of gesture.view.tag like this
controller.userId = [NSNumber numberWithInteger:gesture.view.tag];
OR you can change your userId to NSInteger instead of NSNumber*
#property (nonatomic, assign) NSInteger userId;
(you have to do this for the object attribute userId, too)

Retaining value from textbox in an NSString

Here's my .h file:
The IBOutlet I am having problems with is the textbox. I would like to retain the value in the textbox as an NSString:
#interface UploaderViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
IBOutlet UIImageView *imageView;
IBOutlet UITextField *textCaption;
NSString *caption;
}
- (IBAction)pushPick;
- (IBAction)pushUpload;
#property (nonatomic, retain) IBOutlet UITextField *textCaption;
#end
Where the value is stored in this NSString:
- (IBAction)pushUpload {
caption = textCaption.text;
}
I think you meant to do something like this:
- (IBAction)pushUpload {
[caption release];
caption = [textCaption.text copy];
}
In shouldChangeTextInRange call your pushUpload.

UIPickerView not returning the correct data

I have a Main view with a landscapeview and a portraitview that gets loaded as subviews of the wrapper view. Both the landscape view and portraitview has UIPickerView objects that are linked to them. In my source I have them all as properties and I also have a wrapper for the landscape and portrait pickers. They are defined as
#property (nonatomic, retain, readonly) IBOutlet UIPickerView *pickerTown;
#property (nonatomic, retain, readonly) IBOutlet UIPickerView *pickerType;
#property (nonatomic, retain, readonly) IBOutlet UIDatePicker *pickerFrom;
#property (nonatomic, retain, readonly) IBOutlet UIDatePicker *pickerTo;
#property (nonatomic, retain) IBOutlet UIPickerView *landscapePickerTown;
#property (nonatomic, retain) IBOutlet UIPickerView *landscapePickerType;
#property (nonatomic, retain) IBOutlet UIDatePicker *landscapePickerFrom;
#property (nonatomic, retain) IBOutlet UIDatePicker *landscapePickerTo;
#property (nonatomic, retain) IBOutlet UIPickerView *portraitPickerTown;
#property (nonatomic, retain) IBOutlet UIPickerView *portraitPickerType;
#property (nonatomic, retain) IBOutlet UIDatePicker *portraitPickerFrom;
#property (nonatomic, retain) IBOutlet UIDatePicker *portraitPickerTo;
And for each of the readonly wrappers I have
- (UIPickerView *) pickerTown
{
if(self.landscapeView.superview)
{
return landscapePickerTown;
}else
{
return portraitPickerTown;
}
}
But when I am in portrait or landscape, and I try for instance to say
myHelper.selectedTown = [myHelper.towns objectAtIndex:[pickerTown selectedRowInComponent:0]];
I don't get the selected value but just the first value in the array.
The data gets added as follows:
if(pickerView == landscapePickerType || pickerView == portraitPickerType) {
return [myHelper.types objectAtIndex:row];
}else if(pickerView == landscapePickerTown || pickerView == portraitPickerTown){
return [myHelper.towns objectAtIndex:row];
}else
{
return #"";
}
and the array:
myHelper = [Helper sharedManager];
[myHelper.types addObject:#"None"];
[myHelper.types addObject:#"Food & Wine"];
[myHelper.types addObject:#"Test"];
[myHelper.towns addObject:#"None"];
[myHelper.towns addObject:#"Vanderbijlpark"];
[myHelper.towns addObject:#"Test"];
What am I doing wrong here?
If you are getting only the first element, it is indicative that [pickerTown selectedRowInComponent:0] is evaluating to zero always which will happen only if pickerTown is nil. That again is most likely due to outlet not being connected.
But in this case I think you are accessing the instance variable directly and not the value the method returns. If you are using #synthesize for pickerTown, make that #dynamic. And change the statement to [self.pickerTown selectedRowInComponent:0] to access the value of the getter.

transferring floats to a different class?

I have a problem that I have been searching for a solution for but can't seem to find one that works. UserInput.xib , Calculations.h & .m ,DataOutput.xib ... there is no .xib for that calculations.
UserInput.h
DataOutput *dataOutput;
UITextField *tf1;
UITextField *tf2;
UITextField *tf3;
#property (nonatomic, retain) DataOutput *dataOutput;
#property (nonatomic, retain) IBOutlet UITextField *tf1;
#property (nonatomic, retain) IBOutlet UITextField *tf2;
#property (nonatomic, retain) IBOutlet UITextField *tf3;
UserInput.m
#synthesize dataOutput;
#synthesize tf1;
#synthesize tf2;
#synthesize tf3;
- (IBAction)calculate:(id)sender {
DataOutput *dataOut = [[DataOutput alloc] initWithNibName:#"DataOutput" bundle:nil];
Calculations *calc = [[Calculations alloc] init];
dataOut.dataCalc = calc;
dataOut.dataCalc.tf1 = tf1.text;
dataOut.dataCalc.tf2 = tf2.text;
dataOut.dataCalc.tf3 = tf3.text;
dataOut.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:dataOut animated:YES];
[dataOut release];
DataOutput.h
Calculations *dataCalc;
UILabel *results1;
UILabel *results2;
UILabel *results3;
#property (nonatomic, retain) Calculations *dataCalc;
#property (nonatomic, retain) IBOutlet UILabel *results1;
#property (nonatomic, retain) IBOutlet UILabel *results2;
#property (nonatomic, retain) IBOutlet UILabel *results3;
DataOutput.m
#synthesize results1;
#synthesize results2;
#synthesize results3;
- (void)viewDidLoad {
self.results1.text = dataCalc.tf1;
self.results2.text = dataCalc.tf2;
self.results3.text = dataCalc.tf3;
Calculations.h
NSString *tf1;
NSString *tf2;
NSString *tf3;
NSString *results1;
NSString *results2;
NSString *results3;
float *tempResults1;
float *tempResults2;
float *tempResults3;
#property (nonatomic, retain) NSString *tf1;
#property (nonatomic, retain) NSString *tf2;
#property (nonatomic, retain) NSString *tf3;
#property (nonatomic, retain) NSString *results1;
#property (nonatomic, retain) NSString *results2;
#property (nonatomic, retain) NSString *results3;
- (float)getResults1;
- (float)getResults2;
Calculations.m
#synthesize tf1;
#synthesize tf2;
#synthesize tf3;
#synthesize results1;
#synthesize results2;
#synthesize results3;
- (float) getResults1 {
float temp1 = [tf1 floatValue];
float temp2 = [tf2 floatValue];
if (temp1 <= 1 && temp2 >= 3) {
if (temp2 ==10) {tempResults1 = 50;}
else if (temp2 == 11) {tempResults1 = 52;}
etc etc etc ok here is my problem..with the way I have things setup I can carry data from the userInput, threw Calculations and display them in a label on the DataOutput. BUT, when
using the floats in those if statements on calculations.m that I declared in calculations.h... I can't carry the floats (the actual data calculations) over to the DataOutput screen. on DataOutput when I try to set it a float from calculations it doesn't recognize it, it will recognize the NSString but not the float. I have tried converting float tempResults1 to NSString results1 and i keep getting errors. I have tried several different ways to go about doing this from different questions and answers on here but can't figure out why it wont work. can anyone help me with this?
What I want to do is to be able to display results from the calculations on the dataoutput screen.
I know it has to be something simple, maybe I'm doing something wrong or overlooking looking something I didn't do ... I don't know, I could use a little guidance though I know that much.
if (temp1 <= 1 && temp2 >= 3) // this is ok
{
if (temp2 ==10) { // exact floating point comparisons are dangerous,
// and should be avoided. you must rewrite this.
// turn up your compiler warnings
tempResults1 = 50; // this is not what you think it is.
// turn up your compiler warnings. you
// are assigning the address for the
// pointer's value. this will lead to a
// crash after you use it. what exactly
// are you trying to accomplish with this
// statement? this must be rewritten.
}
else if (temp2 == 11) {tempResults1 = 52;} // as above

how to generate an event

I created a subclass from UIView
#import <UIKit/UIKit.h>
#interface MeeSelectDropDownView : UIView {
UILabel *mainText;
UIImage *bgImg;
UIImageView *bgView;
UIImageView *originView;
NSMutableArray *labelArray;
int selectedItem;
BOOL inSelectTag;
float _defaultHeight;
}
#property (nonatomic , retain) UIImage *bgImg;
#property (nonatomic , retain) UIImageView *bgView;
#property (nonatomic , retain) NSMutableArray *labelArray;
#property (nonatomic , retain) UIImageView *originView;
#property (nonatomic , retain) UILabel *mainText;
#property (nonatomic , readonly) int selectedItem;
- (void) setViewHeight:(float)aheight;
-(void) showDropList;
-(void) hiddenDropList;
-(void) setStringByArray:(NSArray*)array;
-(void)hiddenLabels
{
for(UILabel *aLabel in labelArray){
[aLabel removeFromSuperview];
}
}
Is it possible to generate an Event from function 'hiddenLabels' to inform and do somethings
Thanks
interdev
It is totally unclear what you are trying to do.
Suggestion: you can consider either posting an NSNotification, or registering observers using KVO (Key Value Observing).