首先,对不起我的英语。 我正在尝试为 iPad 开发一个带有导航 Controller 的应用程序,该导航 Controller 会在选择“下一步”按钮时推送 View Controller 。但我也想有一个弹出窗口,从导航栏中的按钮调用,允许用户从一个 View Controller “跳转”到另一个 View Controller ,使用 tableView:didSelectRowAtIndexPath: 和 pushViewController:animated: 方法推送它,但是它不工作。
总结:
标签栏 -> 在 FirstViewController 和 SecondViewController 之间切换(工作正常)
导航栏(按钮Next) -> 在SecondViewController、FirstSlideController 和SecondSlideController 之间切换(也不错)
弹出框 -> 用户选择 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 建议的委托(delegate))
-(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,它说它为空。如果有任何帮助,我将不胜感激。
谢谢。
最佳答案
Presnented UIPopoverController 没有被推送到它被呈现的 View Controller 中的导航堆栈上。它是一个单独的 View Controller 。 因此,弹出框内的 navigationController 为 nil。
我建议您创建一个委托(delegate) MyNavigationPopoverDelegate(创建弹出窗口的类 (PopoverController)。将它的实例作为委托(delegate)传递给 TableView Controller 。
当用户点击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;
关于ios - TableViewController 的pushViewController 方法中的navigationController = null (popover),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19566817/