Xcode:故事板选项卡式应用程序来回传递数据 [英] Xcode: Storyboard Tabbed Application Passing Data Back and Forth

查看:37
本文介绍了Xcode:故事板选项卡式应用程序来回传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下代码尝试在查看相同 ViewController.h 和 ViewController.m 文件的 3 个视图控制器之间传递 UITextField 中的文本:

I am currently using the following code to try to pass the text in a UITextField between 3 view controllers that are looking at the same ViewController.h and ViewController.m files:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"NameOfSegue"]) {
        ViewController *ibcVC = segue destinationViewController;
        ibcVC.myTextField = self.myTextField;
    }
}

我按以下顺序链接了 3 个视图控制器:ViewC1 => ViewC2 => ViewC3.

I have 3 view controllers linked in the following order: ViewC1 => ViewC2 => ViewC3.

我的 UITextField 在 ViewC2 上.

例如,当我点击 ViewC2 上的按钮时,它推送 ViewC3,它将数据传递给 ViewC3.但是,假设我目前在 ViewC2 上,我在 UITextView 中输入了一些内容,然后单击导航上的后退按钮,Xcode 在使用选项卡式视图应用程序时会自动放置在那里,它将带我ViewC1 正如预期的那样.但是,如果我按下在 ViewC2 中推送的 ViewC1 上的按钮,我在 UITextField 中输入的数据/文本已被删除或重置为空.

When I click on the button on ViewC2 for example, that Pushes in ViewC3, it passes the data just fine to ViewC3. However, say I am currently on ViewC2, I type something in the UITextView, and I next click on the Back button on the Navigation that Xcode automatically puts there when working with tabbed view applications, it will take me to ViewC1 just fine as expected. However, if I push the button on ViewC1 that Pushes in ViewC2, the Data/Text I typed in my UITextField has been erased or reset to null.

所以基本上这里是使用轻微数字视觉的问题:

So basically here is the problem using a slight digital visual:

数据被正确推送.

ViewC1 => ViewC2 => ViewC3

如果我们按 NAVIGATION EX 上的返回按钮,数据将被删除.

ViewC1 <= ViewC2

推荐答案

一种解决方案是将字符串存储在单例中.

One solution is to store your string in a singleton.

共享字符串.h =

#import <Foundation/Foundation.h>

@interface SharedStrings : NSObject{
    NSString *string;
}

+(SharedStrings *)sharedString;
-(void)setString:(NSString *)newString;
-(NSString *)getString;

@end

SharedStrings.m =

SharedStrings.m =

#import "SharedStrings.h"

static SharedStrings *sharedString;

@implementation SharedStrings

-(id)init{

    self = [super init];
    string = [NSString new];
    return self;
}

+(SharedStrings *)sharedString{
    if (!sharedString) {
        sharedString = [[SharedStrings alloc] init];
    }
    return sharedString;
}

-(void)setString:(NSString *)newString{
    string = newString;
}
-(NSString *)getString{
    return string;
}

@end

然后,您可以根据需要获取所有视图并设置字符串,如下所示:

You could then have all your views get and set the string as necessary, like so:

- (void)viewWillAppear:(BOOL)animated
{
    [myTextField setText:[[SharedStrings sharedString] getString]];
    [super viewWillAppear:animated];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{

    if (textField == enterInfoTF) {
        [[SharedStrings sharedString] setString:textField.text];
    }
}

这篇关于Xcode:故事板选项卡式应用程序来回传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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