我做了一点研究,但大多数例子仍然是一个ViewController
到一个ViewController
:我有4个TableViewControllers
到一个TableViewController
。
我在detailsTableViewController
上有一个简单的数组。对于数组中的一个项目(在表上显示),我希望它是基于TableViewController
这个页面被调用的不同颜色。我有四个不同的TableViewControllers
它们都有一个segue到detailsTableViewController
的按钮,所以我想知道我是从哪个来的。
然后我想这样做:
if (indexPath.row == 1)
{ cell.detailTextLabel.textColor = [UIColor someColor];}
所以我需要的逻辑是:
if (calledSegue="fromRed")
{if (indexPath.row == 1)
{cell.detailTextLabel.textColor = [UIColor redColor];}}
else if (calledSegue="fromYellow")
{if (indexPath.row == 1)
{cell.detailTextLabel.textColor = [UIColor yellowColor];}}
else if (calledSegue="fromGreen")
{if (indexPath.row == 1)
{cell.detailTextLabel.textColor = [UIColor greenColor];}}
else if (calledSegue="fromBlack")
{if (indexPath.row == 1)
{cell.detailTextLabel.textColor = [UIColor blackColor];}}
谁能帮我弄清楚我需要做些什么来完成这个工作?同样,我需要找出哪个TableViewController
是segue的源,然后根据该信息更改indexPath == 1
的文本颜色。
每个segue都有一个单独的标识符,正如您在我的示例代码中看到的那样。
谢谢
对于这种作业,您应该使用prepareForSegue:sender:
方法。
首先,添加一个属性firstRowColor
到你的细节控制器,这样你就可以在你的tableView
委托上使用它。
然后在你的每个源视图控制器中,重写prepareForSegue:sender:
方法。这个方法在segue将要执行时被调用,以便沿着视图控制器传递数据。试试这样做:
- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
// Check if the segue is the right one
if ([segue.identifier isEqualToString:kDetailSegueIdentifier]) {
// Specify the right color for this source view controller
[(DetailViewController*)segue.destinationViewController setFirstRowColor:[UIColor redColor]];
}
}