简体   繁体   English

在视图之间以单例方式共享数据/字符串

[英]Sharing data/string with a singleton between views

I'm trying to share a string between two views on my iPhone project. 我正在尝试在我的iPhone项目的两个视图之间共享一个字符串。 It currently works if I use the actual @"something here" for the string, but if I want to use something like label.text, it doesn't even though it is still a string. 如果我在字符串中使用实际的@“ something here”,则当前可以使用,但是如果我想使用label.text之类的东西,即使它仍然是字符串,也不能使用。

I'll show you what I have to make it clearer. 我将向您展示我需要使其更清楚的内容。

First View: Info_ViewController.h 第一个视图:Info_ViewController.h

#import <UIKit/UIKit.h>

@interface Info_ViewController : UIViewController {

    IBOutlet UITextField *locationField;

}

@property (nonatomic, retain) NSString *locationString;

+ (id)sharedInfoVC;

@end

First View: Info_ViewController.m 第一视图:Info_ViewController.m

#import "Info_ViewController.h"

static Info_ViewController *sharedInfoVC = nil;

@implementation Info_ViewController
@synthesize locationString;


#pragma mark Singleton Methods
+ (id)sharedInfoVC {
    @synchronized(self) {
        if (sharedInfoVC == nil)
            sharedInfoVC = [[self alloc] init];
        }
    return sharedInfoVC;
}

- (id)init {
    if (self = [super init]) {
        locationString = [[NSString alloc] initWithString:locationField.text]; //This is there part I mentioned earlier, when using @"something" instead of locationField.text works.
    }
    return self;
}

Second View: Confirm_ViewController.m 第二视图:Confirm_ViewController.m

#import "Confirm_ViewController.h"
#import "Info_ViewController.h"

@implementation Confirm_ViewController

- (IBAction)buttonZ:(id)sender
{
    Info_ViewController *infoVCmanager = [Info_ViewController sharedInfoVC];
    locationLabel.text = infoVCmanager.locationString;
}

I put it under a button for now, but it will eventually be under viewDidLoad. 我暂时将其放在一个按钮下,但最终将在viewDidLoad下。 If you replace locationField.text with a string (@"blahblahblah") it won't crash and works. 如果您用字符串(@“ blahblahblah”)替换locationField.text,则它不会崩溃并且可以工作。

When it crashes I get the error: Program received signal: "SIGABRT" 崩溃时出现错误:程序收到信号:“ SIGABRT”

EDIT: I tried changing 编辑:我试图改变

initWithString:locationField.text

to

initWithFormat:@"%@",locationField.text

and now it my label in the second view prints "(NULL)" 现在它在第二个视图中的标签打印为“(NULL)”

Thanks for taking the time to give advice, I really appreciate it. 感谢您抽出宝贵的时间提出建议,我非常感谢。

It is an error to pass nil as the format string to -[NSString initWithString]. 将nil作为格式字符串传递给-[NSString initWithString]是错误的。

So how are you passing nil? 那么你如何过零呢? You actually have two instances of Info_ViewController. 实际上,您有两个 Info_ViewController实例。 You have the one instance which is the normal part of your app, and then you also have a second instance which is your "singleton" (which really isn't a singleton any more). 您有一个实例,这是您应用程序的正常部分,然后还有另一个实例,即您的“单例”(实际上不再是单例)。

So in your "singleton" instance, the UITextField is nil (and will always be nil) and so locationField.text is nil and you are passing that to initWithString:, which is a crash. 因此,在您的“单例”实例中,UITextField为nil(并且始终为nil),而locationField.text为nil,并且您将其传递给initWithString :,这很容易崩溃。 In fact the "singleton" isn't even fully baked as view controller's go. 实际上,随着视图控制器的发展,“单个”甚至还没有完全成熟。

If you want a singleton to share data elsewhere in your app, it really should not be a Info_ViewController or any type of view controller. 如果您希望单例在应用程序的其他位置共享数据,则它实际上不应是Info_ViewController或任何类型的视图控制器。 It should be of some other class that you use to manage your data. 它应该是用于管理数据的其他一些类。 I would create another class and implement that as a singleton. 我将创建另一个类并将其实现为单例。

Hope that helps you understand what's happening here. 希望可以帮助您了解此处的情况。

Pre-pend "self." 前置“自我”。 to your location string. 到您的位置字符串。

    self.locationString = [[NSString alloc] initWithString:locationField.text]; 

From what I understand of your code, you have got the value for locationString when you from the textfield when you initialize the viewController. 根据我对代码的了解,初始化viewController时,从文本字段中获取了locationString的值。 At this point of time, your textfield would not be visible. 此时,您的文本字段将不可见。 After it becomes visible and you enter something, you don't have the code to store it to locationString. 在它变得可见并输入内容之后,您没有将其存储到locationString的代码。

What you should do is wait for Info_ViewController object to be initialized and displayed. 您应该做的是等待Info_ViewController对象初始化并显示。 Then on the press of some button or some other event, assign locationLabel.text from the locationString or even directly from locationField.text. 然后在按下某个按钮或其他事件时,从locationString甚至直接从locationField.text分配locationLabel.text。

I would provide code, but I have no clue as to how you are structuring this. 我会提供代码,但是我不知道您如何构造它。 If you still need help, please provide the details. 如果仍然需要帮助,请提供详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM