使用NSMutableArray项设置UIViewController的标题

时间:2012-08-01 10:52:03

标签: ios uiviewcontroller title

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

4 个答案:

答案 0 :(得分:0)

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

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

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

NSString *foodTitle = [foodArr objectAtIndex:1];

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

self.navigationController.title = foodTitle;

答案 1 :(得分:0)

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

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

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

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

答案 2 :(得分:0)

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

-(void) viewDidLoad {

   self.navigationItem.title = [NSString stringWithString:@"%@",[yourTitles objectAtIndex:yourIndex];

}

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

-(void) viewDidLoad {

   [titleLabel setText:[NSString stringWithString:@"%@",[yourTitles objectAtIndex:yourIndex]];

}

答案 3 :(得分:0)

是非常简单

//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];