iphone - 将 BarButtonItem 添加到 Popover 中的导航 Controller

标签 iphone objective-c ipad

我正在尝试将 UIBarButtonItems 添加到显示为弹出窗口的导航 Controller 。我似乎无法添加按钮,我想知道是否有人可以帮助我。

这是我目前的代码:

UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:aStudentsViewController];

[navigationController setToolbarHidden:NO];
[navigationController setNavigationBarHidden:NO];

UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"All Present"
                                                            style:UIBarButtonItemStylePlain
                                                           target:self
                                                           action:@selector(makeAllPresent:)];  



[navigationController.navigationItem setRightBarButtonItem:myButton];

attendancePopoverController = [[UIPopoverController alloc] initWithContentViewController:navigationController];
[attendancePopoverController setDelegate:self];


//activeBarButtonItem = sender;
[attendancePopoverController presentPopoverFromBarButtonItem:attendanceButton
                                permittedArrowDirections:UIPopoverArrowDirectionAny
                                                animated:YES];

最佳答案

UINavigationController 期望按钮附加到当前显示的 View 的 View Controller (当您使用 UINavigationController 导航时,按钮特定于每个 View )。 UIViewController 有一个 navigationItem 属性,您需要在该位置附加按钮,通常在显示的 View Controller 的 viewDidLoad 方法中。

在您的 aStudentsViewController 类中定义一个 viewDidLoad 方法并在那里设置按钮:

- (void)viewDidLoad {
    UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"All Present"
                                                         style:UIBarButtonItemStylePlain
                                                         target:self
                                                         action:@selector(makeAllPresent:)];  
    self.navigationItem.rightBarButtonItem = myButton;
}

您也可以通过在类外设置您的 aStudentsViewController 上的 rightBarButtonItem 来完成此操作,但我认为您将无法确定 navigationItem 对象何时可用。它会是这样的:

aStudentsViewController.navigationItem.rightBarButtonItem = myButton;

我认为在弹出窗口加载所有内容之前它不会起作用,但我不太确定。最好的方法是将它放在 aStudentsViewController 对象的 viewDidLoad 中。

关于iphone - 将 BarButtonItem 添加到 Popover 中的导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2646713/

相关文章:

iphone - 动画火柴人

Iphone:无法从 UITabBarController 加载模态视图

ios - 在 NSURLSession 上设置一个计时器

iphone - 当在 TableView 中选择单元格时,我应该能够在进入另一个 View 之前为单元格设置动画

ios - 如何更新uitableviewcell中的约束动画

iphone - 如何从url中获取json数据

ios - 框架正确导入但仍然出现 _OBJC_CLASS_ 编译器错误

iphone - Itunes 连接不检测应用程序 ID

c# - ContentView 中的按钮导致 MonoTouch 运行时崩溃。 Monotouch 4.0 中的错误?

iphone - AFNetworking UIImageView setImageWithURLRequest 返回响应代码 0