0

首先,对不起我的英语。我正在尝试使用导航控制器为iPad执行应用程序,该应用程序在选择“ Next”按钮时将视图控制器按下。但我也想要一个弹出框,从导航栏中的按钮调用,允许用户从一个视图控制器“跳转”到另一个视图控制器,使用 tableView:didSelectRowAtIndexPath: 和 pushViewController:animated: 方法推送它,但是它不工作。

概括:

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

导航栏(按钮 Next) -> 在 SecondViewController、FirstSlideController 和 SecondSlideController 之间切换(也不错

Popover -> 用户选择 SecondViewController、FirstSlideController 或 SecondSlideController(这就是问题所在!

代码:

应用委托

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 它说它是空的。我会很感激任何帮助。

谢谢你。

4

1 回答 1

1

Presnented UIPopoverController 不会被推送到它所在的视图控制器中的导航堆栈上。它是一个单独的视图控制器。因此弹出框内的 navigationController 为 nil。

我建议您创建一个委托MyNavigationPopoverDelegate(创建弹出框(PopoverController)的类。将它的实例作为委托传递给TableViewController

用户点击弹出框内的某个按钮后,调用委托的方法来处理按钮点击(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;
于 2013-10-24T13:36:15.897 回答