【问题标题】:Programmatically edit title of controller in didSelectRowAtIndexPath在 didSelectRowAtIndexPath 中以编程方式编辑控制器的标题
【发布时间】:2012-11-22 04:38:57
【问题描述】:

以前,我尝试使用以下代码转到另一个控制器但失败了

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RowDetail *rd = [[RowDetail alloc]initWithNibName:@"RowDetail" bundle:nil];
    if ([[allRow objectAtIndex:indexPath.row] is Equal:@"One"]) 
    {
        [rd setTitle:[allRow objectAtIndex:indexPath.row]];
    }
    [self.navigationalController pushViewController:rd animated:NO];
}

我正在使用情节提要,所以我发现其他代码对我有用,但我无法为我正在推送的控制器设置标题。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
    RowDetail *rd = [sb instantiateViewControllerWithIdentifier:@"RowDetail"];
    if ([[allRow objectAtIndex:indexPath.row] is Equal:@"One"]) 
    {
        [rd setTitle:[allRow objectAtIndex:indexPath.row]];
    }
    [self.navigationalController pushViewController:rd animated:NO];
}

它工作并显示了 RowDetail 控制器,但我无法编辑标题。知道如何做到这一点吗?

编辑: RowDetail.h

#import <UIKit/UIKit.h>

@interface LecturerDetail : UIViewController

@property (weak, nonatomic) IBOutlet UINavigationBar *rowNavBar;

@property (nonatomic, strong) NSString *lecDetailTitle;
@end

对于 RowDetail,我尝试使用 IBOutlet 作为导航栏,但失败了。我仍在尝试让标题显示的方法。

【问题讨论】:

  • 请看我的更新更容易

标签: iphone objective-c ios uitableview didselectrowatindexpath


【解决方案1】:

在 RowDetailViewController 中

@property (nonatomic, strong) NSString *rowDetailTitle;
//synthesize in .m file too

然后

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
    RowDetail *rd = [sb instantiateViewControllerWithIdentifier:@"RowDetail"];
    rd.rowDetailTitle = @"yourTitle";
    [self.navigationalController pushViewController:rd animated:NO];
}

然后在视图中确实加载了 RowDetailViewController.m

self.navigationItem.title = rowDetailTitle;

希望对你有帮助,请反馈一下,谢谢。

更新:或者干脆这样做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
    RowDetail *rd = [sb instantiateViewControllerWithIdentifier:@"RowDetail"];
    rd.navigationItem.title = @"yourTitle";
    [self.navigationalController pushViewController:rd animated:NO];
}

【讨论】:

  • 两种方法都试过了,还是不显示标题画面
  • 我可以看看你的 RowDetailViewController.h 吗?
  • rowNavBar.title = self.navigationItem.title;在 RowDetailViewController.m 的 viewdidload 中
  • rowNavBar.topItem.title = self.navigationalItem.title;
  • 我误读了你的 .h 文件,所以结论 UINavigationBar 使用 navBar.topItem.title = @"title";但是如果 UINavigationItem 像我说的那样做:)
猜你喜欢
  • 2015-10-05
  • 1970-01-01
  • 2013-07-28
  • 2022-01-06
  • 1970-01-01
  • 2017-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多