当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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 first 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.

HTH,

Akshay

分享给朋友:
您可能感兴趣的文章:
随机阅读: