0

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?

4

1 回答 1

0

当您执行展开转场时,除非您对它们有强烈的引用,否则您正在展开的控制器,回到但不包括您正在展开的控制器的任何控制器都将被释放。因此,药丸的所有者属性(如果它在 MedicineCabinetViewController 中)将在控制器被释放时消失。

有几种方法可以解决此问题。一种方法是使用代码而不是使用 segue 在您的应用程序中导航,因此您可以保留对您创建的控制器的引用,并且仅在它尚不存在时分配一个。

另一种方法是拥有一个单例模型类,它将在应用程序的生命周期中存在。这样,所有关于药物的数据都将是这个单例类中某个对象(或多个对象)的属性。

于 2013-07-21T18:10:26.963 回答