UIBarbuttonItem的动作不叫ios

时间:2015-05-26 11:42:43

标签: ios uibarbuttonitem

我试图在按下该控制器的后退按钮时调用UIBarButtonItem。这是我的代码。我已将初始化放在viewDidLoadviewWillAppear中,但方法是没有被召唤。

UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:(@selector(backToViewController:))];

self.navigationItem.backBarButtonItem = exampleButton;

-(void)backToViewController:(UIBarButtonItem*)sender
 {
     [self.navigationController popToRootViewControllerAnimated:YES];
 }

1 个答案:

答案 0 :(得分:1)

IT部门不会将backBarButtonItem与您的行添加为:

self.navigationItem.backBarButtonItem = exampleButton;

您必须提供leftbarbutton

UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithTitle:@"  " style:UIBarButtonItemStylePlain target:self action:(@selector(backToViewController:))];
// As you are going with  "  ", there is no text, that means you have to click at left side randomly, to get affect.
self.navigationItem.leftBarButtonItem = exampleButton;

更新1

您可以根据需要为自己创建的图像提供自定义按钮

UIButton *barCutomBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
[barCutomBtn setImage:[UIImage imageNamed:@"backImage.png"] forState:UIControlStateNormal];
[barCutomBtn addTarget:self action:@selector(backToViewController:)forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithCustomView:barCutomBtn];
self.navigationItem.leftBarButtonItem = exampleButton;