我正在尝试将UIBarButtonItems添加到显示为弹出窗口的导航控制器中。我似乎无法添加按钮,我想知道是否有人可以帮助我。
这是我到目前为止的代码:
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];
答案 0 :(得分:4)
UINavigationController期望按钮被附加到视图控制器以用于当前显示的视图(当您使用UINavigationController导航时,该按钮特定于每个视图)。 UIViewController有一个navigationItem属性,你需要附加你的按钮,通常在显示的视图控制器的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中。
答案 1 :(得分:0)
只需在viewDidLoad方法中使用
self.navigationItem.rightBarButtonItem = self.editButtonItem;
或任何按钮
希望有所帮助
答案 2 :(得分:0)
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"All Present"
style:UIBarButtonItemStylePlain
target:self
action:@selector(makeAllPresent:)];
UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
vc.navigationItem.title = @"Your Title";
vc.navigationItem.rightBarButtonItem =myButton;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
navigationController.navigationBar.hidden = NO;
attendancePopoverController = [[UIPopoverController alloc] initWithContentViewController:navigationController];
希望它能帮到你..