0

I am pretty new to objective-c and I am creating an app where records are held. I have tried to make an adding method when I click a save button though it doesn't save the data when you press the button or if it does it doesn't display it. The data is being shown in a tableView

here is the code for the save button:

-(void)savePatient:(id)sender {
LSAppDelegate *delegate = (LSAppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableArray *patients = delegate.patients;

UITextField *firstnameEntry = (UITextField *)[firstNameCell viewWithTag:777];
UITextField *surnameEntry = (UITextField *)[surnameNameCell viewWithTag:777];
UITextField *dobEntry = (UITextField *)[dobDateCell viewWithTag:777];
UITextField *homeNumberEntry = (UITextField *)[homeNumberCell viewWithTag:777];
UITextField *mobileNumberEntry = (UITextField *)[mobileNumberCell viewWithTag:777];
UITextField *emailAddressEntry = (UITextField *)[emailAddressCell viewWithTag:777];
UITextView *addressEntry = (UITextView *)[addressCell viewWithTag:777];

if (firstnameEntry.text.length > 0) {
    Patient *newPatient = [[Patient alloc] init];
    newPatient.patientName = firstnameEntry.text;
    newPatient.patientSurname = surnameEntry.text;
    newPatient.patientDoB = dobEntry.text;
    newPatient.patientHomeNumber = homeNumberEntry.text;
    newPatient.patientMobileNumber = mobileNumberEntry.text;
    newPatient.patientEmail = emailAddressEntry.text;
    newPatient.patientAddress = addressEntry.text;
    newPatient.patientPicture = nil;
    [patients addObject:newPatient];
    LSViewController *viewController = delegate.viewController;
    [viewController.tableView reloadData];
}
    [delegate.navController popViewControllerAnimated:YES];

}

I've found that the issue is here

if (firstnameEntry.text.length > 0) {

please say if you want any more code

Thanks in advance

6
  • Okay. So you have lines of code for the save button. And? If you click on the button, what happens? You don't say where you have difficulty. Commented Jun 21, 2013 at 17:54
  • it goes back to the first page and then nothing else Commented Jun 21, 2013 at 17:55
  • You could try adding a couple breakpoints or NSLog statements in the savePatient method to see what the value of firstNameTextEntry.text is and see if it actually enters your if statement Commented Jun 21, 2013 at 18:16
  • This may be a dumb question, but are you using the tag 777 over and over in your actual code, or did you just put that in there when you added your code here? Commented Jun 21, 2013 at 20:05
  • @MattBecker that is the first time i've used 777 why? Commented Jun 22, 2013 at 7:59

2 Answers 2

3

You need to make sure that your delegate has the proper retain type and not copy as copy in the property returns an array and not an NSMutableArray.

Although your problem of loading data is probably a simple issue of not allocing/initing your array. It would be better to move your patients array to your local class.

Move this property from your delegate to your class.

@property (nonatomic, retain) NSMutableArray *patients;

Do not use copy and do not use NSArray. Also, you need to make sure that somewhere you are instantiating the mutable array. Preferably in the viewDidLoad function.

In your class, somewhere before this viewcontroller is created be sure you create the array.

self.patients = [[NSMutableArray alloc] init];

This assumes you're using ARC. If you're not using ARC be sure to release the array in the dealloc method.

Usually if data isn't coming across it's b/c your array is nil or an NSArray and not NSMutableArray.

I would also add breakpoints in your method above and make sure that your tableView is not nil and that your array is not nil.

Sign up to request clarification or add additional context in comments.

10 Comments

would you like my app delegate files?
This is a really bad idea. If you have a property that needs to be changed don't just make it mutable so that it can be changed from outside the class, you've just broken encapsulation.
@Abizem, I appreciate your comment. However, he asked why his data wasn't loading. The reason it isn't loading is b/c the array he is using isn't mutable or isn't alloced. I'm not going to refactor his code in order to answer a simple question. Also, you propose no solution to the problem. I gave him a practical solution.
And aside from that adding an array on your delegate is generally a poor idea as well. In fact most of your code @Hive7 is crap. So get cranking and eventually we'll answer your simple data loading question, but not until you understand encapsulation, inheritance, and polymorphism.
@Inturbidus - actually that assumes not-ARC. self.patients = [NSMutableArray array]; assumes ARC.
|
0

Are you sure that [viewController.tableView reloadData]; actually get executed, i seriously doubt it since you're on a different viewController, you could try to put the reloatData statement on the viewDidAppear event of your table controller view after you pop from the editing viewController.

1 Comment

I have imported it so I think so. do you know, if that is not happening, what to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.