iphone - 在 didSelectRowAtIndexPath 中以编程方式编辑 Controller 的标题

标签 iphone objective-c ios uitableview didselectrowatindexpath

之前,我试图使用以下代码转到另一个 Controller 但失败了

- (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];
}

我正在使用 Storyboard,所以我发现其他代码对我有用,但我无法为我推送的 Controller 设置标题。

- (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 Controller ,但我无法编辑标题。知道如何做到这一点吗?

编辑: 行详细信息.h

#import <UIKit/UIKit.h>

@interface LecturerDetail : UIViewController

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

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

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

最佳答案

在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];
}

然后在 View 中加载 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];
}

关于iphone - 在 didSelectRowAtIndexPath 中以编程方式编辑 Controller 的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13505986/

相关文章:

ios - UICollectionView 显示与所选单元格相关的图像

ios - 在应用程序问题中使用自定义字体

ios - 使用 Objective-C 在 Realm 中捕获写入错误的正确方法是什么?

java - 用户登录后,我应该使用类实例(序列化)还是单例来存储 userInfo

iphone - 如何在不使用开放图谱API的情况下在Facebook墙上发布图像和文本

iphone - UITabBarController.selectedIndex 不改变tabBar图标?

ios - __bridge_transfer 从 CFArrayRef 到 NSArray* - 元素也转移了吗?

ios - 当 Objective-C 作为桥梁时,快速在索引 0 处发出检查计数

iphone - objective-c 方法支持 "pass by value"吗?

ios - 遵循 SKAction 的路径不起作用