[英]How can I set a segue to a different view controller from a table view controller, based on the detail text label of the table view cell?
I am just beginning to learn objective-c and programming in general, and I am really stuck on this issue. 我才刚开始学习Objective-C和程序设计,而我确实对这个问题感到困惑。 I am using storyboards to create an app that keeps scores for different types of games.
我正在使用情节提要创建一个可保留不同类型游戏得分的应用程序。 I have a master view controller that is a table view and it has prototype cells with detail labels.
我有一个主视图控制器,它是一个表视图,它具有带有详细标签的原型单元。 There is an
AddKeeperViewController
that allows the user to input player names and the game type. 有一个
AddKeeperViewController
,允许用户输入玩家名称和游戏类型。 The game type is then used as the detail label for the prototype cells. 然后将游戏类型用作原型单元的详细标签。
I want to then have each cell push to a different view controller, depending on what the detail text label is. 然后,我想将每个单元格推送到不同的视图控制器,具体取决于详细文本标签是什么。 I only have 2 game types at the moment, I know I need to set up logic in tableView
didSelectRowAtIndexPath
that will choose which segue to take. 目前,我只有2种游戏类型,我知道我需要在tableView
didSelectRowAtIndexPath
中设置逻辑,该逻辑将选择采用哪种模式。 I set up my segues from the view controller, not the cells, and they each have a unique identifier. 我是从视图控制器(而不是单元格)设置的,它们每个都有唯一的标识符。 I just don't know how to set up my if statements to use the
gameType
as the deciding factor for which segue to take. 我只是不知道如何设置我的if语句,以将
gameType
用作进行选择的决定因素。
Here is some of my code from my MasterViewController.m
file: 这是我的
MasterViewController.m
文件中的一些代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"scoreKeeperCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
scoreKeeper *keeperAtIndex = [self.dataController objectInListAtIndex:indexPath.row];
[[cell textLabel]setText:keeperAtIndex.name];
[[cell detailTextLabel] setText:keeperAtIndex.gameType];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if(cell.detailTextLabel) {
//statement
}
if ([[segue identifier] isEqualToString:@"ShowKeeperDetails"]) {
MADDetailViewController *detailViewController = [segue destinationViewController];
detailViewController.keeper = [self.dataController objectInListAtIndex:[self.tableView indexPathForSelectedRow].row];
}
}
It seems like you're on the right track. 看来您在正确的轨道上。 However, I think you're waiting a bit too long to choose which segue to take.
但是,我认为您等待时间太长,无法选择要使用的序列。 Perhaps something like this would work for you:
也许这样的事情对您有用:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.detailTextLabel.text isEqualToString:@"TEXT"])
{
[self performSegueWithIdentifier:@"segueTypeOne" sender:nil];
}
else
{
[self performSegueWithIdentifier:@"segueTypeTwo" sender:nil];
}
}
您还可以创建不同单元的原型,每个原型都与导致不同视图的不同segue连接,然后使用正确的原型在dequeueReusableCellWithIdentifier
获取单元。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.