【问题标题】:How to preserve text view input when going back to the story board返回故事板时如何保留文本视图输入
【发布时间】:2014-06-15 23:40:08
【问题描述】:

我的应用上有一个文本视图。我还有一个关于/帮助按钮。当他们单击关于/帮助按钮时,它会转到另一个故事板。当他们点击“返回”按钮时,它会返回到原来的故事板。

我遇到的问题是,当他们回到原始故事板时,我该如何做到这一点,文本视图不会恢复到原来的状态,而是保留用户的输入有吗?

好的,当用户按下一个名为“转换”的按钮时,我有这段代码。

- (IBAction)convertButton:(id)sender {

    //Makes the text view into editable NSString
    NSString *input = _inputTextField.text;

    //Changes words that the user input.
    input = [input stringByReplacingOccurrencesOfString@"cool" withString:@"awesome"];
    //Some code here changes it to input2
    //Sends the edited input to the output text field
    _outputTextField.text = input2;
}

这意味着如果用户输入“我很酷”。进入 inputTextField,然后按转换按钮,我希望它保存他的输入,以防用户单击将他带到另一个窗口的“关于/帮助”按钮,因为当他返回时,我希望他的旧输入保留在输入框中。但它并没有这样做。

【问题讨论】:

  • 您需要以某种方式存储此值。然后将该值加载回 viewWillAppear 事件或类似事件的框中
  • 我知道......这就是我的问题......怎么做。你根本没有帮助我。
  • 很抱歉你有这种感觉,但如果你想要更深入的帮助,你需要付出更多的努力(你尝试过的代码,更尖锐的问题......)
  • 为了更好地理解它,我需要创建一个全局字符串,当用户单击“转换”按钮或外部触摸时将输入2保存到其中。
  • 解决方案真的应该放在答案中。

标签: iphone uitextview uistoryboard preserve


【解决方案1】:

您可以在 ViewController 中的 viewWillAppear 方法中将文本设置为 textview。 VC出现时会调用该方法。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    myTextView.text = @"12345\nabcde\nABCDE";
}

更新:

@interface ViewController () <UITextViewDelegate>
@property (nonatomic, retain) NSString *text;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.text = myTextView.text;
    myTextView.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    myTextView.text = self.text;
}

- (void)textViewDidChange:(UITextView *)textView
{
    self.text = textView.text;
}

【讨论】:

  • 对不起,但这并没有什么不同。我需要使“_inputTextField.text”等于用户输入的“*input”。我尝试将它设置为等于“*input”,但它说“*input”在该实例中未声明。我必须让它在按下转换后保存输入,如果触摸文本字段之外的任何地方。
  • @property (nonatomic, 保留) NSString *text;在 appDelegate.h 文件中声明这一点可能会对您有所帮助..
  • 这让我很困惑。我不知道将什么放入哪些文件中。我不断收到一堆错误。
【解决方案2】:

OP 的解决方案。

我们要做的就是:

  1. 确保returned: 的代码在原始 .m 文件中:

    //When the back button is pressed, this is what happens.
    -(IBAction)returned:(UIStoryboardSegue *)segue {
    }
    
  2. 使“返回按钮”连接到情节提要底部的“退出”符号,然后从菜单中选择“返回:”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多