从控制器返回到上一个控制器 [英] Moving back from a Controller to a previous one

查看:43
本文介绍了从控制器返回到上一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 UIViewController 切换回我的 UITableViewController 但显然它没有按照我想要的方式工作.我确实从一开始就用故事板创建了我的界面,所以我并没有真正通过代码从一个视图移动到另一个视图.到现在为止,我只是按 sergues 推送我的视图,这很容易用故事板实现.但是当我尝试返回我之前的视图时,将实现一个新的 ViewController,因此我存储在旧视图中的所有数据都丢失"了.

I'm trying to switch back from my UIViewController to my UITableViewController but apperendly it's not working the way I want it to. I did create my Interface with storyboards from the start, so I'm not really into moving from one View to another by code. Until now, I just pushed my Views per sergues which are easy to implement with storyboards. But when I tried moving back to my previous Views, a new ViewController will be implemented, so all my data I stored in the old one is "lost".

实际上我没有可以向您展示的代码(因为 sergues),但情况是:

Actually there is no code I could present you (because of the sergues) but the situation is:

->我在 UITableView 中有一个 MutableArray 存储东西.

->I got an MutableArray storing stuff in my UITableView.

->用户点击扫描按钮,扫描一个应该导入到我的阵列中的新项目.

->User taps the scan Button, scans a new Item which should be imported in my array.

->当试图推回我的 tableView 时,有一个新的控制器在等待我,不知道我存储在旧控制器中的数据.

->When trying to push back to my tableView there's a new Controller awating me, unaware of the data I stored in the old one.

那么我如何简单地移回我的旧控制器,防止一直创建一个新控制器?

So how do I simply move back to my old Controller preventing to create a new one all the time?

sergue-push 是否总是创建一个新的控制器?

Does a sergue-push always creates a new Controller?

(问题可能很简单,但我是这个东西的新手.尝试了搜索功能提供的一些结果,但没有一个奏效:( )

(Question may be simple, but I'm new to this stuff. Tried some results presented by the search function, but none of them worked :( )

/edit: 弹回问题已解决,但数组填充问题仍然存在.代码:

/edit: Popback problem has been solved but array filling problem still exists. Code:

GeneralSettings *sharedGS = [GeneralSettings sharedInstance];
sharedGS.strEAN = [[NSString alloc] initWithString:strCheckString];
[sharedGS.listArray insertObject:strCheckString atIndex:0];
NSLog(@"Anzahl der EAN-Codes: %d
Erster Code: %@
 In Variable: %@", sharedGS.listArray.count, [sharedGS.listArray objectAtIndex:0],sharedGS.strEAN);

sharedGS.strEAN 中的数据(即83274572354")listArray 中的数据(空)

Data in sharedGS.strEAN ("83274572354" i.e.) Data in listArray (null)

推荐答案

你不会退缩.这将创建前一个控制器类的新实例.你弹回来.您可以通过两种方式实现.

You don't push back. That creates a new instance of the previous controller class. You pop back. You can accomplish that 2 ways.

1:当您想返回(弹出)到平板电脑视图控制器时,在代码中放入以下语句.

1: In code put in the following statement when you want to return (pop) to you tablet view controller.

[self.navigationController popViewControllerAnimated:YES];

2:如果你想在 storyboard 中实现,你需要实现以下自定义 segue 类:

2: If you want to do it in storyboard you need to implement the following custom segue class:

实施

//  PopSegue.m

#import "PopSegue.h"

@implementation PopSegue

- (void) perform {

    UIViewController *src = (UIViewController *) self.sourceViewController;
[src.navigationController popViewControllerAnimated:YES];
}

和标题

//  PopSegue.h

#import <UIKit/UIKit.h>

@interface PopSegue : UIStoryboardSegue

@end

将此方法放在您的 UIViewController 中以将属性设置回您的 UITableViewController:

Place this method in your UIViewController to set a property back to your UITableViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"goBackToTableView"])  {
    [[segue destinationViewController] set{whatEverProperyYouWantTo}: {valueOfPropertyToSet}];
    }
}

这篇关于从控制器返回到上一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆