tableViewController的pushViewController方法中的navigationController = null(popover)

时间:2013-10-24 13:06:43

标签: ios ipad uinavigationcontroller uitableview popover

首先,对不起我的英语。 我正在尝试使用导航控制器为iPad做一个应用程序,当选择一个按钮“下一步”时,它会推动视图控制器。但我也希望有一个弹出窗口,从导航栏中的一个按钮调用,允许用户从一个视图控制器“跳转”到另一个视图控制器,用tableView:didSelectRowAtIndexPath:和pushViewController:animated:方法推送它,但是它不起作用。

摘要:

标签栏 - >在FirstViewController和SecondViewController之间切换(工作得很好

导航栏(按钮下一步) - >在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,它说它是null。我很感激任何帮助。

谢谢。

1 个答案:

答案 0 :(得分:1)

预定义的UIPopoverController不会被推送到视图控制器中的导航堆栈上。它是一个单独的视图控制器。 因此,popover中的navigationController是nil。

我建议你创建一个委托 MyNavigationPopoverDelegate (创建一个popover的类( 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;