2010-04-15 114 views
4

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

这里是我的代码至今:

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

你在哪里定义attendenceButton? – Daniel 2010-04-15 15:41:32

回答

0

在viewDidLoad方法只使用

self.navigationItem.rightBarButtonItem = self.editButtonItem; 

或什么是你的按钮

希望它有助于

4

的UINavigationController期望该按钮被连接到视图控制器以查看其当前的视图y显示(当您使用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; 
} 

您可能还可以通过从类的外部设置rightBarButtonItem您aStudentsViewController做到这一点,但我认为你在导航项目对象可用时会遇到问题。它会是这样的,虽然:

aStudentsViewController.navigationItem.rightBarButtonItem = myButton; 

我不认为它会一直工作到后酥料饼造成的一切加载,但我真的不知道这一点。最好的方法是把它放在你的aStudentsViewController对象的viewDidLoad中。

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]; 

希望它可以帮助亚..

相关问题