0

我正在尝试根据用户输入在 NavigationBar 中编辑按钮,我需要更改按钮指向的操作和标题。

看来我不能在运行时这样做。

CalendarMonthViewParent.h

#import <UIKit/UIKit.h>

@interface CalendarMonthViewParent : UIViewController

-(void) callChildChange; // This is called from another viewController, telling it to change the button

@property (strong, nonatomic) IBOutlet UINavigationItem *skemaNavigationItem; // this is the Navigation Item in the Storyboard, it has no right btn by default

@end

CalendarMonthViewParent.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // on viewDidLoad i set up the button with an action, via the outlet, this works fine

    UINavigationItem *thisNavBar = [self skemaNavigationItem];

    UIBarButtonItem *insertBtn = [[UIBarButtonItem alloc] initWithTitle:@"Indsæt" style:UIBarButtonItemStyleBordered target:self action:@selector(insertSkemaItem:)];

    thisNavBar.rightBarButtonItem = insertBtn;

}


- (void)callChildChange {

...

// this method is called from another view controller, based on some user input
// this method is supposed to change the button

UINavigationItem *thisNavBar = [self skemaNavigationItem];

// i fetch the button like i did it in viewDidLoad, but i cannot preform operations on it here

// thisNavBar is nil when called here, but it is not nil when called in viewDidLoad?

...
}

如果我的方法不正确且不可能,我将不胜感激任何可行的解决方案

编辑: 我的 UIViewController,不在 UINavigationController 内,我只是在使用 UINavigationbar

4

1 回答 1

0

UIViewController已经有一个navigationItem在视图控制器位于导航控制器中时自动使用的属性,因此似乎不太可能skemaNavigationItem被使用。用于创建和设置栏按钮项目的代码看起来没问题(除了它被设置的位置)。如果要编辑需要创建并设置新的条形按钮项,请不要实际尝试编辑现有项。


如果skemaNavigationItem为 nil,则要么您没有连接插座,要么您在错误的时间调用控制器(在加载视图之前 - 这可能是因为您正在创建一个新的视图控制器而不是使用屏幕上的现有控制器)。

于 2013-08-08T20:06:57.393 回答