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);
Related
I have a UITableViewController :
#import <UIKit/UIKit.h>
#import "MainClass.h"
#interface MainViewController : UITableViewController
#property (strong, nonatomic) MainClass *mainClass;
#end
And a class that i want to use as a data source for the table view:
#interface Domain : NSObject <UITableViewDataSource>
-(id) initWithName: (NSString*)name;
#property (nonatomic, retain) NSString* name;
#property (nonatomic, retain) NSMutableArray* list;
#end
When i want to simply test if it's working i create a local domain and i add some documents in it:
Domain* domain = [[Domain alloc] init];
Document* document1 = [[Document alloc]initWithName:#"test1"];
[domain.list addObject:document1];
And then i declare the domain as a data source ( i overrides the two required methods) :
self.tableView.dataSource = domain;
Unfortunately, this cause a Bad access exception.
I guess it's because the local variables are released too soon.
I'm guessing this because when i declare both domain and document as property, it works just fine.
Can anyone explain me the reason of this too soon release and how to avoid it?
'dataSource' is a weak property, so you must keep another reference to the object for it not to be released.
Therefore defining a property on the view controller is really the best solution.
Create strong reference to domain, for example:
#property (strong, nonatomic) Domain* domain;
And change allocation of your domain object to:
self.domain = [[Domain alloc] init];
Document* document1 = [[Document alloc]initWithName:#"test1"];
[self.domain.list addObject:document1];
and after that:
self.tableView.dataSource = self.domain;
In my main view controller, I have a UITextField, and I am trying to save the text input into it to a NSString in my Homework model(class).
Homework.h
#import <Foundation/Foundation.h>
#interface Homework : NSObject
#property (nonatomic, strong) NSString *className;
#property (nonatomic, strong) NSString *assignmentTitle;
#end
Homework.m
#import "Homework.h"
#implementation Homework
#synthesize className = _className;
#synthesize assignmentTitle = _assignmentTitle;
#end
In my assignmentViewController, I create an object of type Homework and try to set it equal to whatever is entered into the the UITextField when the Save Button is pressed.
Assignment View Controller
#import <UIKit/UIKit.h>
#import "Homework.h"
#interface Assignment : UIViewController {
}
#property(nonatomic) IBOutlet UITextField *ClassNameField;
#property(nonatomic) IBOutlet UILabel *ClassNameLabel;
#property (weak, nonatomic) IBOutlet UIButton *SaveButton;
#property (nonatomic, strong) Homework *homeworkAssignment;
- (IBAction)Save:(UIButton *)sender;
#end
AssignmentViewController.m
- (IBAction)Save:(UIButton *)sender {
self.homeworkAssignment.className = self.ClassNameField.text;
NSLog(#"SaveButtonPressed %#", self.homeworkAssignment.className);
}
The NSLog prints out that className is (null). Can anyone help me figure out what I am doing wrong? This is my first ever iOS app (besides Hello World).
Edit: This is using ARC
Edit: I tried changing
self.homeworkAssignment.className = self.ClassNameField.text; to
self.homeworkAssignment.className = #"TEST";
and the log still shows (Null).
Double check you properly linked ClassNameField outlet and that you're initializing homeworkAssignment. Something like.-
self.homeworkAssignment = [[Homework alloc] init];
By the way, you should consider using camelCase notation for your variable names :)
Well to be honest the first steps are always hard but you should learn it the right way, héhé
First of all synthesize this way:
#synthesize labelAssignmentTitle,labelClassName;
or
#synthesize labelAssignmentTitle;
#synthesize labelClassName;
there is no need to do the following:
#synthesize className = _className;
#synthesize assignmentTitle = _assignmentTitle;
Now if you initialize the right way from the the start you'll find it a lot easier later!
HomeWork.h
#interface HomeWork : NSObject
#property (nonatomic, strong) NSString *className;
#property (nonatomic, strong) NSString *assignmentTitle;
-(id)initWithClassName:(NSString *)newClassName andAssignmentTitle:(NSString*)newAssigmentTitle;
HomeWork.m
#implementation HomeWork
#synthesize assignmentTitle,className;
-(id)initWithClassName:(NSString *)newClassName andAssignmentTitle:(NSString*)newAssigmentTitle {
self = [super init];
if(self){
assignmentTitle = newAssigmentTitle;
className = newClass;
}
return self;
}
#end
ViewController.m
- (IBAction)saveIt:(id)sender {
HomeWork *newHomeWork = [[HomeWork alloc]initWithClassName:[labelClassName text]andAssignmentTitle:[labelAssignmentTitle text]];
}
Because of this, you directly make a newHomeWork object with the parameters given by your two UITextFields.
Now print it out in your logmessage and see what happends ;)
I would like to create an NSObject class that I can use the instance of and save to its variables and later pass its data elsewhere (NSManagedObject). Do I need to do anything else besides creating a new Object-C Class that inherits from NSObject. Create Variables in .h and synthesize in .m.
i.e.:
my .h file:
#import <Foundation/Foundation.h>
#interface MyDataClass : NSObject
#property (nonatomic, retain) NSNumber *variable1
#property (nonatomic, retain) NSString *variable2
#property (nonatomic, retain) NSDate *variable3
#end
.m file:
#import "MyDataClass.h"
#implementation MyDataClass
#synthesize variable1, variable2, variable3
#end
I would like to be able do the following in some SomeViewController:
#property (nonatomic, strong) MyDataClass *newDataClass
#systhesize newDataClass;
newDataClass.variable1 = #"123457890";
newDataClass.variable2 = #"This is the new Variable";
newDataClass.variable3 = [NSDate date];
Is there anything else I need to do to initialize each variable when an instance of this class is created? Am I missing anything?
That's all that you need for a custom object which you want to use to store data.
Of course when you go to use it (in SomeViewController), you need to actually create an instance of your class before you start setting the variables:
self.newDataClass = [[MyDataClass alloc] init];
I would also use self.newDataClass instead of just newDataClass unless you have a reason not to.
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;