【问题标题】:ios Storyboard - Push a title onto the View Controllerios Storyboard - 将标题推送到视图控制器上
【发布时间】:2012-06-13 01:21:04
【问题描述】:

这段代码是否假设在我要连接的 ViewController 上设置标题?

两个 UIViewControllers 通过 push segue 连接 - 第一个嵌入在 NavigationController 中。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 if ([[segue identifier] isEqualToString:@"settingsSegue"])
 {

     self.navigationItem.title = [[NSString alloc] initWithFormat:@"Custom Title"];

 }
 }

它对我不起作用,但语法是正确的。

提前谢谢:-)

【问题讨论】:

    标签: ios uinavigationcontroller storyboard


    【解决方案1】:

    上述答案对我有用,但有一个例外......

    改变

    self.title = myTitle;
    

    self.navigationItem.title = myTitle;
    

    【讨论】:

    • 好吧,myTitle 值没有被推过 - 我检查了返回 (null) 的 NSLog
    【解决方案2】:

    使用目标 VC 中的属性在目标视图控制器的 viewDidLoad 中设置标题:

    if ([[segue identifier] isEqualToString:@"settingsSegue"]) {
        MyDestinationViewController *mdvc = segue.destinationViewController;
        mdvc.myTitle = [[NSString alloc] initWithFormat:@"Custom Title"];
    }
    

    然后在 MyDestinationViewController.h 中的viewDidLoad 事件中:

    @property (nonatomic,strong) NSString *myTitle;
    

    在 MyDestinationViewController.m 中:

    @synthesize myTitle;
    

    最后在viewDidLoad:

    self.title = myTitle;
    

    【讨论】:

    • 抱歉看起来很有希望:-) 再次没有错误,但它没有通过抱歉
    • 我得到了它的工作 - 有一个金发碧眼的时刻,忘记将 ViewController 链接到 Storyboard 中的头文件:-)
    【解决方案3】:

    你也可以直接在segue中设置标题,不需要通过属性:

    - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"settingsSegue"]) {    
            segue.destinationViewController.navigationItem.title = @"Custom Title";
        }
    }
    

    或者,我需要的是,将推送的视图控制器的标题设置为单击的表格单元格的标题:

    - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"settingsSegue"]) {   
            NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
            UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:myIndexPath];        
            segue.destinationViewController.navigationItem.title = cell.textLabel.text;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 2020-02-02
      • 1970-01-01
      相关资源
      最近更新 更多