我在 Stackoverflow 上看到了很多类似的问题,我尝试了很多东西,但我似乎无法弄清楚这一点。我有多个 TableViewController 和 1 个 MainViewController。MainViewController 具有调用不同 TableViewControllers 的按钮,并且在选择表格单元时 tableViewController 会关闭。
问题是,每次我从我的任一 tableViewControllers 推送时,我都会推送我的 MainViewController 的一个新实例。我目前使用 Segues 在这些不同的控制器之间进行推送。
简而言之:当从 TableViewControllers 切换到 ViewController 时,我想防止 ViewController 作为新实例被推送,因为这样会删除我以前的数据输入。
我很确定我必须使用:
[self dismissModalViewController: withCompletion:]
performSegue
prepareForSegue
或者在一个类中设置一些全局变量并调用它们,但我还没有足够的经验来正确实现它。
最终结果的一个简单示例是:VC 中的 3 个文本字段。单击 textfield1 会打开 tableview1,单击单元格会更新 textfield1。Textfield2 打开 tableview2 等。
希望我足够清楚,如果需要可以发布示例代码。
编辑,发布代码(请记住,在情节提要中执行转场):TableViewExample.h:
@interface IndustryViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSArray *tableViewArray;}
@property (nonatomic, strong) IBOutlet UITableView *tableViewIndustry;
TableViewExample.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showIndustry"]) {
NSIndexPath *indexPath = [self.tableViewIndustry indexPathForSelectedRow];
ViewController *destViewController = segue.destinationViewController;
destViewController.industryText = [tableViewArray objectAtIndex:indexPath.row];
destViewController.industryTextName = [tableViewArray objectAtIndex:indexPath.row];
}}
然后在 ViewController.m 中,viewDidLoad:
[industry setTitle:industryText forState:UIControlStateNormal];
这些是我认为最重要的部分。