iOS:从 UITabBar/UINavigationController 应用程序中的视图设置 nib 子视图中的文本
我在通过另一个视图加载视图时以编程方式设置 UISearchDisplay 的文本值时遇到问题,我的问题是我是否使问题过于复杂并错过了某些内容,或者我的思维是否正确。
情况是这样的:我有一个 UITabBarController 作为我的根视图,有 2 个选项卡,都有一个 UINavigationController 设置,所以我可以根据需要推送视图。
选项卡 1 有一个 UITableViewController,其中填充了类别列表。
选项卡 2 的主视图中有一个 MapView,但我已经完成了自定义 UINavigationItem 视图,以将各种按钮和 UISearchDisplay 放在 rightBarButtonitem 区域上。
地图视图布局和自定义导航项作为两个单独的视图对象存储在同一笔尖中。在选项卡 2 的 viewDidLoad() 中,我以编程方式初始化 rightBarButtonItem:
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:buttonBar];
self.navigationItem.rightBarButtonItem = btnItem;
[btnItem release];
一切都会启动,buttonBar 作为 searchWhat 连接到 IBOutlet,我可以从 Mapview 的控制器类中与该对象进行通信。
如果用户位于选项卡 1 中并点击一个单元格,我希望它切换到选项卡 2 并填充 searchWhat.text,然后执行搜索代码,就像有人自己输入搜索一样。
我遇到的问题是视图上的加载和填充顺序。
我可以毫无问题地从第一个选项卡访问第二个选项卡,并使其显示如下内容:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Quick Category cell tapped at row %d", indexPath.row);
self.tabBarController.selectedIndex = 1; // change to the search view controller
//[self.tabBarController.selectedViewController viewDidAppear:YES];
UINavigationController *nav = (UINavigationController *)self.tabBarController.selectedViewController;
SearchViewController *srch = [nav.viewControllers objectAtIndex:0];
//NSLog(@"%@", [srch description]);
[srch queueSearchByType:kSearchTypeQuickCategories withData:[catList objectAtIndex:indexPath.row]];
[srch viewDidAppear:YES];
}
不要担心 catList 和 SearchViewController,它们存在,并且此位可以切换选项卡。
但这是问题所在,如果用户启动应用程序并在选项卡 1 中选择一个项目,则会出现选项卡 2,但搜索显示文本的值不会设置 - 因为 viewDidLoad 和 viewDidAppear 在另一个线程中调用,因此执行queueSearchByType :withData: 在视图仍在加载和设置时被调用。
如果用户选择选项卡 2(因此初始化子视图),然后选择选项卡 1 和一个项目,则它可以填充搜索显示文本。
我不能只更改选项卡的顺序,以便 tab2 位于第一个,因此将其子视图加载到导航栏,因为项目规范首先是类别搜索。
我错过了一些非常简单的事情吗?我需要做的是等待第二个选项卡完全显示,然后再调用queueSearchByType:withData: - 有没有办法做到这一点?
目前,我已经实现了队列搜索,检查队列搜索方法,这似乎有点啰嗦。
I'm having a problem getting a UISearchDisplay's text value to be set programatically on load of the view by another view and my question is have I overcomplicated my problem and missed something or am I on the right track of thinking.
Here's the situation: I have a UITabBarController as my root view, there are 2 tabs, both have a UINavigationController setup so I can push views as needed.
Tab 1 has a UITableViewController which is populated with a list of categories.
Tab 2 has a MapView in it's main view but I have done a custom UINavigationItem view to put various buttons and a UISearchDisplay on the rightBarButtonitem area.
The mapview layout and custom navigation item are stored in the same nib as two separate view objects. In Tab 2's viewDidLoad(), I initialise the rightBarButtonItem programatically with:
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:buttonBar];
self.navigationItem.rightBarButtonItem = btnItem;
[btnItem release];
Everything fires up, buttonBar is wired up to an IBOutlet as searchWhat and I can talk to this object from within the mapview's controller class.
If the user is in Tab 1 and taps a cell, I want it to switch to Tab 2 and populate the searchWhat.text and then execute the search code as if someone had typed in the search themselves.
What i'm having trouble with is the order of load and populate on a view.
I can access the 2nd tab from the 1st without any problem and get it to appear with something like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Quick Category cell tapped at row %d", indexPath.row);
self.tabBarController.selectedIndex = 1; // change to the search view controller
//[self.tabBarController.selectedViewController viewDidAppear:YES];
UINavigationController *nav = (UINavigationController *)self.tabBarController.selectedViewController;
SearchViewController *srch = [nav.viewControllers objectAtIndex:0];
//NSLog(@"%@", [srch description]);
[srch queueSearchByType:kSearchTypeQuickCategories withData:[catList objectAtIndex:indexPath.row]];
[srch viewDidAppear:YES];
}
Don't worry about catList and SearchViewController, they exist and this bit works to switch tabs.
Here's the problem though, if the user starts the application and selects an item in tab 1, tab 2 appears but the values of the search display text don't get set - because viewDidLoad and viewDidAppear are called in another thread so the execution of queueSearchByType:withData: gets called while the view is still loading and setting up.
If the user selects tab 2 (therefore initialising the subview) and then selects tab 1 and an item, it can populate the search display text.
I can't just change the order of the tabs so that tab2 is first and therefore loads it's subviews to the navigation bar as the project specification is category search first.
Have I missed something very simple? What I need to do is wait for the second tab to fully appear before calling queueSearchByType:withData: - is there a way to do this?
At the moment, i've implemented a queue the search, check for a queue search approach, this seems to be a bit long winded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我不喜欢回答我自己的问题,但看来我的担心是正确的,基本上如果你想要一个 UINavigationItem 是一个自定义视图(即,在导航控制器上放置一个搜索栏和各种其他按钮)并成为能够切换到选项卡栏控制器上的另一个选项卡并填充它们,然后您需要将子视图放入它自己的类中,该类是 UIViewController 的子类,然后使委托成为您的朋友(它已经是),我已经提供了一个例子任何人都需要重复我已经放在我的博客上的内容。
http://www.jamesrbrindle.com/developer/ios-developer/howto-add-a-custom-uinavigationitem-to-a-uinavigationcontroller-with-delegation.htm
如果有人不同意并认为这可以更简单,请告诉我或评价这篇文章
Ok, I don't like answering my own question but it appears my fears were right, basically if you want a UINavigationItem that is a custom view (ie, to put a search bar and various other buttons up on the nav controller) and be able to switch to and populate them from another tab on a tab bar controller, then you need to put the subview in it's own class which is a subclass of UIViewController and then make delegation your friend (which it already is), i've provided an example in case anybody needs to repeat it which i've put on my blog HERE.
http://www.jamesrbrindle.com/developer/ios-developer/howto-add-a-custom-uinavigationitem-to-a-uinavigationcontroller-with-delegation.htm
If anyone disagrees and thinks this can be simpler, please let me know or rate this post