How do I pass a string or data object between two view controllers?

前端 未结 2 658
梦谈多话
梦谈多话 2021-01-28 01:00

In my last question I asked how to best send a string from one view controller to another, both which were on a navigation stack: Pass string from tableviewcontroller to viewcon

2条回答
  •  温柔的废话
    2021-01-28 01:42

    Passing a reference would be less overhead than reinitializing a fresh object in the child view controller.

    Set up a retain property in the child view controller for the NSString instance.

    In the parent view controller, instantiate the child view controller and set its string property equal to the string you want to pass it:

    childViewController.myStringProperty = parentViewControllerString;
    

    As this increments the retain count of the string, you're not recreating the object, just keeping a reference to it.

    You could instead retain an NSData instance, if you wanted. This would be less of a hit than recreating it in the child v.c.

提交回复
热议问题