NavigationItem setTitle不适用于二级UIViewController [英] NavigationItem setTitle doesn't work for second level UIViewController

查看:134
本文介绍了NavigationItem setTitle不适用于二级UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中我有一个UITabBarController,UINavigationControllers像这样初始化

in my app i have an UITabBarController with UINavigationControllers initialized like this

UINavigationController *newsNavigationController = [[UINavigationController alloc] initWithRootViewController: newsViewControler];

newsViewController是一个UIViewController。
当我按下标签栏中的按钮以显示带有newsViewController的导航项时,导航栏中的标题设置为ok。在newsViewController中我有一个我在init之后调用的方法设置。在设置方法中,我设置标题,如
[[self navigationItem] setTitle:[category_c title]];

newsViewController is an UIViewController. When i press the button in the tabbar to show the navigation item with the newsViewController the title in the navigation bar is set ok. in the newsViewController i have a method setup that i call after init. In the setup method i set the title like this [[self navigationItem] setTitle:[category_c title]];

问题来了,在我的newsViewController中我有一个tableView,当我触摸一个单元格时,我想用所选新闻推送一个UIViewController,并在导航栏中显示标题。

The problem comes now, in my newsViewController i have a tableView, when i touch a cell i want to push a UIViewController with the selected news and in the navigation bar to show the title.

在UIViewController中我有相同的设置方法,我在其中设置如上所述的标题。问题是没有显示。导航项不为null,因为我可以调用 self.navigationItem.hidesBackButton = YES; 并且每次都会隐藏后退按钮但标题不会显示。

In the UIViewController i have the same setup method in which i setup the title like above. The problem is that is not shown. The navigation item is not null because i can call self.navigationItem.hidesBackButton = YES; and it hides the backbutton every time but the title isn't shown.

当触摸单元格时,我创建并推送新闻UIViewController

I create and push the news UIViewController like this when a cell is touched

if(newsViewController == nil){
        newsViewController = [[NewsViewController alloc] init];
        [newsViewController setup];
    }
    [self.navigationController pushViewController: newsViewController animated:YES];

newsViewController中的设置方法如下所示:

The setup method in the newsViewController looks like this:

- (void) setup {
    self.navigationItem.hidesBackButton = YES;
    [self.navigationItem setTitle:@"my view"];
}

在newsViewController我有viewDidLoad并尝试在那里设置标题但没有成功。

in the newsViewController i have viewDidLoad and tried to set the title there but with no success.


  • (void)viewDidLoad {
    self.navigationItem.title = @Test;
    [super viewDidLoad];
    }

如果在之后[self.navigationController pushViewController:newsViewController animated:YES];
i write [[self.navigationController.viewControllers objectAtIndex:0] setTitle:@myTitle]; 包含UITableView的UIViewController获取标题myTitle而不是我触摸单元格时推送的newsViewController。

if after [self.navigationController pushViewController: newsViewController animated:YES]; i write [[self.navigationController.viewControllers objectAtIndex:0] setTitle:@"myTitle"]; the UIViewController that contains the UITableView gets the title myTitle instead of the newsViewController that i pushed when i touched the cell.

任何指针?我无法理解为什么标题设置不正确。

any pointers? i can't understand why the title is not set correctly.

EDIT2:在阅读了一些内容并逐步使用调试器完成我的代码之后比较rootviewcontrollers和childViewControllers我发现在设置self.title = @title之后,在childViewController的viewDidLoad方法中创建了navigationItem,因此它不是零情况。在调试器中,当我展开_navigationItem变量时,字段_navigationBar在离开viewDidLoad后为nil,在rootViewControllers中,navigationBar为NOT NULL,标题就在那里。我读到如果navigationBar为null,标题将不会显示。在图像是我在debuger中看到的。 alt text http://img138.imageshack.us/img138/1513/picture2bq.png 现在我正朝这个方向搜索。

After reading a little more and going through my code with the debugger step by step and comparing the rootviewcontrollers with the childViewControllers i've observed that in the viewDidLoad method of the childViewController after setting self.title = @"title" the navigationItem is created so it's not the nil situation. In the debugger when i expand the _navigationItem variable the field _navigationBar is nil after leaving viewDidLoad, in the rootViewControllers the navigationBar is NOT NULL and the title is there. I read that if the navigationBar is null the title will not show up. In the image is what i've seen in the debuger. alt text http://img138.imageshack.us/img138/1513/picture2bq.png now i'm searching in this direction.

EDIT1:下面是parentViewController,firstChildViewController和第二个ChildViewController的代码。
查看parentViewController(标题正常)。我按下导航栏上的一个按钮,调用bearbeitenAction - >第一个孩子被按下(标题未显示)。在firstChildViewController的导航栏上,我有一个按钮,按下时按下堆栈上的新ViewController(secondChildViewController)。第二个ChildViewController的标题不正常。当我按Back时,firstChildViewController的标题显示为Ok。

parentViewController:

parentViewController:

    //MARK: -
    //MARK: Initialization methods
    - (void) setup {
        dataManipulator = [[DataManipulation alloc] init];
        dataManipulator.delegate = self;

        [self.tabBarItem initWithTitle:[NSString stringWithFormat:@"%@",[category_c title]] image:[UIImage newImageFromResource:[category_c icon]] tag:0];

        searchResults = nil;
        companyDetailsPushed = NO;
    }

    //MARK: -
    //MARK: ViewControllerMethods
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void) loadView {
        NSLog(@"WatchlistViewController: loadView");

        UIView *mainView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] applicationFrame].size.width, [[UIScreen mainScreen] applicationFrame].size.height)] autorelease];
        [mainView setBackgroundColor:[UIColor whiteColor]];
        mainView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        mainView.contentMode = UIViewContentModeScaleToFill;
        [mainView setAutoresizesSubviews:YES];

            companiesTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, [[UIScreen mainScreen] applicationFrame].size.width, 322)];
        companiesTable.delegate = self;
        companiesTable.dataSource = self;
        [mainView addSubview:companiesTable];
        [companiesTable release];

            self.view = mainView;
    }

    - (void)viewDidLoad {
        [super viewDidLoad];
        if(![[category_c subtitle] isEqualToString:@""]) {
            self.title = [category_c subtitle];
        }
        else {
            self.title = [category_c title];
        }
    }

    - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        if(companyDetailsViewController == nil) {
            companyDetailsViewController = [[CompanyDetailsScrollViewController alloc] init];
            [companyDetailsViewController setup];
        }

        [watchlistDoneBtn setHidden:TRUE];
        companyDetailsPushed = YES;

        if(viewMode == SearchViewMode)
            [companyDetailsViewController setCompany:[searchResults objectAtIndex:indexPath.row]];
        else if(viewMode == WatchlistViewMode)
            [companyDetailsViewController setCompany:[[[Financial_deAppDelegate sharedAppDelegate] watchlistCompanies] objectAtIndex:indexPath.row]];

        [[self navigationController] pushViewController:companyDetailsViewController animated:YES];
    }

- (void) bearbeitenAction:(UIButton *) sender {
    NSLog(@"Berbeiten btn was pressed");
    if(berbeitenViewController == nil){
        berbeitenViewController = [[BerbeitenViewController alloc] init];
        [berbeitenViewController setup];
    }
    [self.navigationController pushViewController:berbeitenViewController animated:YES];
}

firstChildViewController = berbeitenViewController代码:

firstChildViewController = berbeitenViewController in the code:

- (void) setup {
    self.navigationItem.hidesBackButton = YES;
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    NSLog(@"BerbeitenViewController: loadView");

    UIView *mainView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] applicationFrame].size.width, [[UIScreen mainScreen] applicationFrame].size.height)] autorelease];
    [mainView setBackgroundColor:[UIColor darkGrayColor]];
    mainView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    mainView.contentMode = UIViewContentModeScaleToFill;
    [mainView setAutoresizesSubviews:YES];

    berbeitenBackBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 23, 69, 36)];
    [berbeitenBackBtn setBackgroundImage:[UIImage newImageFromResource:@"back_btn.png"] forState:UIControlStateNormal];
    [berbeitenBackBtn addTarget:self action:@selector(popViewController:) forControlEvents:UIControlEventTouchUpInside];
    [berbeitenBackBtn setEnabled:TRUE];
    [self.navigationController.view addSubview:berbeitenBackBtn];
    [berbeitenBackBtn release];

    berbeitenAddBtn = [[UIButton alloc] initWithFrame:CGRectMake(260, 23, 55, 36)];
    [berbeitenAddBtn setBackgroundImage:[UIImage newImageFromResource:@"bearbeiten_add_btn.png"] forState:UIControlStateNormal];
    [berbeitenAddBtn addTarget:self action:@selector(addCompany:) forControlEvents:UIControlEventTouchUpInside];
    [berbeitenAddBtn setEnabled:TRUE];
    [self.navigationController.view addSubview:berbeitenAddBtn];
    [berbeitenAddBtn release];

    companiesTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] applicationFrame].size.width, 366)];
    companiesTable.delegate = self;
    companiesTable.dataSource = self;
    [mainView addSubview:companiesTable];
    [companiesTable release];

    self.view = mainView;
}

- (void)viewDidLoad {
    NSLog(@"BerbeitenViewController viewDidLoad");
    [super viewDidLoad];
    self.title = @"Edit watchlist";
}

//MARK: Buttons actions

- (void) popViewController:(UIButton *) sender {
    NSLog(@"Pop BerbeitenViewController");
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) addCompany:(UIButton *) sender {
    NSLog(@"Berbeiten addCompany btn pushed");
    if(searchViewController == nil){
        searchViewController = [[SearchViewController alloc] init];
        [searchViewController setup];
    }
    [self.navigationController pushViewController:searchViewController animated:YES];
}

secondChildViewController =代码中的searchViewController

secondChildViewController = searchViewController in code

- (void) setup {
    self.navigationItem.hidesBackButton = YES;
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    NSLog(@"BerbeitenViewController: loadView");

    UIView *mainView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] applicationFrame].size.width, [[UIScreen mainScreen] applicationFrame].size.height)] autorelease];
    [mainView setBackgroundColor:[UIColor darkGrayColor]];
    mainView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    mainView.contentMode = UIViewContentModeScaleToFill;
    [mainView setAutoresizesSubviews:YES];

    searchViewBackBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 23, 69, 36)];
    [searchViewBackBtn setBackgroundImage:[UIImage newImageFromResource:@"back_btn.png"] forState:UIControlStateNormal];
    [searchViewBackBtn addTarget:self action:@selector(popViewController:) forControlEvents:UIControlEventTouchUpInside];
    [searchViewBackBtn setEnabled:TRUE];
    [self.navigationController.view addSubview:searchViewBackBtn];
    [searchViewBackBtn release];

    self.view = mainView;
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Edit Watchlist";
}


推荐答案

问题的根源是我使用在此地址找到的方法添加的navigationBar的自定义背景图像 http://sebastiancelis.com/ 。基本上发生了什么,我引用了作者:

the source of the problem was the custom background image for the navigationBar that i added using the method found at this address http://sebastiancelis.com/ . Basically what happened was and i quote from the author:


通过使用类别,我们可以轻松
添加我们自己的setBackgroundImage:方法
并在我们创建
UINavigationBar或
UINavigationController时调用它。这个方法
只是将UIImageView作为
子视图添加到导航栏,然后
将该子视图发送到其
超级视图的后面。

By using a category, we could easily add our own setBackgroundImage: method and call it whenever we create a UINavigationBar or UINavigationController. This method would just add a UIImageView as a subview to the navigation bar and then send that subview to the back of its superview.

但是,如果你试试这个,你会很快看到
只有
才有效。 UIImageView肯定是
可见,甚至最初是在
正确的z-index。 但是,一旦你将
推送或弹出一个视图控制器到
导航控制器的堆栈,你
就会看到背景图像不再是后台的
。相反,
它会阻止标题和导航
栏按钮。这绝对不是我们想要的

However, if you try this you will quickly see that it only kind of works. The UIImageView is definitely visible and even initially at the correct z-index. However, once you push or pop a view controller onto the navigation controller's stack, you will see that the background image is no longer in the background. Instead, it blocks out the title and navigation bar buttons. This is definitely not what we want.

当我找到这个改变背景的解决方案时navigationBar我很确定我做对了,但似乎我没有..

when i found this solution for changing the background of the navigationBar i was pretty sure that i got it right but it seems i didn't..

再次感谢你的时间!

这篇关于NavigationItem setTitle不适用于二级UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆