UISearchDisplayController not working when created in code?

前端 未结 2 1711
执念已碎
执念已碎 2021-02-09 12:34

I\'m working on a tab bar application and one of the tabs has a UISearchDisplayController hooked up to a UISearchBar. It\'s all connected up in the NIB and is working. When I ta

相关标签:
2条回答
  • 2021-02-09 13:08

    Usually child objects shouldn't retain its parent. In this case its the parent controller that should retain the child (which is the search display controller).

    This has been done for you automatically when you're creating the SDC in NIB file because it has been added to the view controller's searchDisplayController property and thus retained for the view controller's lifetime.

    However since setting the searchDisplayController property on a view controller is considered usage of private api. You should just add an ivar to retain it and release it on dealloc manually.

    Simply removing the autorelease call is a memory leak as you're leaving around an object you init-ed without saving a reference to it so I don't think it is the correct answer.

    Instead you should retain the searchController properly in an ivar and release it on properly on dealloc as you would any object you want to stay alive for the lifetime of the view controller.

    0 讨论(0)
  • 2021-02-09 13:22

    Wow, I just figured out the problem.

    I figured, because the searchDisplayController property for the UIViewController is set inside the initWithSearchBar:contentsController: message I would still autorelease my copy of the pointer, but when I removed the autorelease the stupid thing started working. Gaaaah. Why would it not retain its own copy (the UIViewController)?

    0 讨论(0)
提交回复
热议问题