UINavigationController 和 titleView

发布于 2024-12-17 09:34:21 字数 1788 浏览 1 评论 0原文

我的应用程序适合 UINavigationController 。 只需将 MyUIViewController 设置为 navigationControllerrootViewController,然后为 设置一个 customTitleView viewDidLoadmyViewController 的navigationItem

现在,当我推送 newViewController 时,我希望看到之前的 customTitleView (如 Apple NavigationController 参考所述),但事实并非如此。

出了什么问题?

下面的一小部分代码和 Apple UINavigationController 参考

“如果新的顶级视图控制器具有自定义标题视图,导航栏会显示该视图而不是默认标题视图。要指定自定义标题视图,请设置视图控制器导航项的 titleView 属性。”

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setTitleView:customTitleView];
}

也许“默认标题视图”意味着“什么都没有”?我把它解释为之前的titleView。

编辑

我正在尝试解决方法,但我还有其他一些问题。 解决方法是:

在我推入NavigationController的每个ViewController中,我在viewDidLoad中设置titleView,从rootViewController获取它,

UiView *originalContainer = [[[self.navigationController.viewControllers objectAtIndex:0] navigationItem] titleView]

我创建一个newTitleView并将其放入originalContainer.subviews(UIButton)中,因为我需要从中执行Target操作。

对于第一个推送的控制器来说,所有操作都完美,但是如果我在堆栈中推送另一个 viewController(第二个推送的),我就会丢失对 navigationController 的所有引用。每个实例变量都是nil。

该值是在 viewDidLoad

firstViewControllerPushed.navigationController = (UINavigationController*)0x6693da0 内获取的 FirstViewControllerPushed.parentViewController = (UINavigationController*)0x6693da0

SecondViewControllerPushedFromFirstViewControllerPushed.navigationController = 0x0 SecondViewControllerPushedFromFirstViewControllerPushed.parentViewController = 0x0

看来第二个ViewControllerPushed无处可去!

怎么可能?

我仔细检查了我是否正确推送了 viewController 而不是以模态方式呈现它,因为

这个问题我无法为第二个ViewControllerPushed 正确设置 newTitleView 。

My application fit inside a UINavigationController.
Simply I setted up a MyUIViewController as rootViewController of navigationController and than I set up a customTitleView for the navigationItem of myViewController inside viewDidLoad.

Now when I push a newViewController I expect to see the previous customTitleView (as described by Apple NavigationController reference) but it doesn't.

What's wrong?

A little part of code below and the Apple UINavigationController Reference

"If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the titleView property of the view controller’s navigation item."

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setTitleView:customTitleView];
}

Maybe the "default title view" means "nothing"? I interpreted it as the previous titleView.

EDIT

I'm trying a work around but I have some other issues.
The work around is:

In every ViewController that I push inside the NavigationController I setup in viewDidLoad the titleView getting it from the rootViewController

UiView *originalContainer = [[[self.navigationController.viewControllers objectAtIndex:0] navigationItem] titleView]

I create a newTitleView and I put inside the originalContainer.subviews (UIButton) cause I need the Target action from it.

All works perfectly for the first pushed controller but if I push another viewController in the stack (the second one pushed) I loose every reference to the navigationController. Every instance variables are nil.

This values are getted inside the viewDidLoad

firstViewControllerPushed.navigationController = (UINavigationController*)0x6693da0
firstViewControllerPushed.parentViewController = (UINavigationController*)0x6693da0

secondViewControllerPushedFromFirstViewControllerPushed.navigationController = 0x0
secondViewControllerPushedFromFirstViewControllerPushed.parentViewController = 0x0

It seems that the secondViewControllerPushed lives nowhere!!

How it's possible?

I double checked that I correctly push the viewController instead of present it modally

Couse this issue I'm not able to make a right setup of the newTitleView for the secondViewControllerPushed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

錯遇了你 2024-12-24 09:34:21

这是一个一半的黑客,但我觉得它给了你在你正在使用的 viewController 中更多的控制权。所以下面是我设置的一个小方法,然后我只是在 viewDidLoad 中调用它 - [self setUpNavBar];

使用 [UIButton buttonWithType:101] 完全没问题,因为它通过了 Apple 验证。

- (void) setUpNavBar
{
    [self.navigationController setNavigationBarHidden: NO animated:YES];

    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.title = @"Settings";


    UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];

    UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;
    [backItem release];

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Logout" 
                                                                            style:UIBarButtonItemStylePlain target:self 
                                                                            action:@selector(logoutAction)] autorelease];                     

}

This is a hack in a half but I feel it gives you a bit more control in the viewController you are working in. So below is a small method I have setup and then I just call it in viewDidLoad - [self setUpNavBar];

And using [UIButton buttonWithType:101] is perfectly fine as it passed Apple Validation.

- (void) setUpNavBar
{
    [self.navigationController setNavigationBarHidden: NO animated:YES];

    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.title = @"Settings";


    UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];

    UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;
    [backItem release];

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Logout" 
                                                                            style:UIBarButtonItemStylePlain target:self 
                                                                            action:@selector(logoutAction)] autorelease];                     

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文