我有一个导航栏,其中包含一个导航项,其中包含2个栏按钮,这些是在故事板中创建的,我想在运行时更改其中一个按钮,现在可以正常工作:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UINavigationItem *thisNavBar = [self myNavigationItem];
thisNavBar.rightBarButtonItem = nil; // this works, it gets removed
UIBarButtonItem *insertBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(insertSkemaItem:)];
thisNavBar.rightBarButtonItem = insertBtn; // this also works, it sets the btn
}
现在,在另一个由另一个控制器调用的方法中,它不起作用
- (void)callChildChange {
...
// remove old btn
UINavigationItem *thisNavBar = [self skemaNavigationItem];
thisNavBar.rightBarButtonItem = nil; // does not work?
}
该方法没有任何问题,它运行得很好,但nav btn项目没有被删除?
skemaNavigationItem是一个导航项,在.h文件中声明,它链接我通过故事板制作的导航项。
答案 0 :(得分:0)
您的UI项目需要在头文件(.h)中添加到您的代码中(通过ctrl-dragging),以便可以从其他类/视图控制器公开访问它们。
假设您已完成此操作,最好使用
隐藏UI项目relevantClass.yourViewObject.hidden = YES;
或者如果你真的需要将其删除,
[relevantClass.yourViewObject.view removeFromSuperView];
<强>编辑强>
更改目标方法的选项:
声明@property (nonatomic, assign) BOOL myButtonWasPressed;
和:
- (IBAction) myButtonPressed
{
if (!self.myButtonWasPressed)
{
// This code will run the first time the button is pressed
self.myButton.text = @"New Button Text";
self.myButtonWasPressed = YES;
}
else
{
// This code will run after the first time your button is pressed
// You can even set your BOOL property back, and make it toggleable
}
}
或
- (IBAction) myButtonWasPressedFirstTime
{
// do what you need to when button is pressed then...
self.myButton.text = @"New Button Text";
[self.myButton removeTarget:self action:@selector(myButtonPressedFirstTime) forControlEvents:UIControlEventTouchUpInside];
[self.myButton addTarget:self action:@selector(myButtonPressedAgain) forControlEvents: UIControlEventTouchUpInside];
}
- (IBAction) myButtonWasPressedAgain
{
// this code will run the subsequent times your button is pressed
}