Setting variable in another class in objective-c - ios

I have a map-controller where the user can tab the map to add a new marker. The idea is then to store the coordinates in the new marker-class. The problem I am facing is setting those variables.
NewMarkerController.h
#interface NewMarkerController : UIViewController
{
NSNumber *posLat;
NSNumber *posLng;
}
#property (nonatomic, retain) NSNumber *posLat;
#property (nonatomic, retain) NSNumber *posLng;
#end
I am also synthesizing this in the .m file is that makes any difference.
MapController.m
NewMarkerController *vc = [[NewMarkerController alloc] init];
[vc posLat:coordinate.latitude];
The last line shows an error saying No visible #interface for 'NewMarkerController' declears the selector 'postLat', but...there is...?
Can anyone spot the problem I am having here?

[vc setPosLat:coordinate.latitude];
or
vc.posLat = coordinate.latitude;

This syntax:
[vc posLat:coordinate.latitude]
means that posLat is a function of the vc kind of class. As you want to set a variable, if you synthesized it you can just do:
[vc setPosLat:coordinate.latitude]
or
vc.posLat = coordinate.latitude

Related

iphone-Changing float in second ViewController

I have two ViewController , ViewController and getraenkeController.
Now in ViewController.h is float named getraenk. I read that I should do it like this:
#property float getraenk;
and then in getraenkeController.m I should do this:
ViewController.getraenk
but that doesn't work.
I also importet the header from ViewController.h
Now how can I access the float from ViewController in getraenkeController?
The float is added as a property to instances of the class, not the class itself. Once you create an instance of the class, then you'll be able to access the property.
ViewController *controller = [[ViewController alloc] init...];
controller.getraenk = 4334.3;
First declare public property
if you use ARC :
#property(nonatomic, assign) float getraenk;
and then you must create object of ViewController class e.g.:
ViewController *obj = [ViewController new];
now you should get access to getraenk property e.g.:
obj.getraenk

Passing Data between View Controllers (yes, I know... )

Yes, I know that this question is very popular here and has been given a lot of answers to this question, and yes, I was here Passing Data between View Controllers. But I can't do it for a long time.
in ViewControllerB.h I create a property for the BOOL
#property(nonatomic) BOOL *someBool;
ViewControllerA.m:
#import "ViewControllerB.h"
ViewControllerB *viewControllerB = [[ViewControllerB alloc] init];
viewControllerB.someBool = YES;
[self.navigationController pushViewController:viewControllerB animated:YES];
In ViewControllerB.m ViewDidLoad:
NSLog(#"%#", self.someBool);
But xCode give me error on this line ( NSLog(#"%#", self.someBool);) and say: Thread 1:EXC_BAD_ACCESS (code =2). What am I doing wrong?
Your property is a pointer. It shouldn't be. Change this:
#property(nonatomic) BOOL *someBool;
to:
#property(nonatomic) BOOL someBool;
The log should be:
NSLog(#"%d", self.someBool);
Only use %# with objects.
Declare it as a BOOL, not a pointer to a BOOL:
#property(nonatomic) BOOL someBool;
You either need to declare it as a primitive and get rid of the * or store it as an object by wrapping it as an NSNumber
#property (strong, nonatomic) NSNumber *someBool
Then you'd write someBool.boolValue to grab its value

iOS Won't pass variable to child view?

I have faced a problem I can not see the problem to. I am trying to pass a simple NSString to a child variable but it continues to return as null even when I use NSLog to show there is a string in the variable.
The variable finalDate will not pass to the child view.
Parent View
ChangeTimeViewController *ChangeTimeView = [[ChangeTimeViewController alloc] init];
NSLog(#"%#", date);
ChangeTimeView.finalDate = date;
[ChangeTimeView setDelegate:self];
[self.navigationController pushViewController:ChangeTimeView animated:YES];
Child View .H
#import <UIKit/UIKit.h>
#protocol ChangeTimeViewControllerDelegate;
#interface ChangeTimeViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
#property (nonatomic, weak) id <ChangeTimeViewControllerDelegate> delegate;
#property (nonatomic) NSString *enteredTime;
#property (nonatomic, strong) UIPickerView *UIPicker;
#property (nonatomic, strong) NSString *finalDate;
#end
#protocol ChangeTimeViewControllerDelegate <NSObject>
- (void)childTimeViewController:(ChangeTimeViewController *)viewController didChooseValue:(NSString *)string;
#end
Child View .M
NSLog(#"%#", self->finalDate);
What you are doing is perfectly fine. You should insert the NSLog in the view(Did/Will)Appear or some similar method and you may use the self.finalDate notation to make sure you don't try to read some uninitialized ivar.
Note: properties synthesize ivars with _ as prefix (_finalDate is the correct storage unless you synthesized it it with some other name)
If you want to make sure that all input parameters are passed to the view controller, then create an init method for it. Similar to this:
- (id)initWithDate:(NSDate*)date delegate:(id)delegate
Pass NSString As ChangeTimeView.finalDate = #"This Is my Simple String"; and use/put NSLog in viewDidLoad method for show is it rich at nextViewController or not ?? Otherwise if your date (NSString) is proper then Your code is correct.
Check what is happening if you set like,
ChangeTimeView.finalDate = #"MyString";
and in view.m log NSLog(#"%#", self.finalDate);

unrecognized selector sent to instance to a newly added method; messages to previously defined methods work fine

'-[MTviewFilesVC launchVF]: unrecognized selector sent to instance 0x1e59fcd0'
I added a method to a class but calling it creates 'unrecognized selector' run time error
The calling code is:
self.viewFilesVCPtr = [[MTviewFilesVC alloc] init];
[self.viewFilesVCPtr launchVF];
This works if, for example, I substitute viewDidLoad which exists already hence I
think the calling code is OK.Is there something else I need to add to the declaration of lanuchVF
to make it visible?
The method declaration, etc is:
.h:
#import "DirectoryWatcher.h"
#interface MTviewFilesVC : UITableViewController <QLPreviewControllerDataSource,
QLPreviewControllerDelegate,
DirectoryWatcherDelegate,
UIDocumentInteractionControllerDelegate>
-(IBAction)saveViewFiles;
- (void)launchVF;
#end
.m:
#interface MTviewFilesVC ()
#property (nonatomic, strong) DirectoryWatcher *docWatcher;
#property (nonatomic, strong) NSMutableArray *documentURLs;
#property (nonatomic, strong) UIDocumentInteractionController *viewFileController;
-(void) launchVF;
#end
...
- (void)lanuchVF
{
UIStoryboard *settingsStoryBoard = [UIStoryboard storyboardWithName:
#"viewFiles" bundle:nil];
UIViewController *initialViewFilesVC = [settingsStoryBoard instantiateInitialViewController];
initialViewFilesVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:initialViewFilesVC animated:YES];
}
Your method name in the .m has a typo, lanuchVF instead of launchVF :-)

Singleton changing integer

I have a singleton I implement in this way:
PhotoViewController* sharedSingleton = [PhotoViewController sharedManager];
I know that to launch a method I have to do [sharedSingleton method];
but what if I want to change an integer declared in the PhotoViewController.h file as NSInteger* tagNumber, hoe can I do that? I tried this:
[sharedSingleton.tagNumber = 1];
but it doesn't work!
EDIT:
error: property tagNUmber not found on object of type photoViewController
#interface PhotoViewController : UIViewController{
BOOL newMedia;
UIPopoverController *popoverController;
DBRestClient *restClient;
NSInteger* tagNumber;
}
+ (PhotoViewController *) sharedManager;
#end
Singletons are regular objects. The only difference is that only one instance will be created from the class.
If you aren't able to set the tagNumber it is likely that some other type of coding error is happening... perhaps the tagNumber property was declared in a class extension, making the accessor/mutator methods private?
If you edit your question with how the tagNumber is declared, and also include the error message you are getting, I'll be able to edit this answer and give you more specific advice.
EDIT: ...and yes, definitely double check to make sure you didn't declare the NSInteger to be a pointer... an NSInteger is a scalar type (so it takes a direct value, and doesn't use the dereference '*' operator).
I suggest using properties instead of accessing the instance variables directly:
#interface PhotoViewController : UIViewController
#property (nonatomic, assign) BOOL newMedia;
#property (nonatomic, strong) UIPopoverController *popoverController;
#property (nonatomic, strong) DBRestClient *restClient;
#property (nonatomic, assign) NSInteger tagNumber;
+ (PhotoViewController *) sharedManager;
#end
Then set the variable without the brackets as:
sharedSingleton.tagNumber = 1;

Resources