[segue.destinationViewController setTitle:@"doesn't work"];
[segue.destinationViewController.navigationItem setTitle:@"this either"];
[segue.destinationViewController setTitle:@"doesn't work"];
[segue.destinationViewController.navigationItem setTitle:@"this either"];
UINavigationController
中的 ViewController
。首先,在接口生成器中为您的segue命名标识符,然后在 prepareForSegue
方法中访问segue,并通过访问您跳转到的导航控制器的 topViewController
属性来设置标题。- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"yourSegueIdentifier"]) {
UINavigationController *navController =
(UINavigationController*)[segue destinationViewController];
YourViewController *destViewController =
(YourViewController* )[navController topViewController];
destViewController.navgationItem.title.text = @"Your new title";
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"DetailSegue"]) {
// Fetch Item
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDictionary *item = [self.groceries objectAtIndex:[indexPath row]];
// Configure Detail View Controller
TPDetailViewController *vc = [segue destinationViewController];
vc.navigationItem.title = [item objectForKey:@"name"];
[vc setItem:item];
}
在目标UITableViewController文件中设置一个公共的NSString属性。
@property (strong, nonatomic) NSString *navBarTitle;
在.m文件中覆盖setter以确保其正确性。
- (void) setNavBarTitle:(NSString *)navBarTitle
{
_navBarTitle = navBarTitle;
}
在源tableView的segue方法中,传递所需的任何字符串作为标题。
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString: @"userDetails"]) {
UsersActivitiesTableViewController *destinationController = segue.destinationViewController;
//获取用户刚刚单击的单元格的索引路径
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//如果下一个视图需要特定的用户名等标题,请从用户数组中获取对象。
UserMO *thisUser = [self.users objectAtIndex:indexPath.row];
//将要用作标题的字符串传递给公共的NSString属性
destinationController.navBarTitle = thisUser.name;
}
}
现在是重点...
在目标控制器中,在视图加载后、显示前,获取视图的顶部控制器并设置标题属性:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController topViewController].title = self.navBarTitle;
}
if ([segue.identifier isEqualToString:@"yourSegueNameHere"]) {
[segue.destinationViewController setTitle:@"yourTitleHere"];
}