【问题标题】:Set a UIViewController's title with a NSMutableArray item使用 NSMutableArray 项设置 UIViewController 的标题
【发布时间】:2012-08-01 10:52:03
【问题描述】:

是否可以通过数组项设置UIViewController 的标题?例如,如果我有一个 NSArrayNSMutableArray 食物列表,单击食物名称,然后它会成为加载视图中的标题?

【问题讨论】:

    标签: ios uiviewcontroller title


    【解决方案1】:

    假设您有一系列食物名称,例如:

    NSArray *foodArr = [NSArray arrayWithObjects:@"Chicken",@"Cheese",@"Hamburger"]
    

    并且您希望标题为“Cheese”(索引为 1 的对象),您需要从数组中获取 NSString:

    NSString *foodTitle = [foodArr objectAtIndex:1];
    

    然后您可以将标题设置为该字符串:

    self.navigationController.title = foodTitle;
    

    【讨论】:

    • 您也可以说UIViewController 必须包含在UINavigationController 中。干杯:)
    【解决方案2】:

    如果你有一个字符串数组,你可以这样做:

    yourViewController.title = [self.myNameArray objectAtIndex:selectedIndex];
    

    如果你有一个带有 title 属性的对象数组:

    yourViewController.title = [[self.myObjectArray objectAtIndex:selectedIndex] title];
    

    【讨论】:

      【解决方案3】:

      如果您将 UINavigationController 与 NavigationBar 一起使用,您可以轻松地进行以下操作:

      -(void) viewDidLoad {
      
         self.navigationItem.title = [NSString stringWithString:@"%@",[yourTitles objectAtIndex:yourIndex];
      
      }
      

      如果您不使用它,那么您将手动添加 UILabel 以显示您的标题并执行相同操作:

      -(void) viewDidLoad {
      
         [titleLabel setText:[NSString stringWithString:@"%@",[yourTitles objectAtIndex:yourIndex]];
      
      }
      

      【讨论】:

        【解决方案4】:

        是的,很简单

        //Assuming you are displaying food item in tableView
        //vController is class object which is going to be pushed on navigation stack
        //Put this line in didSelectRowAtIndexPath method
        
        yourViewControllerObject.title = [foodItemArray objectAtIndex:indexPath.row];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-23
          • 1970-01-01
          • 2018-08-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-24
          • 1970-01-01
          相关资源
          最近更新 更多