菜鸟教程小白 发表于 2022-12-12 23:33:52

ios - 当 viewController 转到另一个 tableViewController 时,我如何保存字符串的值


                                            <p><p>嗯,这个问题听起来很奇怪,但我找不到更好的方法。</p>

<p>我很确定这是一个基本错误,但我被卡住了。</p>

<p>我有一个主视图 Controller ,有 2 个按钮可以通向 2 个不同的 tableViewController。</p>

<p>我将使用这两种选择。</p>

<p>但是当我从一个 TableView 中获取选定的索引并转到下一个时,第一个的值变为空。</p>

<pre><code>if (tempFromLocationString!=NULL) {
    //tempFromLocationString=@&#34;asd&#34;;
    fromLocationLabel.text=tempFromLocationString;
}
if (tempToLocationString!=NULL) {
    toLocationLabel.text=tempToLocationString;
}
</code></pre>

<p>这就是我从 tableView 到 ViewController 的方式</p>

<pre><code>- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([ isEqualToString:@&#34;fromLocationSegue&#34;])
    {

         NSLog(@&#34;%@&#34;,selectionString);
ViewController *vc = ;
      vc.tempFromLocationString=selectionString;


}
}
</code></pre>

<p>这就是我获取所选单元格值的方式。</p>

<pre><code>- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


   selectionString=;

    NSLog(@&#34;%@&#34;,selectionString);


}
</code></pre>

<p>这是我的代码。我得到带有 segues 的临时字符串,我在 View 中应用这些代码确实加载了。</p>

<p>.h 文件中声明的所有 NSStrings。</p>

<p>流程是这样的;</p>

<p>用户进入应用程序,
选择一个按钮,
转到第一个 TableViewController
选择一个位置,
单击确定按钮并使用 segue ( selectionString) 返回到第一个 ViewController
标签被适本地设置为 selectionString </p>

<p>用户点击下一步按钮,
转到选择 TableView
选择一个位置
单击确定并返回第一个 ViewController ,现在第二个标签被适本地设置为 selectionString 但现在第一个被删除并且字符串变为空</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>好的
您的应用流程</p>

<p><strong>案例1</strong></p>

<ol>
<li><p>用户进入应用<strong>-正确</strong></p></li>
<li><p>选择一个按钮<strong>- 正确</strong></p></li>
<li><p>转到第一个 TableViewController 选择一个位置 <strong>-
    正确</strong></p></li>
<li><p>点击确定按钮<strong>- 正确</strong></p></li>
<li><p>然后使用 segue 回到第一个 ViewController
    (selectionString) 标签设置为 selectionString
    适当 - <strong>不正确</strong></p>

<p><strong>第 5 步</strong>不正确,为什么?</p>

<p><strong>Answer -</strong> 因为您在 tableViewController 中选择之后再次推送 <code>ViewController</code>,因为您的 <code>ViewController</code> 已经存在于堆栈中,所以在这里而不是使用 segue,您应该使用从 ViewController 获取的相同引用弹出 ViewController 。</p></li>
</ol>

<p><strong>案例2</strong></p>

<ol>
<li><p>用户点击下一步按钮 - <strong>正确</strong></p></li>
<li><p>转到选择 TableView 选择位置点击确定 - <strong>正确</strong></p></li>
<li><p>现在返回第一个 ViewController ,第二个标签被适本地设置为 selectionString 但现在第一个被删除并且字符串变为空 - <strong>不正确</strong></p><</li>
</ol>

<p><strong>第 3 步</strong>不正确,与 <strong>Case1</strong> 相同。</p>

<p><strong>Answer-</strong> 同样,您实际上并没有后退,而是在前进,所以发生的情况是您在选择时创建了一个新的 ViewController 实例,它没有先前选择的值。 </p>

<p><strong>解决方案</strong></p>

<ol>
<li><p>在每个相应的 tableViewController 中分别创建 <code>NSString</code> 属性,与在 <code>ViewController</code> 中的相同。</p></li>
<li><p>当你从 ViewController 中分离 tableViewController 时,分配属性就像</p>

<pre><code>TableViewController *vc = ;
vc.tempFromLocationString=self.tempFromLocationString;
</code></pre> </li>
<li><p>在 tableviewcontroller 中进行选择时,请执行以下操作</p>

<pre><code>self.tempFromLocationString=selectionString;
;
</code></pre> </li>
<li><p>现在不要在 <code>ViewController</code> 中的 <code>ViewDidLoad</code> 中赋值,而是在 <code>ViewWillAppear</code> 中进行赋值。</p></li>
</ol>

<p>希望对你有帮助。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 当 viewController 转到另一个 tableViewController 时,我如何保存字符串的值,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/24505652/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/24505652/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 当 viewController 转到另一个 tableViewController 时,我如何保存字符串的值