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
Related
I am implementing a simple game in which the user tries to guess a randomly selected card. They select their guess from a two-component picker (in FirstViewController), and in the next screen (SecondViewController) they can check if they are correct. I am stuck right now trying to pass the guess the user selected to the screen where they can check.
In SecondViewController.h, I declare properties for both parts of the guess (number and suit) like this:
#property (nonatomic, assign) NSString * guessNumber;
#property (nonatomic, assign) NSString * guessSuit;
Then in didSelectRow in FirstViewController.h, I am trying to pass the info forward like this:
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
SecondViewController *secondVC = [[SecondViewController alloc] init];
if (component == 0){
secondVC.guessNumber = _cardNumbers[row];
}else{
secondVC.guessSuit = _cardImages[row];
}
[self.navigationController pushViewController:secondVC animated:YES];
}
In viewDidLoad for SecondViewController, I NSLog the values of guessNumber and guessSuit, but it prints out null for both.
Clearly I am going wrong somewhere and the data isn't actually being passed, if anyone has any guidance about what I need to fix that would be amazing!
EDIT:
I have now changed the values to copy rather than assign, like this:
#property (nonatomic, copy) NSString * guessNumber;
#property (nonatomic, copy) NSString * guessSuit;
I have tried strong as well, and neither work. The weird thing is that if I print out the value right after I assign it, like this:
secondVC.guessNumber = _cardNumbers[row];
NSLog(secondVS.guessNumber);
I get the right value. Now I am even further confused as it seems to be assigning the value but not saving it when I actually go to my second view controller.
The assign tells the NSString * property setter to save the pointer address instead of the OC object itself.
Both _cardNumbers[row] and _cardImages[row] return an autoreleased object which will be released after finishing your didSelectRow method, so you couldn't get the expected string values in your viewDidLoad of SecondViewController.
Use the strong or copy instead, it tells the property setter to increase the value's retain count by 1, so SecondViewController owns (guessNumber owns) the string value, you could get it in its life cycle all the time.
NSString used copy is right . The problem is _cardNumbers and _cardImages ,let' see data source in _cardNumbers.
I have a problem with a BOOL, when I try to change the value of an another view's BOOL but it is not working.
The declaration of BOOL :
#property (nonatomic, assign) BOOL vuesortie;
Assigning vuesortie in view :
premierview *vcc = [self.storyboard instantiateViewControllerWithIdentifier:#"jeux"];
SWRevealViewController *revealController = self.revealViewController;
premierview *view;// I'm really sorry not to be a robot and do shit
if ([cellchoistxt isEqual: #"Déjà sortie"])
{
[view setVuesortie:YES];
[revealController pushFrontViewController:vcc animated:YES];
} else if ([cellchoistxt isEqual: #"Prochaines sorties"]) {
NSLog(#"////////////////////////////////////////////////");
[view setVuesortie:NO];
[revealController pushFrontViewController:vcc animated:YES];
}
And the code when view is loaded after pushFrontViewController :
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%hhd" , vuesortie); //--> value = 0 in all cases
....
}
premierview *view;
is not initialized.
You have to initizialized your view before (and please use another class name like "PremierView"):
In any case if for premier view you mean your viewController vcc you should to do:
[vcc setVuesortie:YES];
and obviously the class premierview contains the property:
#property (nonatomic, assign) BOOL vuesortie;
You have to study concept of OOP because you must understand that if you want point to the property of your instanced viewController you have to set the value in your vcc object.
Finally please, use correct notation, like for example:
`PremierViewController` and not `premier view`
for 2 reasons:
is not a UIView but a UIViewController and so is correct maintain the end with ViewController rather than View;
use camel notation, so capitalize the first letter of each word.
Looks like you never assign anything to the view variable. When you call [view setVueSortie] the view object is nil.
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);
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
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;