覆盖backBarButtonItem的默认行为

时间:2011-06-03 10:32:21

标签: iphone objective-c uiview uinavigationcontroller

我正面临着在视图之间导航的问题,基本上我有3个观点:

  1. RootView
  2. view1
  3. 视图2
  4. 我想要做的是当我点击View2的后退按钮时,我希望加载RootView而不是加载view1(这是默认设置)。

    ..我在推送view2ViewController之前在My view1.m中编写了以下代码,代码:

     UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"Back" target:self action:@selector(someMethod:)];
    self.navigationItem.backBarButtonItem = back;
    -(void)someMethod:(id)sender{ [self.navigationViewCOntroller popToRootViewControllerAnimated:YES] }
    

    以上代码无效

2 个答案:

答案 0 :(得分:0)

你问题的后半部分不清楚。

试试这个 [self.navigationController popToRootViewControllerAnimated:YES];

希望这对你有用。通过这种方式,您可以从View2导航到根视图。

答案 1 :(得分:0)

试试这个:

转到视图:

Aview *aview =[[Aview alloc] initWithNibName:nil bundle:nil];
    settings.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentModalViewController:settings animated:YES];

回去:

[self dismissModalViewControllerAnimated:YES];

所以在你的情况下:

-(void)someMethod:(id)sender{ 
 Rootview *rootview =[[Rootview alloc] initWithNibName:nil bundle:nil];
        settings.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentModalViewController:settings animated:YES];
}