Property not found on ViewController - iphone

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)

Related

Custom Annotation with a button. didSelectAnnotaionView does not seem to get called

I am having a problem with my didSelectAnnotationView getting called when I use a custom annotation view. I was hoping someone might be able to take a look and help me with what I am missing.
I actually want a method to get called when the user touches the button show in the screenshot of my custom annotation which can be seen below. I was trying to make that the calloutAccessoryView, but I am doing something wrong. Please if anyone has done custom annotation views that a user can click on please see if you can help!! This is driving me crazy. I think I have posted the relevant info here, if not ask and I will edit my post.
I am adding a custom annotation like this:
LocationObject * obj = [m_MyObject.marLocations objectAtIndex:page];
obj.title =#"A";
[mvMapView addAnnotation:obj];
My Location object used above is defined like this:
// Location Object
#interface LocationObject : NSObject <MKAnnotation>
#property (nonatomic, retain) NSString * practicename;
#property (nonatomic, retain) NSString * address1;
#property (nonatomic, retain) NSString * mapaddress1;
#property (nonatomic, retain) NSString * address2;
#property (nonatomic, retain) NSString * city;
#property (nonatomic, retain) NSString * state;
#property (nonatomic, retain) NSString * zip;
#property (nonatomic, retain) NSString * phone;
#property (nonatomic, retain) NSString * fax;
#property (nonatomic, retain) NSString * email;
#property (nonatomic, retain) NSString * longitude;
#property (nonatomic, retain) NSString * latitude;
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#end // End Location Object
My viewForAnnotation does this:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
[map.userLocation setTitle:#"I am here"];
return nil;
}
static NSString* AnnotationIdentifier = #"annotationViewID";
ViewDirectionsAnnotationView * annotationView = [[ViewDirectionsAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
int page = floor((svOfficesScrollView.contentOffset.x - svOfficesScrollView.frame.size.width / 2) / svOfficesScrollView.frame.size.width) + 1;
LocationObject * obj = [m_DentistObject.marLocations objectAtIndex:page];
double dLatitude = [obj.latitude doubleValue];
double dLongitude = [obj.longitude doubleValue];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(dLatitude, dLongitude);
((LocationObject*)annotation).coordinate = coordinate;
annotationView.lbPracticeName.text = obj.practicename;
annotationView.lbStreet.text = obj.address1;
NSString * sCityStateZip = [NSString stringWithFormat:#"%#, %# %#", obj.city, obj.state, obj.zip];
annotationView.lbCityStateZip.text = sCityStateZip;
annotationView.lbPhoneNumber.text = obj.phone;
annotationView.canShowCallout = YES;
annotationView.annotation = annotation;
annotationView.rightCalloutAccessoryView = (UIButton *)[annotationView viewWithTag:1]; //[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
And my Annotation View is like this:
#interface ViewDirectionsAnnotationView : MKAnnotationView
{
CLLocationCoordinate2D coordinate;
NSString * title;
NSString * subtitle;
}
#property (nonatomic, retain) IBOutlet UIView * loadedView;
#property (weak, nonatomic) IBOutlet UILabel * lbPracticeName;
#property (weak, nonatomic) IBOutlet UILabel * lbStreet;
#property (weak, nonatomic) IBOutlet UILabel * lbCityStateZip;
#property (weak, nonatomic) IBOutlet UILabel * lbPhoneNumber;
#end
And the xib for the above view looks like this:
But, my didSelectAnnotationView never gets called. Can anybody explain what I am missing?
Please, any help is appreciated this has been driving me nuts. I could have it completely wrong, but the custom view does appear so I think I am close.
Thanks for the help!!
UPDATE:
I forgot to mention about the delegate. I have checked the delegate many times and believe I have it right. In the ViewController .h file where the mapView is, I have the and I set [mapView setDelegate:self] In that same .m file is where I have implemented the viewForAnnotation method and didSelectAnnotationView. The viewForAnnotation gets called but not the other.
One more interesting piece of information is that if I click (touch) just below my custom annotation then didSelectAnnotationView does get called. I guess now I am more confused!!
UPDATE #2:
My xib is loaded this way:
In my viewForAnnotation I call initWithAnnotation. That method is as follows:
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString*)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil)
{
[[NSBundle mainBundle] loadNibNamed:#"DirectionsAnnotation" owner:self options:nil];
if (loadedView)
{
[loadedView setFrame:CGRectMake(-(loadedView.frame.size.width/2), -(loadedView.frame.size.height), loadedView.frame.size.width, loadedView.frame.size.height)];
[self addSubview:loadedView];
}
}
return self;
}
The view which is displaying your MapView needs to be set as your delegate.
Where is your didSelectAnnotationView? Make sure that it implements the delegate protocol. E.g.
#interface MyView : UIViewController <MKMapViewDelegate>
...
#end
Then somewhere else...
[mapView setDelegate:myView];

unable to set values taken from text fields from a UIViewController to another class for calculation

I am having problems unable to set values taken from UItextfields from a Viewcontroller to another class which does the calculations from those values.
#interface InitialViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *phValue;
#property (weak, nonatomic) IBOutlet UITextField *pco2Value;
#property (weak, nonatomic) IBOutlet UITextField *hco3Value;
#property (weak, nonatomic) IBOutlet UITextField *sodiumValue;
#property (weak, nonatomic) IBOutlet UITextField *chlorideValue;
#property (nonatomic,retain)NSDecimalNumber* ph;
#property (nonatomic,retain) NSDecimalNumber* pco2;
#property (nonatomic,retain)NSDecimalNumber* hco3;
#property (nonatomic,retain)NSDecimalNumber* sodium;
#property (nonatomic,retain)NSDecimalNumber* chloride;
- (IBAction)analyseValues:(UIButton *)sender;
#end
in the implementation
-(void)values{
[self setPh:[[NSDecimalNumber alloc] initWithFloat:phValue.text.floatValue ]];
[self setPco2:[[NSDecimalNumber alloc] initWithFloat:pco2Value.text.floatValue]];
[self setHco3:[[NSDecimalNumber alloc ] initWithFloat:hco3Value.text.floatValue]];
[self setSodium:[[NSDecimalNumber alloc] initWithFloat:sodiumValue.text.floatValue] ];
[self setChloride:[[NSDecimalNumber alloc] initWithFloat:chlorideValue.text.floatValue] ];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self values];}
Then I have another class which takes these values ,but on running the program it runs but does assign values taken from the initialViewController.
#import <Foundation/Foundation.h>
#import "InitialViewController.h"
#interface AcidBaseCalculations : NSObject
#property (nonatomic) float ph;
#property (nonatomic) float pco2;
#property (nonatomic) float chloride;
#property (nonatomic) float sodium;
#property (nonatomic) float hco3;
#implementation AcidBaseCalculations
#synthesize ph,pco2,chloride,hco3,sodium;
-(void)calculations{
InitialViewController *bvc = [[InitialViewController alloc] init];
ph = bvc.ph.floatValue ;
pco2 = bvc.pco2.floatValue;
// these values are not being set although the program runs successfully
hco3 = bvc.hco3.floatValue;
sodium = bvc.sodium.floatValue;
chloride = bvc.chloride.floatValue;
I want to use these new values here and perform some logical operations in this class.
You are creating a new InitialViewController with this line:
InitialViewController *bvc = [[InitialViewController alloc] init];
It is a diffrent instance than your other/original InitialViewController
You either have to make a property
#property(nonatomic,strong)InitialViewController myInitialVC;
And set this property when you are alloc/initing your AcidBaseCalculations.
Our you have to make property for all variables you would like to calculate and set them while alloc/initing to the
AcidBaseCalculations

Calling a function in viewcontroller from appdelegate

I want to call a function in a viewController from my appDelegate but with the following code, it doesn't get called. What am I doing wrong?
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "DetailsToTransfer.h"
#class AccountDetailTransferViewController;
#interface AccountDetailTransferAppDelegate : NSObject <UIApplicationDelegate> {
DetailsToTransfer *objDetailsToTransfer;
}
#property (nonatomic, retain) DetailsToTransfer *objDetailsToTransfer;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet AccountDetailTransferViewController *viewController;
-(void)sendToTransferScreen:(NSArray *)detailsArray;
#end
AppDelegate.m:
....
-(void)sendToTransferScreen:(NSArray *)detailsArray {
[objDetailsToTransfer setLabels:detailsArray];
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:#"DetailsToTransfer" bundle:nil];
[self.window addSubview:objDetailsToTransfer.view];
}
DetailsToTransfer.h:
#import <UIKit/UIKit.h>
#interface DetailsToTransfer : UIViewController {
NSArray *accountDetailsArray;
UILabel *nameLabel;
UILabel *accountLabel;
IBOutlet UIButton *btnTransfer;
IBOutlet UIButton *btnBack;
}
#property (nonatomic, retain) NSArray *accountDetailsArray;
#property (nonatomic, retain) IBOutlet UILabel *nameLabel;
#property (nonatomic, retain) IBOutlet UILabel *accountLabel;
-(IBAction)sendDetails;
-(IBAction)goBack;
-(void)setLabels:(NSArray *)array;
#end
DetailsToTransfer.m
....
-(void)setLabels:(NSArray *)array {
NSLog(#"Setting Labels");
self.nameLabel.text = [array objectAtIndex:0];
self.accountLabel.text = [array objectAtIndex:1];
}
....
I would like to add that all the properties have been synthesized and that I'm calling the method in the appDelegate properly (i checked using NSLogs)
In AppDelegate.m:
Looks as if you are calling a method on your object before the object has been created. Until you have alloc / init your object, there will be no labels to set text.
Try changing your method to this:
-(void)sendToTransferScreen:(NSArray *)detailsArray {
if (!objDetailsToTransfer) {
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:#"DetailsToTransfer" bundle:nil];
[objDetailsToTransfer setLabels:detailsArray];
[self.window addSubview:objDetailsToTransfer.view];
}
}
The problem might be that you are trying to set the values on a object that hasn't been created yet. I also provided an if statement to prevent the object from being created multiple times.

Setting the value of a UITextField in an IBAction

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;

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