gpt4 book ai didi

ios - didSelectRowAtIndexPath 或 prepareForSegue 使用?

转载 作者:行者123 更新时间:2023-11-28 22:18:17 24 4
gpt4 key购买 nike

我希望能够在我的表格 View 中选择一个项目并进入正确的 View 。假设我的 uitableview 中有两个项目。一个的值为 ubuntu,另一个的值为 arch——在我来自 arraA 的 plist 中,我有一个 SystemID,其中包含文本 Ubuntu/Arch,从来没有两个

现在,当我选择带有 ubuntu 的那个时,我希望它能推送到一个新 View ,其中包含 ubuntu 内容如果我返回并选择 arch,它会再次将我推向 arch 东西所在的 View

到目前为止我做了这个但它没有按预期工作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *segueName = nil;

if ([[arrayA valueForKey:@"SystemID"]isEqualToString:@"Ubuntu Linux"]) {
ActionViewController *controller = [[ActionViewController alloc] initWithNibName:@"Ubuntu Linux" bundle:nil];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
else if ([[arrayA valueForKey:@"SystemID"]isEqualToString:@"Arch Linux"]) {
ActionViewController *controller = [[ActionViewController alloc] initWithNibName:@"Arch Linux" bundle:nil];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}

[self performSegueWithIdentifier: segueName sender: self];
}

也试过prepare forsegue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([arrayA valueForKey:@"Arch Linux"])
{
ActionViewController *controller = (ActionViewController *)segue.destinationViewController;
NSIndexPath *indexPath = [self.mainTableView indexPathForSelectedRow];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
NSLog(@"hostname = %@", controller.Serverinfo);
}
if ([arrayA valueForKey:@"Ubuntu Linux"]) {
ActionViewController *controller = (ActionViewController *)segue.destinationViewController;
NSIndexPath *indexPath = [self.mainTableView indexPathForSelectedRow];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
NSLog(@"hostname = %@", controller.Serverinfo);
}
}

arrayA 是一个从 plist 中读取的 NSMutableArray,您可以在此处看到 nib

最佳答案

将您的方法替换为:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if ([arrayA[indexPath.row][@"SystemID"]isEqualToString:@"Ubuntu Linux"]) {
//ActionViewController *controller = [[ActionViewController alloc] initWithNibName:@"Ubuntu Linux" bundle:nil];
// Make sure you replace Storyboard with your storyboard name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
ActionViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"Ubuntu Linux"];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
else if ([arrayA[indexPath.row][@"SystemID"]isEqualToString:@"Arch Linux"]) {
ActionViewController *controller = [[ActionViewController alloc] initWithNibName:@"Arch Linux" bundle:nil];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
}

您不需要在 didSelectRow 中调用 [self performSegueWithIdentifier: segueName sender: self]; 因为您已经在 didSelectRow 方法中推送了您的 View 。如果你调用它,你会放置两次 View 。

关于ios - didSelectRowAtIndexPath 或 prepareForSegue 使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21062840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com