Ios TableViewController';中的navigationController=null;s pushViewController方法(popover)

Ios TableViewController';中的navigationController=null;s pushViewController方法(popover),ios,ipad,uinavigationcontroller,uitableview,popover,Ios,Ipad,Uinavigationcontroller,Uitableview,Popover,首先,对不起我的英语。 我正在尝试为iPad开发一个应用程序,其中有一个导航控制器,当选择“下一步”按钮时,它会按下视图控制器。但我还希望有一个弹出窗口,从导航栏中的按钮调用,它允许用户从一个视图控制器“跳转”到另一个视图控制器,使用tableView:DidSelectRowatineXpath:和pushViewController:animated:方法按下它,但它不起作用 总结: 选项卡栏->在FirstViewController和SecondViewController之间切换(工作

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

总结:

选项卡栏->在FirstViewController和SecondViewController之间切换(工作正常

导航栏(下一步按钮)->在SecondViewController、FirstSlideControl和SecondSlideControl之间切换(也很好

Popover->用户选择SecondViewController、FirstSlideControl或SecondSlideControl(问题出在这里!

代码:

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)的DidSelectRowatineXpath方法:

- (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推荐代表)

我试着打印self.navigationController,它说它是空的。我非常感谢你的帮助


谢谢。

预安装的UIPopoverController不会被推到显示它的视图控制器中的导航堆栈上。它是一个单独的视图控制器。 因此,popover中的navigationController为零

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

用户单击popover中的某个按钮后,调用代理的方法来处理按钮单击(myNavigationPopover:(UIPopopOvercontroller*)popover ClickedButtonIndex:(NSInteger)buttonIndex

那么也许可以解雇代表

最后,按照您的意愿更改导航!:)

@协议MyNavigationPopoverDelegate
-(void)myNavigationPopover:(UIPopoverController*)popover单击按钮索引:(NSInteger)按钮索引;
@结束
@界面TableViewController:UITableVieController//您在popover中的viewController
... // 你的代码
@属性(非原子,弱)NSObject*委托;
... // 你的代码
@结束
@TableViewController的实现
...
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
[self.delegate myNavigationPopover:self-ClickedButtonIndex:IndexXPath.row];
}
...
@结束
//定义SecondViewController实现委托的方法
@界面SecondViewController:UIViewController
//你的代码
@结束
//你在哪里展示popover的代码
@第二视图控制器的实现
//这是在按下按钮后执行的方法,它负责显示弹出窗口
-(无效)当前POPOVER{
...
myPopover.delegate=self;//设置委托
[myPopover PresentPopover fromXXX…];//无论您如何呈现它
...
}
-(无效)myNavigationPopover:(UIPopoverController*)popover单击按钮索引:(NSInteger)按钮索引
{
UINavigationController*currentNavigationController=;//从选项卡栏获取导航控制器
如果(按钮索引==0){
FirstSlideControl*detailViewController=[[FirstSlideControl alloc]initWithNibName:@“FirstSlideControl”捆绑包:nil];
[currentNavigationController pushViewController:detailViewController动画:是];
}
否则如果(按钮索引==1){
SecondSlideControl*detailViewController=[[SecondSlideControl alloc]initWithNibName:@“SecondSlideControl”捆绑包:nil];
[currentNavigationController pushViewController:detailViewController动画:是];
}
否则{
SecondViewController*detailViewController=[[SecondViewController alloc]initWithNibName:@“SecondViewController”捆绑包:nil];
[currentNavigationController pushViewController:detailViewController动画:是];
}
}
@结束;

很抱歉,我不知道如何使用委托,但我尝试过了,它说[self.delegate myNavigationPopover:self-clickedButtonIndex:indexPath.row]有一个错误;它说TableViewController类中没有名为delegate的属性顺便说一下,我将委托的实现放在TableViewController.h中,对吗?谢谢!:)@user2303633一些教程将对您有所帮助,例如。您应该首先将委托定义为PopoverController上的属性,然后可以在那里传递实例。委托的实现应该在创建PopoverController的类上完成。请参阅,我在SecondViewController中有一个方法,在该方法中我实现了按下popover按钮的方法,该方法使用TableViewController对象的内容初始化popover(我现在讨论这个方法)。那么,我如何执行“myPopover.delegate=self;//设置委托”这一部分呢?因为代理没有myPopover属性…谢谢您的教程。。。我看了,但还是没看懂=/
-(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];
}
@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;