推送后如何覆盖导航控制器?

时间:2016-06-13 14:11:47

标签: ios objective-c uinavigationcontroller override pushviewcontroller

我想用自己的后退按钮,标题等覆盖我自己的导航控制器。但是在我按下视图控制器之后,默认显示堆栈中最后一个视图的导航栏,该导航栏是在前一个类中继承的。 / p>

让我们假设这是我的第一个类,它在头文件中继承了NavBarViewController;

//ListingDetailViewController.h
@interface ListingDetailViewController : NavBarViewController

并且,在ListingDetailViewController中,我推动了视图:

//ListingDetailViewController.m
ChatListViewController *chatList= [[ChatListViewController alloc]init];
[self.navigationController pushViewController:chatList animated:YES];

所以这仍然会导致导航控制器与ListingDetailViewController相同,即使我试图在ChatListViewController的viewdidLoad和viewWillAppear中再次覆盖后退按钮,标题。这是ChatListViewController的标题:

//ChatListViewController.h
@interface ChatListViewController : IPChatListViewController

我怎样才能做到这一点?任何帮助深表感谢。谢谢。

2 个答案:

答案 0 :(得分:1)

为此,我使用BaseViewController类。将此代码粘贴到基本控制器中:

if (self.navigationController.viewControllers)
{
    if (self.navigationController.viewControllers.count > 1)
    {
        [self createBackButton];
    }
    else
    {
        //[self createSideBarButton];
    }
}
else
{
    //[self createSideBarButton];
} 

在您的基类中包含此方法:

- (UIBarButtonItem *)createBackButton
{

    UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSeperator.width = 0;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigation_back_button.png"] style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(popViewControllerAnimated:)];

    [item setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSeperator,item, nil];
    return item;
}

如果您确保从这个基础VC中继承View Controllers(而不是导航控制器),它将自动替换后退按钮。

答案 1 :(得分:0)

我通过覆盖viewDidAppear中的按钮找到了解决方案,因为在viewDidLoad之前它会重新使用继承的导航栏。所以,

v -1.0 -1.0 -1.0 1 0 0
v +1.0 -1.0 -1.0 1 0 0
v +1.0 +1.0 -1.0 1 0 0
v -1.0 +1.0 -1.0 1 0 0
v -1.0 -1.0 +1.0 1 0 0
v +1.0 -1.0 +1.0 1 0 0
v +1.0 +1.0 +1.0 1 0 0
v -1.0 +1.0 +1.0 1 0 0

f 1 2 3 4 
f 5 6 7 8 
f 1 2 6 5 
f 2 3 7 6 
f 3 4 8 7 
f 4 1 5 8 

然后:

-(void)viewDidAppear:(BOOL)animated {
[self overwriteBackButton];
}

它被触发:

- (void)overwriteBackButton {
UIImage *backNormal = [UIImage imageNamed:@"backButton"];
_navBarLeftButton = [OpenSansButton buttonWithType:UIButtonTypeCustom];
_navBarLeftButton.frame = CGRectMake(10, 10, 35, 35);
[_navBarLeftButton setImage:backNormal forState:UIControlStateNormal];
[_navBarLeftButton addTarget:self action:@selector(backToList) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:_navBarLeftButton];
}