UITextField.text keeps returning null - objective-c

I have create a delegate for this UIViewController, and I am passing in an employee name to set a UITextField. But, no matter what I do, as you can see, it results in (null). However, I have a datelabel that gets populated in a similar way, but it works correctly. I include that for reference. I am missing something simple. But, now I am blind from staring at it...
Any ideas? Thanks.
#interface TimesheetEntryViewController()
#property (weak, nonatomic) IBOutlet UILabel *datelabel;
#property (weak, nonatomic) IBOutlet UITextField *employeeTextField;
#end
#implementation TimesheetEntryViewController
#synthesize tableView = _tableView;
#synthesize datelabel = _datelabel;
#synthesize employeeName = _employeeName;
#synthesize employeeTextField = _employeeTextField;
-(void)setEmployeeNameString:(NSString *)lemployeeNameString
{
self.employeeTextField.text = lemployeeNameString;
NSLog(#"%#:%#", lemployeeNameString, self.employeeTextField.text);
}
Result: 2012-05-02 08:42:50.137 rbTimesheet[7855:11303] Bob Smith:(null)
-(void)changeDateViewController:(ChooseDateViewController *)ntvc didChooseDate:(NSDate *)lDate
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
self.datelabel.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:lDate]];
[self dismissModalViewControllerAnimated:YES];
}

I have mapped the textfield through drag and drop in xcode 5 ,So it is not synthesized but i also tried to resolve this issue by synthesizing it manually but it not worked.
I resolved this issue by assigning empty string in the text field object in viewDidLoad method
_txtDialedNumber.text=#"";
Note:-While using this i am not synthesizing the textfield object and it is working for me in Xcode 5.0(testing in iPhone 5 IOS 6)

So I had the same issue and after googling and coming across multiple posts like this my issue was a fellow developer was doing this(which worked in previous versions of xCode somehow) which I cut and pasted from a similar project
-(void)setTextFieldDelegates{
self.usernameTextField = [[UITextField alloc] init];
self.passwordTextField = [[UITextField alloc] init];
[self.usernameTextField setDelegate: self];
[self.passwordTextField setDelegate: self];
}
So even though the outlet was connected he was somehow re assigning it and it was working.

Check if the connection between the text field and the outlet is correct in the Interface Builder.

Check if you have subclassed the uitextField by using
textField=[[UITextField alloc]init].
If you have done that, try removing it from the code. I was having the same problem and I got it working by removing these lines.

Maybe you are calling setEmployeeNameString: before the view is loaded? That should give you null as a result.

It looks as if you are overriding the automatic setter/getter for your textfield. The whole point of using #synthesize is to save you some code and hassle from setting them yourself.
My guess is you aren't setting the delegate right. You are saying that the current view controller text field should get the date, then you dismiss the modal view killing the value saved there.
I would change your reference from weak to strong and get rid of your setter. It is being done for you already, no need to clutter things up.

Related

self.view removeFromSuperview crashes

i hope this is my last question for a while, I open a xib file via:
Results1 *myView1 = [[Results1 alloc]initWithNibName:#"Results1" bundle:nil];
[self.view addSubview:myView1.view];
I have a button on the second xib file:
-(IBAction)Button1:(id)sender
{
[self.view removeFromSuperview];
}
It crashes every time:
0xecf09b: movl 8(%edx), %edi
I have the views linked
I am not sure if this is the problem:
#interface TestTypingToolViewController ()
{
NSString *iResults1;
NSString *iResults2;
NSString *iResults3;
NSString *iResults4;
NSString *iResults5;
NSString *Segment;
NSDictionary *ResultsData;
}
#end
Thanks for all your help, everyone!
The problem is I needed to turn off Automatic Reference Counting and everything worked.
It seems that you have got some issue with zombies, i.e., some object that is deallocated at some point but you try to access through some (dangling) reference. You could get more info about it by enabling zombies detection.
Actually, my guess is that you could fix this by storing Results1 *myView1 in a property of your class. Indeed, in your code, what happens is the myView1.view is retained by self.view; while myView1 is stored in a local variable, so the object (under ARC) should be deallocated when the variable is not used anymore. You have a mismatch here between the lifetimes of the two objects and this could lead to the crash.
ClassName.h
#property (nonatomic, strong) UIViewController *myView1;
ClassName.m
#synthersize myView1;
// in -(void)viewDidLoad
self.myView1 = [[Result1 alloc] init];
[self.view addSubview:self.myView1.view];

Access class variable from another class

I have a UITabBarController that manages two ViewControllers. The first is a UIViewController that allows the user to change game settings. The second is a GLKViewController that runs the game simulation.
I'm trying to enable the Game ViewController to fetch the settings from the Settings ViewController. I have a Slider on the Settings View that represents "Speed".
I have a reference to the other controller, but I'm unable to expose the variable that backs my Slider properly.
SecondViewController.h
#interface SecondViewController : UIViewController{
IBOutlet UISlider * mySlider;
}
property (nonatomic,retain) IBOutlet UISlider * mySlider;
#end
SecondViewController.m
- (IBAction) mySliderWasMoved:(id)sender;
#implementation SecondViewController
#synthesize mySlider;
- (IBAction) mySliderWasMoved:(id)sender{
};
ThirdViewController.m
NSArray *tmpVCs = self.parentViewController.tabBarController.viewControllers;
UIViewController *tmpVC = [tmpVCs objectAtIndex:1]; //obtain handle to SecondViewController
//NSNumber *mySpeed = tmpVC.mySlider; //doesn't see mySlider
NSNumber *mySpeed = [tmpVC mySlider]; //doesn't see mySlider
I'm new to this, and there are many aspects of my project to learn - so I'm not trying to learn how to manage data at this time. I just need to know how to access an instance variable
As mention on the comments,
Use NSDefault to save the value on slider changed. On the very first time of loading your application, you will want to set a default value.
Use Singleton Object to store value.
We understand that, quoting from you " not trying to learn data persistence at this time. Nor do I need architecture direction.", but the rule of thumb here is that you probably will be able to access the instance variable in some way or the other but i think having the best approach will benefit you greatly.
Just my 2 cent.
Fort the benefit of others: I grabbed a handle to the other class, but I hadn't declared the return type as the correct type of class.
Replace:
UIViewController *tmpVC = [tmpVCs objectAtIndex:1];
With:
SecondViewController *tmpVC = [tmpVCs objectAtIndex:1];
Now I have access to the properties that are specific to the SecondViewController.

what is the correct way to pass values to properties?

i have problems with a really basic thing. setting properties after instantiation a UIViewController-
header.h
IBOutlet UITextField *actionLocationFld;
#property (nonatomic, retain) IBOutlet UITextField *actionLocationFld;
main.h
#synthesize actionLocationFld;
(void)loadSocialActionView:(id)sender
{
self.socialActionView = [[SocialActionViewController alloc] init];
self.socialActionView.actionLocationFld.text = #"test";
[self.view addSubview:self.socialActionView.view];
}
actionLocationFld is a UITextfield. i try to set the text value after instantiation but it does not work. it looks so easy but i can´t figure out. what do i wrong?
any suggestions?
This may happen because actionLocationFld isn't initialized.
Try to call functions in this order:
self.socialActionView = [[SocialActionViewController alloc] init];
[self.view addSubview:self.socialActionView.view];
self.socialActionView.actionLocationFld.text = #"test";
i´ve solved it not by adding another property and passing it the value. it seems it is not possible to access the text property of an UITextfield by the way i tried. here´s the new code line
self.socialActionView.address = #"test";
inside of my viewcontroller i pass the value forward to self.view.actionLocationFld.text

Properly passing NSManagedObjects between views when using ARC? Memory warning & crash

Hi I have a nasty memory management issue under ARC and cannot figure out how to solve it. The problem stands like this, I have these objects:
ObjectManager - a singleton that does fetchRequests to core data and fetches one or more NSManagedObjects
UIViewControllerA - a view controller in which I have a button "PassManagedObject" and a property declared as below:
#property (strong, nonatomic) ManagedObject *objectForToday;
in viewDidLoad on UIViewControllerA, I call the method refreshDailyObject which does this:
self.objectForToday = nil;
self.objectForToday = [[ObjectManager sharedManager] getDailyObject];
if I tap the PassManagedObject button I create UIViewControllerB, pass the objectForToday to it and display it, see below
- (IBAction)passManagedObjectTapped:(id)sender {
UIViewControllerB *viewController = [[UIViewControllerB alloc] initWithNibName:#"UIViewControllerB"];
viewController.object = self.objectForToday;
[self.navigationController pushViewController:viewController animated:NO];
}
UIViewControllerB has a property declared like this:
#property (strong, nonatomic) ManagedObject *object;
and a button "Back" which does this:
- (IBAction)backAction:(id)sender {
self.object = nil;
[self.navigationController popViewControllerAnimated:NO];
}
Now the problem is this. If I tap continuously passManagedObjectTapped and then backAction, and again passManagedObjectTapped and again backAction and then again passManagedObjectTapped and automate this I'm getting eventually Received Memory Warning 1, and then crash.
The Instruments doesn't show any leaks but my memory allocation keeps slowly going up.
I am using ARC under iOS4.3 & iOS5. I've been struggling to figuring out what is wrong for a day now. Any help will be highly appreciated.
Thanks!
The self.object = nil; and self.objectForToday = nil; isn't necessary - ARC and the synthesized properties setters already take care of that.
It seems pretty likely that you have a circular reference somewhere. Do you have any situations where object A has a strong reference to object B and object B has a strong reference to object A?
If so, just change one of those references to weak instead (or assign if you want to support iOS 4.3).

UITableView and delegates

I have an iPad app in progess but I'm having difficulty catching the selection of a row in my table view. I know this is because I haven't defined my delegate properly yet but, after 2 hours on the net, it still isn't making much sense.
What I'm trying to do is pass the selected table row item to a new view that displays info based on the selection - pretty standard.
I set up the tableViewController sub class using the option to create it as a UITableViewController subclass which, unless I am wrong, incorporates the delegates (UITableViewDelegate and UITableViewDataSource) automatically.
In the didSelectRowsAtIndex method I'm trying to create a DetailViewController. I've tried with a nib file and creating one purely in code but the class is never created. I'm missing a step I'm sure of it but I can't find what it is. At some point shouldn't I be defining what function I want to access with the selected row? But where? How?
In what I considered was my best attempt, I created the DetailViewController, set a string variable in the detailViewController to the selected row, and then tried to add the detailViewController view to display. I figured I could then use the viewDidload to call the next function but the view never got displayed on screen.
Some basic guidence would be nice. Or a decent tutorial would be nice. No calls to read the relevant docs please, I've been over it and right now I need a example to pull things together.
Thanks,
Steve
I think you are missing this line in your didSelectRowAtIndexPath: method
[self presentModalViewController:controller animated:YES];
where controller is an object of class DetailViewController
Yeah, maybe paste the code snippet will be easier to figure out what's going on here. And are those delegate(didSelectRowAtIndexPath:) methods being called correctly?
Try this,
this goes in didSelectRowAtIndexPath
MoreInfoTable *moreInfoView = [[MoreInfoTable alloc] initWithStyle:UITableViewStyleGrouped];
//in the MoreInfoTable, make properties like titles etc.
[self.navigationController pushViewController:moreInfoView animated:YES];
[moreInfoView release];
}
here's an example of an MoreInfoTable.h
#interface MoreInfoTable : UITableViewController {
NSMutableArray *moreInfo;
NSURL *getDirections;
NSURL *getWebsite;
NSMutableString *getPhoneNumber;
NSString *address;
NSString *footer;
float lat, lon;
}
-(void)goToWebsite;
-(void)goToMaps;
-(IBAction)addToFavorites:(id)sender;
-(void) callNumber;
#property (nonatomic,retain) NSURL *getDirections;
#property (nonatomic,retain) NSURL *getWebsite;
#property (nonatomic,retain) NSMutableString *getPhoneNumber;
#property (nonatomic,retain) NSString *footer;
#property (nonatomic,retain) NSString *address;
#property (readwrite) float lat;
#property (readwrite) float lon;
#end
now back in the other file in which you declare the table, you can say
MoreInfoTable *moreInfoView = [[MoreInfoTable alloc] initWithStyle:UITableViewStyleGrouped];
//in the MoreInfoTable, make properties like titles etc.
moreInfoView.title = #"TITLE!";
//etc.
[self.navigationController pushViewController:moreInfoView animated:YES];
[moreInfoView release]; //