Sharing data/string with a singleton between views

寵の児 提交于 2019-12-10 12:17:00

问题


I'm trying to share a string between two views on my iPhone project. 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.

I'll show you what I have to make it clearer.

First View: 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

#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

#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. If you replace locationField.text with a string (@"blahblahblah") it won't crash and works.

When it crashes I get the error: Program received signal: "SIGABRT"

EDIT: I tried changing

initWithString:locationField.text

to

initWithFormat:@"%@",locationField.text

and now it my label in the second view prints "(NULL)"

Thanks for taking the time to give advice, I really appreciate it.


回答1:


It is an error to pass nil as the format string to -[NSString initWithString].

So how are you passing nil? You actually have two instances of 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. 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. 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.




回答2:


Pre-pend "self." to your location string.

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



回答3:


From what I understand of your code, you have got the value for locationString when you from the textfield when you initialize the viewController. 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.

What you should do is wait for Info_ViewController object to be initialized and displayed. Then on the press of some button or some other event, assign locationLabel.text from the locationString or even directly from locationField.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.



来源:https://stackoverflow.com/questions/8388048/sharing-data-string-with-a-singleton-between-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!