0

我目前为我的应用程序编写了一个滑块(类似于 Facebook 应用程序)。滑块顶部是一个搜索框,控制搜索功能的方法也在应用程序委托中。

同样,我在一个单独的类 (SliderMenuViewController) 中有控制滑块表格视图的方法。

我正在寻找一种方法让滑块(搜索框或表格视图单元格)能够告诉 RootViewController(或当前可见的任何 viewController)推送一个新的 ViewController(在 UINavigationController 内)。

这就是我试图做的(这段代码在 AppDelegate 中):

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(@"Searching for: \"%@\"",searchBar.text);
[searchBar resignFirstResponder];
IndexAndSearch *vc = [[IndexAndSearch alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}

但它不起作用(它写入日志,但不推送新的 ViewController)。我还尝试向 RootViewController 发送消息,如下所示:

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(@"Searching for: \"%@\"",searchBar.text);
[searchBar resignFirstResponder];
RootViewController *vc = [[RootViewController alloc]initWithNibName:nil bundle:nil];
[vc performSearchFromDelegateSlider];
}

在 RootViewController 的实现文件中使用以下代码:

-(void)performSearchFromDelegateSlider{
NSLog(@"Searching");
IndexAndSearch *vc = [[IndexAndSearch alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}

但又一次,它只写入日志,而不是推送 viewController。

我在 Google 和 SO 上看了很多遍,但找不到任何有用的东西。这个问题与我的类似,但没有任何合适的答案。我知道答案可能涉及委派,但我无法解决这个问题。

重要提示:应用程序中几乎每个 ViewController 都可以使用此滑块,这意味着无论我实施什么解决方案,都必须能够为每个类推送一个新的 ViewController。这就是为什么我不能使用这样的解决方案我必须将 NavigationDelegate 代码输入到每个 ViewController 中,这在我这样大的应用程序中不起作用)。

提前感谢您的帮助。

4

2 回答 2

1

我不相信这是最好的解决方案,但我能够使用通知来完成这项工作。对于任何感兴趣的人,这是我所做的:

第1步

第一步是在 RootViewController 的 viewDidLoad 方法中注册通知:

-(void)viewDidLoad{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNavSliderSearchNotification:) name:@"navSliderSearchNotification" object:nil];
}

第2步

然后,当从滑块执行搜索时,我需要触发通知。searchBar 代码位于我的 AppDelegate 中,如下所示:

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    //Write to the log
    NSLog(@"Searching for: \"%@\"",searchBar.text);
    //Dismiss the keyboard
    [searchBar resignFirstResponder];
    //Post the notification (to be used by the RootViewController
    [[NSNotificationCenter defaultCenter] postNotificationName:@"navSliderSearchNotification" object:self];
}

第 3 步

然后我需要编写didReceiveNavSliderSearchNotification类(在发布和接收navSliderSearchNotification通知时将在 RootViewController 中调用):

-(void)didReceiveNavSliderSearchNotification:(NSNotification *) notification {

    if ([[notification name] isEqualToString:@"navSliderSearchNotification"])
    NSLog (@"Successfully received the search notification!");    

    //Push the next ViewController when the *navSliderSearchNotification* is received
    IndexAndSearch *vc = [[IndexAndSearch alloc]initWithNibName:@"IndexAndSearch" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

And that is how I managed to push new viewControllers from a separate class (in this case the App Delegate, but I also have it working from other classes as well).

Final step (optional)

My slider is accessible from everywhere in the app, so I did not unregister from my notifications in the RootViewController (meaning these methods will continue to fire even if the user has been pushed to another viewController). If you do not want this functionality, make sure to unregister from these notifications using the following code (it would go in the RootViewController):

-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"navSliderSearchNotification" object:nil];
}

Like I said, I am not entirely convinced this is the best method. If you have another solution that you prefer, please feel free to post it.

于 2013-02-05T02:58:53.690 回答
0

您是否尝试过这样做:

在您的 AppDelegate 中创建一个 UIViewController 变量,该变量始终引用屏幕上的当前 UIViewController(您可能需要在每次创建视图控制器时设置当前控制器)一旦这一切都完成了。在您的 AppDelegate 中使用

[self.currentViewController.navigationController pushViewController:anotherNewViewController animated:YES];
于 2013-02-04T15:43:03.573 回答