我正在尝试根据用户输入编辑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
答案 0 :(得分:0)
UIViewController
已经有一个属性navigationItem
,当视图控制器位于导航控制器中时会自动使用该属性,因此skemaNavigationItem
似乎不太可能被使用。用于创建和设置条形按钮项的代码看起来不错(除了设置为的位置)。如果要编辑需要创建和设置新项目的条形按钮项目,请不要尝试编辑现有项目。
如果skemaNavigationItem
为零,则说明您没有连接插座或者您在错误的时间调用控制器(在视图加载之前 - 这可能是因为您正在创建一个新的视图控制器而不是使用屏幕上的现有。)