我们有两个 UIViewControllers
firstViewController
第二 View Controller
。当我们点击 UItableViewCell
时,我们希望它转到 SecondViewController
。
我们这样试过
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard;
destinationController = [[UserDetails alloc] init];
destinationController = (UserDetails *)[storyboard instantiateViewControllerWithIdentifier:@"User"];
destinationController.FriendlyName.text=[usernameFriendlyName objectAtIndex:indexPath.row];
UIBarButtonItem * backButton =[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
destinationController.navigationItem.backBarButtonItem = backButton;
[self.navigationController pushViewController:destinationController animated:TRUE];
}
请指导我们。我们的代码有什么问题。
最佳答案
不需要做这一切。只需将两个 viewController 与 Push segue 连接起来。 给 segue 一些标识符 并简单地将这一行写在 didSelectRowAtIndexPath
[self performSegueWithIdentifier: @"Your segue identifier" sender: self];
要将数据从一个 View 传递到另一个 View ,请使用 PrepareForSegue
方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"Your segue identifier"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
关于ios - 应用程序试图在 didSelectRowAtIndexPath 中将一个 nil View Controller 推送到目标上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32505160/