在 TableViewController 的 pushViewController 方法中 navigationController = null (popover)
Posted
技术标签:
【中文标题】在 TableViewController 的 pushViewController 方法中 navigationController = null (popover)【英文标题】:navigationController = null in TableViewController's pushViewController method (popover) 【发布时间】:2013-10-24 13:06:42 【问题描述】:首先,对不起我的英语。 我正在尝试使用导航控制器进行iPad的应用程序,该导航控制器在选择“下一步”按钮时按下视图控制器。但我也想要一个弹出框,从导航栏中的按钮调用,允许用户从一个视图控制器“跳转”到另一个视图控制器,使用 tableView:didSelectRowAtIndexPath: 和 pushViewController:animated: 方法推送它,但是它不工作。
总结:
标签栏 -> 在 FirstViewController 和 SecondViewController 之间切换(工作正常)
导航栏(按钮 Next) -> 在 SecondViewController、FirstSlideController 和 SecondSlideController 之间切换(也不错)
Popover -> 用户选择 SecondViewController、FirstSlideController 或 SecondSlideController(这就是问题所在!)
代码:
AppDelegate
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navigationController1, navigationController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
TableViewController(popover) 的 didSelectRowAtIndexPath 方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
if(indexPath.row == 0)
FirstSlideController *detailViewController = [[FirstSlideController alloc] initWithNibName:@"FirstSlideController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
else if(indexPath.row == 1)
SecondSlideController *detailViewController = [[SecondSlideController alloc] initWithNibName:@"SecondSlideController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
else
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
SecondViewController(使用 maros 建议的委托)
-(void) showPopover:(id) sender
TableViewController *PopoverView = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil];
self.popOver = [[UIPopoverController alloc] initWithContentViewController:PopoverView];
self.popOver.delegate = self;
[self.popOver presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedArrowDirections: UIPopoverArrowDirectionUp animated: YES];
我试图打印 self.navigationController 并且它说它是空的。如有任何帮助,我将不胜感激。
谢谢。
【问题讨论】:
【参考方案1】:Presnented UIPopoverController 不会被推送到它所呈现的视图控制器中的导航堆栈上。它是一个单独的视图控制器。 因此popover里面的navigationController是nil。
我建议你创建一个委托 MyNavigationPopoverDelegate(创建弹出框的类 (PopoverController)。将它的实例作为委托传递给 TableViewController.
用户点击popover内的某个按钮后,调用delegate的方法处理按钮点击(myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex)。
那么也许解散委托?
最后随心所欲地改变导航! :)
@protocol MyNavigationPopoverDelegate
- (void) myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex;
@end
@interface TableViewController : UITableVieController // your viewController in popover
... // your code
@property (nonatomic, weak) NSObject <MyNavigationPopoverDelegate> * delegate;
... // your code
@end
@implementation TableViewController
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[self.delegate myNavigationPopover:self clickedButtonAtIndex:indexPath.row];
...
@end
// defines that SecondViewController implements the delegate's method
@interface SecondViewController <MyNavigationPopoverDelegate> : UIViewController
// your code
@end
// code where you presenting popover
@implementation SecondViewController
// This is the method that is executed after your button press and it is responsible for presenting a popover
- (void) presentPopover
...
myPopover.delegate = self; // setting the delegate
[myPopover presentPopoverFromXXX ...]; // however you present it
...
- (void) myNavigationPopover:(UIPopoverController*)popover clickedButtonAtIndex:(NSInteger)buttonIndex
UINavigationController *currentNavigationController = ; // get the navigation controller from the tab bar
if(buttonIndex == 0)
FirstSlideController *detailViewController = [[FirstSlideController alloc] initWithNibName:@"FirstSlideController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
else if(buttonIndex == 1)
SecondSlideController *detailViewController = [[SecondSlideController alloc] initWithNibName:@"SecondSlideController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
else
SecondViewController *detailViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[currentNavigationController pushViewController:detailViewController animated:YES];
@end;
【讨论】:
对不起,我不知道如何使用委托,但我试过了,它说 [self.delegate myNavigationPopover:self clickedButtonAtIndex:indexPath.row];它说 TableViewController 类中没有一个名为 delegate 的属性 对了,我把delegate的实现放在TableViewController.h,对吗?谢谢! :) @user2303633 一些教程会帮助你,例如link。您应该首先将委托定义为 PopoverController 上的属性,然后您可以在那里传递实例。委托的实现应该在您创建 PopoverController 的类上完成。 看,我在 SecondViewController 中有一个方法,我在其中实现了按下弹出框按钮的方法,它使用 TableViewController 对象的内容初始化弹出框(我现在把这个方法放在问题中)。那么我该如何做这部分“myPopover.delegate = self; // 设置委托”?因为委托没有 myPopover 属性... 感谢您的教程...我读了但还是没明白 =/以上是关于在 TableViewController 的 pushViewController 方法中 navigationController = null (popover)的主要内容,如果未能解决你的问题,请参考以下文章
在一个 TableViewController 中显示两组结果
登录->第一个视图(tableviewcontroller)