ios - 从单独的类推送新的 UIViewController

标签 ios objective-c delegates uinavigationcontroller appdelegate

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

类似地,我有在单独的类(SliderMenuViewController)中控制 slider 的表格 View 的方法。

我正在寻找一种方法,让 slider (搜索框或表格 View 单元格)能够告诉 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];
}

但它再次只写入日志,而不推送 View Controller 。

我在 Google 和 SO 上进行了广泛的搜索,但没有找到任何有用的东西。 This question和我的类似,但是没有找到合适的答案。我知道答案可能涉及委托(delegate),但我无法解决这个问题。

重要说明:应用中几乎每个 ViewController 都可以使用此 slider ,这意味着我实现的任何解决方案都必须能够为每个类推送一个新的 ViewController。这就是为什么我不能使用像 this one 这样的解决方案(我必须将 NavigationDelegate 代码输入到每个 ViewController 中,这在像我这样大的应用程序中不起作用)。

预先感谢您的帮助。

最佳答案

我不相信这是最好的解决方案,但我能够使用通知来实现此目的。对于任何感兴趣的人,这就是我所做的:

步骤 1

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

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

步骤 2

然后,当从 slider 执行搜索时,我需要触发通知。 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];
}

这就是我如何设法从一个单独的类(在本例中是应用程序委托(delegate),但我也让它在其他类中工作)推送新的 viewController。

最后一步(可选)

我的 slider 可以从应用程序中的任何位置访问,因此我没有从 RootViewController 中的通知中取消注册(这意味着即使用户已被推送到另一个 viewController,这些方法也将继续触发)。如果您不想要此功能,请确保使用以下代码取消注册这些通知(它将位于 RootViewController 中):

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

就像我说的,我并不完全相信这是最好的方法。如果您有其他喜欢的解决方案,请随时发布。

关于ios - 从单独的类推送新的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14690041/

相关文章:

iphone - 在sqlite中使用行id比较两个表

ios - 如何在 CollectionView 中选择单个单元格并取消选择所有其他单元格

c# - 创建 C# Func<> 类型别名

ios - Delegate vs Unwind Segue 将数据传递给父场景

c++ - 在 xcode 的调试器中查看 IL2CPP 的 string_t 的内容

ios - Mapbox:仅当注释在屏幕上可见时才添加注释

IOS OCR tesseract 在为 nil 并使用 ACR 后不释放内存

iphone - 如何将核心数据与引用的文件同步?

objective-c - 关于实例变量和父类(super class)的奇怪错误

objective-c - 如何在不同的类中设置委托(delegate)