gpt4 book ai didi

iphone - UINavigationController 和 titleView

转载 作者:行者123 更新时间:2023-11-28 23:11:17 27 4
gpt4 key购买 nike

我的应用程序适合放在 UINavigationController 中。简单地说,我将 MyUIViewController 设置为 navigationControllerrootViewController,然后为 设置了一个 customTitleView viewDidLoadmyViewController 的 navigationItem

现在,当我按下 newViewController 时,我希望看到之前的 customTitleView(如 Apple NavigationController 引用所述),但它没有。

怎么了?

下面的一小部分代码和 Apple UINavigationController 引用

“如果新的顶级 View Controller 有自定义标题 View ,导航栏会显示该 View 以代替默认标题 View 。要指定自定义标题 View ,请设置 View Controller 导航项的 titleView 属性。 "

- (void)viewDidLoad {
[super viewDidLoad];

[self.navigationItem setTitleView:customTitleView];
}

也许“默认标题 View ”意味着“无”?我理解为之前的titleView。

编辑

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

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

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

我创建了一个 newTitleView 并将其放入 originalContainer.subviews (UIButton),因为我需要它的 Target 操作。

对于第一个推送的 Controller ,一切都完美无缺,但如果我将另一个 viewController 推送到堆栈中(第二个推送的),我将丢失对 navigationController 的所有引用。每个实例变量都是零。

这个值是在 viewDidLoad 中获取的

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

secondViewControllerPushedFromFirstViewControllerPushed.navigationController = 0x0secondViewControllerPushedFromFirstViewControllerPushed.parentViewController = 0x0

secondViewControllerPushed 似乎无处可去!!

这怎么可能?

我再次检查我是否正确地推送了 viewController 而不是模态显示

因为这个问题,我无法为 secondViewControllerPushed 正确设置 newTitleView。

最佳答案

这是一半的破解,但我觉得它可以让您对正在使用的 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];

}

关于iphone - UINavigationController 和 titleView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8214391/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com