之前,我试图使用以下代码转到另一个 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/