开发者

Initialize a UIViewController before an item in a search bar is selected

开发者 https://www.devze.com 2023-04-10 15:48 出处:网络
I have a UITabBarController and a SearchBar on the home page.The SearchBar searches items in the database and when an item is selected, jumps to that item in tab 2.I changed my view hierarchy to use a

I have a UITabBarController and a SearchBar on the home page. The SearchBar searches items in the database and when an item is selected, jumps to that item in tab 2. I changed my view hierarchy to use a UINavigationController in that tab. In my viewDidLoad of the rootViewController for the UINavigationController, I push the first viewController (out of 3). It's on this viewController that the search item goes to.

If I go to that tab just once, then my first viewController is loaded, and I select a search item, then it works. The problem is if I never go to that tab and my first viewController is not pushed onto the stack, then the search does not know where to go and crashes. I'm pretty sure I do NOT call viewDidLoad myself from my first tab to ensure that the fir开发者_开发知识库st ViewController is pushed onto the stack. How do I get around this problem? Is there something I can do in loadView? Thanks.


You are correct, viewDidLoad is a delegate method, not supposed to be called by you. One of the approaches that you can follow is- from tab 1, set an instance variable of firstViewController in tab 2, something like pendingItemIndex, corresponding to the item that is to be displayed in tab 2.

Then, in viewDidLoad of firstViewController, check if pendingItemIndex has a valid value (you can set the default value to -1). If it does, display the corresponding item.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号