Unwind Segue nil argument error - iphone

I am new to ios development and I am trying to create a simple test app that allows users to record and edit their pills in their medicine cabinet.
I am using ios 6.1, arc and segues.
Background to the error:
So the user has a list of pills in their medicine cabinet, he picks one and a segue brings him to a view controller that gives him information about that pill. He then has the option to edit the info which segues him to another view controller that shows all the current info in textfields which he can edit and then click a save button which initiates an unwind segue to the original medicine cabinet.
The error I am getting is this. The user can edit the info and save it successfully and pop back to the cabinet. But then when he goes to edit the pill again, I get the error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString appendString:]: nil argument'
I did some digging and I found out what is happening. In the unwind segue method,I am successfully setting the properties of the pill to match the text in the textfield of the editPillView controller. For example, I have a pill and it belongs to Jay. But then I edit it and say it belongs to Raf. Using breakpoints, I can see the string change from Jay to Raf in the unwind segue method of the MedicineCabinetViewController. But once the unwind segue is complete, I see that at some point, the string gets set to nil in apple's code (probably because arc is deallocing memory because the EditPillViewController is no longer needed and the pill's owner property then becomes a null pointer). Then when the user clicks the pill again, the pillInfoViewController is trying to use the null pointer and BOOM.
I've tried so many solutions to try to keep the string to stay raf such as using the NSString copy method. Any ideas?

When you do an unwind segue, any controllers from the one you're unwinding from, back to but not including the one you're unwinding to, will be deallocated unless you have a strong reference to them. So, the pill's owner property, if it's in MedicineCabinetViewController, will go away when the controller is deallocated.
There are several ways you can fix this problem. One way, would be to navigate in your app using code rather than using segues, so you can keep references to the controllers you create, and only allocate one if it doesn't exist yet.
Another way would be to have a model class that's a singleton, which would live for the life of the app. That way all the data concerning the medicines would be properties of some object (or objects) in this singleton class.

Related

iPhone [self performseguewithidentifier... ] is not showing the new view controller

I have been all over stackoverflow and all over Google and I cannot seem to figure this one out. Here's my scenario:
I have my app's "main screen" where the user first makes decisions about what they're going to do. The app works off of a CoreData database which is created by "importing" XML files. The user can choose to open an XML file attached to an email in my application, which automatically triggers my main screen to show up and run the import of the file.
I can get this far without any issues. In my storyboard, I have a segue called ParseSegue from my main screen to a view controller which will handle the parsing and give the user some status information.
When the main screen is called via the email app, the main screen automatically calls
[self performSegueWithIdentifier:#"ParseSegue" sender:self];
I then check for this segue name in prepareForSegue and it's a valid name. This is where I assign the file URL to the parser controller so that it can parse the correct file.
The problem is that the segue never actually happens. The prepareForSegue method gets called, the name "ParseSegue" can be checked against and is valid, but the segue itself simply does not happen. If I add a button to the main screen and tell it to perform the segue within the storyboard, it works fine. But calling it programmatically seems to do nothing.
It turns out that I was looking in the wrong place entirely. My problem was that in my appDelegate, where the app reacts to the incoming URL, I was inadvertently creating a new instance of my storyboard and my main view controller. This was different than the one which was already active and may or may not have been on the screen.
The controller I was creating was never actually shown. I only noticed this because the log:
NSLog(#"Source: %#", [segue.sourceViewController description]);
would show different memory addresses for my test (the button push) and the import test. This led me to believe that I was, in fact, working with two different instances of the storyboard and the app's main view controller. Thanks to Paul for the suggestion of logging the destination and the source controllers.

Weird XIB file issue

Experiencing a weird problem in a modified program written by me. In my first iteration, the view controller had an IBAction by the title userSpecifyingInput and had all my buttons wired up to this IBAction.
To make the design more sophisticated, I introduced userSpecifyingDigit and userSpecifyingLetter and accordingly had some buttons wiring up to the first IBAction (userSpecifyingDigit) and the next button wiring up to the second IBAction (userSpecifyingLetter).
Upon running my program, it gave me this error:
terminating app: NSInvalidArgumentException
Unrecognized selector sent to instance
And these selectors were alternating between userSpecifyingInput and userSpecifyingDigit which did not make sense as userSpecifyingInput was completely removed from my ViewControllers interface and implementations.
Now I am not sure why userSpecifyingInput still existed, but after dabbling with sent events, I noticed that the buttons were wired upto the new selectors as well as the old selector.
I had to manually remove the old selector from the touchupinside events for all the buttons.
Naturally this does not seem to be a very convenient way to go about proceedings and if the view controller selectors are modified then the touch events ought to be automatically removed.
Am I missing something here? This is a pretty open ended question with different answers
Eliminating an IBAction from your implementation will not remove any previous links to it in IB. I haven't heard of any XCode/IB preferences to shortcut this issue.
Sounds like the xib still thinks one of it's objects is hooked up to the UIViewController, but the function on the UIViewController is no longer there. To check and fix this...
Click on the xib file in the project navigator
Select the File's Owner
Click on the Connections Inspector
Verify that none of these connections are invalid
I would also look in the UIViewController to make sure no automagically created references exist here as well.

Why does UITableView loses ManagedObjectContext after returning from a ModalView?

I have created a window based application with the following
a TableViewController (Without a XIB file)
a ViewController (With a XIB file) <-- to be used as modal view
a CoreData model to store some data
I managed to load the application and populate the TableView with the data from the Entity, and I was able to scroll through all of the cells of the TableView, without any issues.
I added a UIBarButton item (rightBarButton) that causes a Modal View to appear for the user to input some data. The model view has a SAVE and CANCEL buttons.
The problem is once I press the Cancel Button, I go back to the TableView but if I try to scroll throgh the items in the tableview, the app crashes.
After 4 hours of searching Google and StackOverflow, I was not able to see why my app crash. I did however notice by the debugger that the ManagedObjectContext is set to NIL the second time I scroll the tableview (after the modalview is dismissed), although no data is changed and no insertion/deletion occured.
I tried using a timer to call reloadData as I found some answers on StackOverflow, but that did not work. I tried setting the ManagedObjectContext as a property with retain and removed all occurences of [myManagedObjectContext release] to avoid releasing it earlier than needed, but that did not help.
It seems that I am doing an obvious mistake, but I am not sure where.
Please help.
ivars do not become nil just because they're released somewhere else (at least not in iOS 4.3). So an over-release is not the specific cause of myManagedObjectContext becoming nil. Assuming you're using accessors to reference your ivars (and you should be), hand-implement setManagedObjectContext: and put a breakpoint in there to see who's calling it. Alternately, you can add a gdb watchpoint to myManagedObjectContext to see when the memory is changed.
You haven't indicated what the crash stack is when you crash. You should be focused on what memory you're accessing at the point of the crash, and ensuring that the crash is due to a memory violation rather than an exception. Check your debugger output. Often it will tell you what's happening.

Works in iPhone Simulator, but not on Phone

So I have an app which runs fine on the simulator, but not on the actual device.
At runtime, it gives me the following error:
2010-12-05 19:58:32.006 Sports[4668:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableView isEqualToString:]: unrecognized selector sent to instance 0x800800'
A bit about the structure: I have a Tab Bar Controller, the first view being a UINavigationController.
The view in there is a UITableView. One thing that may or may not be related is that if I do #synthesize tableView; in the table view controller, the table stays blank on both simulator and phone (but does not crash).
If I take that out, it loads the correct data on the simulator, and crashes on the phone.
Where should delegate/dataSource be linked to in the Interface Builder? I tried linking it to "View" to "File's Owner", and making a new "ViewController" and none of those worked.
Both the delegate and dataSource should be linked to File's Owner, which is the view controller class that declares the table view as an IBOutlet; this should be the same view controller that owns the nib file. Additionally, that view controller should be implement the UITableViewDelegate and UITableViewDataSource protocols.
Add that #synthesize line back in, (unless you're subclassing UITableViewController, as pointed out by grahamparks in the comments!) make sure those connections are right, and, finally, make sure you've declared an IBOutlet for the table view, and connected that properly between your class and interface builder.
Found it!
Turns out that there wasn't really a problem with this at all. The problem was that the date field in my database, when run on my phone was always zero/nil.
Why? Because the NSDate object created never initialized and stayed at nil.
Why?
Because my phone is in 24 hour time and did not parse the am and pm properly.
Lessons learned!
Run your app with NSZombieEnabled set to yes. See http://www.cocoadev.com/index.pl?NSZombieEnabled for an explanation about it and how to set it. I think what you will find is that the app will now throw an exception in the simulator telling you that you are sending a message to an object that has been released.
Based on the error message you are receiving, I expect the culprit is a NSString or NSMutableString object. If it is an NSString then warning, that NSString could be shared by several different objects so figuring out where the extra release is might be hard.
Never fear though, Instruments helps tremendously in this regard. Here is a link that explains how to use Instruments to find out exactly where your object is being retained and released so you can track down which release is inappropriate. http://www.markj.net/iphone-memory-debug-nszombie/
Good luck!

Core Data, KVO, and NSInternalInconsistencyException

I'm using Core Data and KVO to look for changes in values to trigger saves to the data store. I have a table view with search hooked up to NSFetchedResultsController. When the search changes, a new results controller is made on pressing the Search button. When the user selects an item in the results table view, then the user enters a detail view and can make edits.
This is where I encounter the problems. When the user makes a change in a separate UIControl that I made, the detail view is notified and can use the NSFetchedResultsController it remembers from when the table view pushed me onto the view stack to get the NSManagedObjectContext and do a save. When I do so, I sometimes get the following error:
Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. no object at index 89 in section at index 0 with userInfo (null)
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 89 in section at index 0'
I found that this tends to happen when I do a search and an object that isn't on the top of the table is moved up to the top. I vaguely know what might be wrong in the back of my head, but would appreciate any pointers as to how to fix this.
I figured out what was wrong. I forgot to unset the old NSFetchedResultsController's delegate, and also forgot to release said NSFetchedResultsController. My UITableView subclass also responded to updates to the NSFetchedResultsController through delegates, and caused weird calls to be made.