Ios 自定义后退按钮选择器未触发
我有两个UIViewController,让我们在UINavigationController中称它们为vcA和vcB 我希望vcB有一个自定义的backbutton来触发一些代码,目标是做一些自定义动画Ios 自定义后退按钮选择器未触发,ios,uinavigationcontroller,back-button,Ios,Uinavigationcontroller,Back Button,我有两个UIViewController,让我们在UINavigationController中称它们为vcA和vcB 我希望vcB有一个自定义的backbutton来触发一些代码,目标是做一些自定义动画 - (void)addAction:(id)sender { UIViewController *vcA = [UIViewController alloc] init]; [[self retain] autorelease]; [self.navigati
- (void)addAction:(id)sender
{
UIViewController *vcA = [UIViewController alloc] init];
[[self retain] autorelease];
[self.navigationController pushViewController: vcA animated: NO];
[UIView transitionWithView:self.navigationController.view duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
[self.navigationController popViewControllerAnimated:NO];
}
在vcA中,我输入以下代码:
UIViewController *vcB = [UIViewController alloc] init]
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(handleBack)];
self.navigationItem.backBarButtonItem = custombackBackButton;
[self.navigationController pushViewController: vcB animated: YES];
然后我在vcA和vcB中添加了以下代码:
-(void) handleBack
{
NSlog(@"Going back to vcA");
}
handleback方法从未被调用。有什么提示吗
谢谢
尼古拉,你做错了。首先推到vcB,然后在vcBviewDidLoad方法中输入此代码
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(handleBack)];
self.navigationItem.backBarButtonItem = custombackBackButton;
并将其放入vcB中:
-(void) handleBack{
NSlog(@"Going back to vcA");
}
使用William Jockusch提供的技巧 而且还按照
- 如果新的顶级视图控制器具有自定义左栏按钮项,则将显示该项。要指定自定义左栏按钮项,请设置视图控制器导航项的属性
- 如果顶级视图控制器没有自定义的左栏按钮项,但上一个视图控制器的导航项在其BackBarButtonim属性中具有有效项,则导航栏将显示该项
这里提到,如果要为“后退”按钮指定自定义图像或标题,可以指定自定义栏按钮项(带有自定义标题或图像),但它会忽略自定义视图。因此您的选择器没有调用。您不能修改BackBarButtonims操作,它是执行默认(后退)操作,您应该在LeftBarButtonims中执行自定义操作 在vcBs类中添加以下内容:
- (void)viewWillAppear:(BOOL)animated
{
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(handleBack)];
self.navigationItem.leftBarButtonItem = customBackButton;
}
- (void)handleBack
{
NSLog(@"back");
}
别误会我的意思,但我认为你需要弄清楚UINavigationController的push-pop 导航控制器通过在控制器的视图堆栈上推/弹出视图来管理视图。推送项目时,当前视图从屏幕向左滑动,新视图从右侧滑动。当然,这些动画可以根据您的意愿进行更改 我认为这是你完全需要的 把这个放在你想从vcA中推出vcB的vcA中
UIViewController *vcB = [UIViewController alloc] init];
[self.navigationController pushViewController: vcB animated: NO];
[UIView transitionWithView:self.navigationController.view
duration:0.8
options:UIViewAnimationOptionTransitionFlipFromRight
animations:nil
completion:nil];
在vcB中,您可以制作一个巴布托石
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:@"BackToVcA"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
In-addActionMethod:您可以将下面的代码放在需要的动画中导航回来
- (void)addAction:(id)sender
{
UIViewController *vcA = [UIViewController alloc] init];
[[self retain] autorelease];
[self.navigationController pushViewController: vcA animated: NO];
[UIView transitionWithView:self.navigationController.view duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
[self.navigationController popViewControllerAnimated:NO];
}
这不管用。要更改vcB backButton的标题,您必须从vcA而不是vcB viewDidLoad进行更改。我知道这对程序员来说不是很友好,但要更改vcB backButton的标题,您必须从vcA而不是vcB viewDidLoad进行更改。只有将customBackButton设置为self.navigationItem.leftBarButtonItem(矩形)时,此技巧才有效。我希望自定义方法由一个真正的后退按钮(箭头形状)调用,您只能在vcA中更改它的标题,但永远不会调用选择器。如果您想更改后退按钮的标题,请在vcA中放置此选项-(void)视图将消失:(BOOL)已设置动画的{self.navigationItem.title=@“Back”}并且在vcB put中-(void)视图将出现:(BOOL)已设置动画的{self.navigationItem.title=@“vcB”}