在视图之间使用单例共享数据/字符串 [英] Sharing data/string with a singleton between views
问题描述
我正在尝试在我的iPhone项目中的两个视图之间共享一个字符串。如果我在字符串中使用实际的@something here,它目前有效,但如果我想使用像label.text这样的东西,它甚至不会是一个字符串。
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.
第一视图:Info_ViewController.h
First View: Info_ViewController.h
#import <UIKit/UIKit.h>
@interface Info_ViewController : UIViewController {
IBOutlet UITextField *locationField;
}
@property (nonatomic, retain) NSString *locationString;
+ (id)sharedInfoVC;
@end
第一视图:Info_ViewController.m
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;
}
第二个视图:Confirm_ViewController.m
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;
}
我现在把它放在一个按钮下,但它最终将在viewDidLoad下。
如果你用一个字符串(@blahblahblah)替换locationField.text,它将不会崩溃并且有效。
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.
当它崩溃时我得到错误:节目收到的信号:SIGABRT
When it crashes I get the error: Program received signal: "SIGABRT"
编辑:我尝试更改
initWithString:locationField.text
to
initWithFormat:@"%@",locationField.text
现在我在第二个视图中的标签打印(NULL)
and now it my label in the second view prints "(NULL)"
感谢您花时间提供建议,我真的很感激。
Thanks for taking the time to give advice, I really appreciate it.
推荐答案
将nil作为格式字符串传递给 - [NSString initWithString]是错误的。
It is an error to pass nil as the format string to -[NSString initWithString].
那你怎么通过nil?实际上你有两个的Info_ViewController实例。你有一个实例,它是你的应用程序的正常部分,然后你还有一个第二个实例,它是你的单身人士(它实际上不再是单身人士)。
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).
所以在你的单例实例中,UITextField是nil(并且将始终为nil),所以locationField.text为nil,你将它传递给initWithString:,这是一个崩溃。事实上,单身人士甚至没有完全像视图控制器一样。
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.
如果你想让单身人士在你的应用程序的其他地方共享数据,那真的不应该是Info_ViewController或任何类型的视图控制器。它应该是您用来管理数据的其他类。我会创建另一个类并将其实现为单例。
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.
这篇关于在视图之间使用单例共享数据/字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!