【问题标题】:Setting Navigation Item Title in a modal view在模态视图中设置导航项标题
【发布时间】:2011-06-16 14:15:07
【问题描述】:

在查看现有答案时,我认为 Steve's question and codelark's answer 是我的问题的壁橱。但我在这里遗漏了一些东西。

我的模式视图的 xib: IFDNewFormViewController.xib:

View Controller
> Table View
>> View
>>> Navigation Bar
>>>> Navigation Item - New Form
>> View
>>> Toolbar
>>>> Bar Button Item - Cancel
>>>> Bar Button Item - Flexible Space
>>>> Bar Button Item - Done

按下按钮时调用我的方法:

IFDNewFormViewController.m:

- (void) newFormDisplay:(id)sender {
    // Show the Create Flightlog View
    IFDNewFormViewController *newForm = [[IFDNewFormViewController alloc] init];
    newForm.flightlogDelegate = self;
    newForm.dataType = [self ifdDataType];
    newForm.displayDataType = [NSString stringWithFormat:@"New %@",[self displayDataType]];
    newForm.title = [NSString stringWithFormat:@"Title %@",[self displayDataType]];
    newForm.navigationItem.title = [NSString stringWithFormat:@"NavItem.Title %@",[self displayDataType]];
    newForm.navigationController.title = [NSString stringWithFormat:@"NavController.Title %@",[self displayDataType]];
    newForm.modalViewController.title = [NSString stringWithFormat:@"ModalViewController.Title %@",[self displayDataType]];


    NSLog(@"iFDListViewController_Pad:newFormDisplay start form for dataType=%@ (%@)",[self ifdDataType], [self displayDataType]);
    [self presentModalViewController:newForm animated:YES];
    [newForm release];
}

我已经在xib中设置了标题:

导航项 - 新表单 -> 标题。

该标题是视图上显示的标题。使用在这个论坛中找到的信息,我添加了以下几行:

newForm.navigationItem.title = [NSString stringWithFormat:@"NavItem.Title %@",[self displayDataType]];
newForm.navigationController.title = [NSString stringWithFormat:@"NavController.Title %@",[self displayDataType]];
newForm.modalViewController.title = [NSString stringWithFormat:@"ModalViewController.Title %@",[self displayDataType]];

还是不开心。

我在这里遗漏了一些东西。有什么想法吗?

【问题讨论】:

    标签: ios uinavigationbar uinavigationitem


    【解决方案1】:

    由于您没有将此视图控制器推送到 UINavigationController 上,因此设置 newForm.navigationItem.title 将无法解决问题。

    看起来您正在推送一个包含它自己的导航栏的模态视图控制器。在这种情况下,您可能需要创建对它的自己的引用并将 xib 中的栏与它联系起来。

    在 IFDNewFormViewController.h 创建一个实例变量

        UINavigationBar *customNavBar;
    

    还有一个插座属性

        @property (nonatomic, retain) IBOutlet *UINavigationBar customNavBar;
    

    在 IFDNewFormViewController.m 中:

        @synthesize customNavBar;
    

    一定要在 dealloc 中释放它,并在 viewDidUnload 中设置为 nil。

    编辑 xib 时,右键单击导航栏,然后单击并将“新引用出口”旁边的打开圆圈拖动到文件所有者并选择 customNavBar。这样,该属性将在加载后包含您的 xib 中的栏。

    当您初始化 IFDNewFormViewController 时,请务必使用以下内容:

        IFDNewFormViewController *newForm = [[IFDNewFormViewController] alloc] initWithNibName:@"whatever-the-file-name-is" bundle:nil];
    

    否则xib中的数据甚至都不会被使用。

    【讨论】:

    • 我已经添加了建议的代码。我需要在@property 行中添加类型。分配 IFDNewFormViewController 后,当我设置标题时,没有错误,但 xcode 从 .title= 行末尾设置的断点显示“newFormNavBar (UINavigationBar *) 0x0”。我在 xib 中验证了单击 View Controller -> Table View -> View -> Navigation Bar 时,在 Reference Outlets 下:newFormNavBar
    • 我确实得到了这个,对@tlbrack 提供的解决方案进行了一些更改。更改:1.我引用了 UINavigationItem,2.在 IFDNewFormViewController.m 中将行线:newFormNavItem.title = [NSString stringWithFormat:@"Title %@",[self displayDataType]]; 移动到viewWillAppear:(BOOL)animated。为什么在调用 `IFDNewFormViewController *newForm = [[IFDNewFormViewController] alloc] initWithNibName:@"whatever-the-file-name-is" bundle:nil];` 之后没有初始化 newFormNavItem?我应该通过最终更改为这个问题添加一个解决方案,还是将@tlbrack 标记为解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多