繁体   English   中英

如何在prepareForSegue期间在目标视图控制器上设置标题:

[英]How do I set the title on the destination view controller during prepareForSegue:

我想简单地在我的目标视图控制器上设置标题,以便它在prepareForSegue:方法的导航控制器的导航栏中显示,但是设置它的标题或navigationItem如下:

[segue.destinationViewController setTitle:@"doesn't work"]; [segue.destinationViewController.navigationItem setTitle:@"this either"];

不起作用可能是因为目标的视图控制器的视图尚未加载。 我可以不创建自定义目标视图控制器吗?

尝试像这样访问嵌入在UINavigationController中的ViewController 首先,在界面构建器中为segue指定一个标识符,然后在prepareForSegue方法中访问segue,并通过访问您所topViewController的导航控制器的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";
    }
}

如果你现在想要一个静态标题(例如,在Xcode 4.6.3中),只需在相关视图控制器的导航栏中设置标题即可在故事板中完成。

但是,如果您希望导航栏标题根据正在被查看的视图而更改,例如,特定表行的详细信息,据我所知,需要以编程方式设置。

我花了永远(新手,叹气!)来弄清楚如何修改Julian Vogels的正确答案来调用我想要使用的密钥。 这是有效的:

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

这是一个segue到另一个UITableView

  1. 在目标UITableViewController文件中设置公共NSString属性。

    @property (strong, nonatomic) NSString *navBarTitle;

  2. 覆盖.m文件中的setter只是为了确定。

    - (void) setNavBarTitle:(NSString *)navBarTitle { _navBarTitle = navBarTitle; }

  3. 在原始tableView的segue方法中,传递标题中需要的任何字符串。

    - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString: @"userDetails"]) { UsersActivitiesTableViewController *destinationController = segue.destinationViewController; //Get the index path of the cell the user just clicked NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; //Grab the object from the array of users if the next view requires a specific title for a user name etc. UserMO *thisUser = [self.users objectAtIndex:indexPath.row]; //Pass the string you want as the title to the public NSString property destinationController.navBarTitle = thisUser.name; } }

  4. 现在,对于重要的位....在目标控制器中,获取视图的顶级控制器并在加载视图之后,在显示之前设置title属性:

    - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController topViewController].title = self.navBarTitle; }

如果segue是push segue - 如果你正在使用UINavigationController它应该是 - 那么目标视图控制器会自动添加到窗口层次结构中,你甚至不需要识别UINavigationController:

if ([segue.identifier isEqualToString:@"yourSegueNameHere"]) {    
    [segue.destinationViewController setTitle:@"yourTitleHere"];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM