【问题标题】:How do I set the title on the destination view controller during prepareForSegue:如何在 prepareForSegue 期间在目标视图控制器上设置标题:
【发布时间】:2013-07-21 10:54:32
【问题描述】:

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

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

可能因为目标视图控制器的视图尚未加载而无法工作。我可以在不创建自定义目标视图控制器的情况下执行此操作吗?

【问题讨论】:

    标签: ios uistoryboard uistoryboardsegue


    【解决方案1】:

    尝试像这样访问嵌入在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";
        }
    }
    

    【讨论】:

    • 谢谢。这不完全是我的问题,这个解决方案也不适合我,但它提醒我源视图控制器位于容器视图中,然后位于导航视图控制器内。所以我只需要在我的视图堆栈中引用正确的导航控制器。
    • Steve,您能详细说明一下您是如何引用正确的导航控制器的吗?
    【解决方案2】:

    如果您现在需要静态标题(即在 Xcode 4.6.3 中),只需在相关视图控制器的导航栏中设置标题即可在 Storyboard 中完成。

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

    我花了永远(新手,叹息!)弄清楚如何修改 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];   
    }
    

    【讨论】:

      【解决方案3】:

      如果 segue 是 push segue - 如果您使用 UINavigationController 应该是这样 - 然后目标视图控制器会自动添加到窗口层次结构中,您甚至不需要识别 UINavigationController:

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

      【讨论】:

        【解决方案4】:

        这是为了转入另一个 UITableView

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

          @property (strong, nonatomic) NSString *navBarTitle;

        2. 重写 .m 文件中的设置器以确保安全。

          - (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. 现在是重要的一点...... 在目标控制器中,获取视图的顶部控制器并设置标题属性,在加载视图之后,在它显示之前:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-26
          • 2015-11-03
          • 2017-01-20
          • 1970-01-01
          相关资源
          最近更新 更多