在许多 UIVIewController 之间传递变量

发布于 2024-09-28 02:34:39 字数 202 浏览 4 评论 0原文

在我的 iPhone 应用程序中,有一个设置助手可以帮助用户输入大量数据。它基本上是一个 UINavigationController,里面有很多 UIViewController。现在,在某个时刻,我想访问用户在他看到的第一个 UIViewController 中输入的变量。我可以使用 setter 方法在每个 UIViewController 之间传递变量,但我想有一种更简单的方法。

In my iPhone app, there is a setup assistant which helps users to input a lot of data. It's basically a UINavigationController with lots of UIViewControllers in it. Now, at a certain point, I want to access a variable that the user entered in the first UIViewController he saw. I could pass the variable between every UIViewController with a setter method, but I guess there is an easier way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

放飞的风筝 2024-10-05 02:34:39

如果需要,您可以用 C 风格声明全局变量或类变量。如果您希望同一个变量在 UIViewController 的多个子类中可用,您可以在第一个控制器的 .h 文件中将其声明为外部变量,例如:

#import <UIKit/UIKit.h>

extern NSString *myGlobalString;

@interface MyFirstViewController : UIViewController {
...

然后在 .m 中重新声明它不带 extern 的文件。

#import "MyFirstViewController.h"

NSString *myGlobalString;

@implementation MyFirstViewController 

您不应在其他 .m 或 .h 文件中重新声明它,但您可以在导入 MyFirstViewController.h 的所有文件中访问该变量。设置变量时,注意正确释放和保留它。这种全局变量很容易造成内存泄漏。

You can declare global or class variables in C style if you want to. If you want the same variable to be available in several of your sub classes of UIViewController, you'd declare it as an extern variable in the .h file of your first controller, for example:

#import <UIKit/UIKit.h>

extern NSString *myGlobalString;

@interface MyFirstViewController : UIViewController {
...

You'd then redeclare it in your .m file without the extern.

#import "MyFirstViewController.h"

NSString *myGlobalString;

@implementation MyFirstViewController 

You shouldn't redeclare it in the other .m or .h files, but you can access the variable in all files that import MyFirstViewController.h. When setting the variable, take care to release and retain it properly. It's easy to create a memory leak with this kind of global variable.

看透却不说透 2024-10-05 02:34:39

解决此问题的一种简单但可重用且可扩展的方法是使用单例。

例如,声明一个名为 SetupConfig 的新类。

然后,您的SetupConfig.h 应如下所示:

@interface SetupConfig : NSObject {
    NSString *_myString;
}

@property (retain) NSString *myString;

+ (id)sharedSetupConfig;

@end

以及相应的SetupConfig.m:

#import "SetupConfig.h"

SetupConfig *g_sharedSetupConfig = nil;

@implementation SetupConfig

@synthesize myString = _myString;

+ (id)sharedSetupConfig {
    if (!g_sharedSetupConfig) {
        g_sharedSetupConfig = [[SetupConfig alloc] init];
    }
}

@end

现在,在视图控制器实现中,您希望从以下位置访问myString:

@import "MyViewController.h"
@import "SetupConfig.h"

@implementation MyViewController

- (void)methodDoingSomethingWithSingletonString
{
    NSString *myString = [[SetupConfig sharedSetupConfig] myString];
} 

@end

与使用全局C 变量相比,单例方法具有许多优点。首先,您不必一遍又一遍地重新声明全局变量。更重要的是,您的“全局”变量被封装在一个类中。综合属性 getter/setter 是将实际变量从代码的其余部分中抽象出来的好方法。最后,这个实现可以很容易地集成到单元测试中。

A simple yet reusable and extensible way to solve this problem is using a singleton.

Declare a new class named SetupConfig, for example.

Your SetupConfig.h should then look as follows:

@interface SetupConfig : NSObject {
    NSString *_myString;
}

@property (retain) NSString *myString;

+ (id)sharedSetupConfig;

@end

And the corresponding SetupConfig.m:

#import "SetupConfig.h"

SetupConfig *g_sharedSetupConfig = nil;

@implementation SetupConfig

@synthesize myString = _myString;

+ (id)sharedSetupConfig {
    if (!g_sharedSetupConfig) {
        g_sharedSetupConfig = [[SetupConfig alloc] init];
    }
}

@end

Now, in the view controller implementation you want to access myString from:

@import "MyViewController.h"
@import "SetupConfig.h"

@implementation MyViewController

- (void)methodDoingSomethingWithSingletonString
{
    NSString *myString = [[SetupConfig sharedSetupConfig] myString];
} 

@end

The singleton approach comes with a number of advantages over using a global C variable. First of all you do not have to re-declare your global variables over and over. What is more, your "global" variables are encapsulated in a class. Synthesizing property getters/setters is a nice way to abstract the actual variable away from the rest of your code. Finally, this implementation may be integrated into unit tests easily.

仙女山的月亮 2024-10-05 02:34:39

是的,有很多简单的方法来处理这个问题......

一个全局变量

您可以在 Delegate.h 文件中声明

@interface Smoke_ApplicationAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *navigationController;
    NSString *messageString;  //This would be your String Variable

}

@property(nonatomic,retain)NSString *messageString;

:其次在 Delegate.m 文件中

@implementation Smoke_ApplicationAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize messageString;  // Synthesize it over here..

这就完成了。现在您可以在中使用这个字符串变量您想要的所有/任何类..

使用此全局变量。

只需导入您的委托文件
制作它的 obj...

#import "DelegateFile.h"

@implementation About

 DelegateFile *appDel;

现在在您的 class.m

- (void)viewDidLoad {
    [super viewDidLoad];

    appDel=[[UIApplication sharedApplication]delegate];

}

现在您可以通过此对象在类中的任何位置访问它:

appDel.messageString

只需仔细按照我的步骤操作即可
在我的手指经历了如此多的痛苦之后,
我相信这肯定会对您有所帮助......

祝您生活轻松,

Ya , there is much a easy way to handle this.....

You can take a Global Variable

In your Delegate.h file declare your variable:

@interface Smoke_ApplicationAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *navigationController;
    NSString *messageString;  //This would be your String Variable

}

@property(nonatomic,retain)NSString *messageString;

Secondly in Delegate.m file

@implementation Smoke_ApplicationAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize messageString;  // Synthesize it over here..

This is Done .Now you can use this String Variable in All/any class you want..

To use this Global Variable.

Just import you Delegate file
make the obj of it....

#import "DelegateFile.h"

@implementation About

 DelegateFile *appDel;

Now in Your class.m

- (void)viewDidLoad {
    [super viewDidLoad];

    appDel=[[UIApplication sharedApplication]delegate];

}

Now you can access it anywhere in your class by this Object:

appDel.messageString

Just follow my Steps Carefully
After giving so much pain to my finger,
I am sure this is definitely going to help you.....

Have a easy life,

烈酒灼喉 2024-10-05 02:34:39

您可以使用所有类都可用的单例实例,它将处理您需要的所有信息。

objective-C 中的单例,维基百科

You can use a singleton instance, available from all your classes, that will handles all the information you need.

Singleton in objective-C, Wikipedia

有深☉意 2024-10-05 02:34:39

我不会太快地规避数据封装,这是 Objective-C 的一个很好的特性。

如果您正在收集一些您认为是“设置”的内容,请考虑使用 NSUserDefaults。

如果您的视图以结构化的方式进行,请考虑创建一个“数据”类,无论您正在创建什么,然后将其从父视图传递到子视图,直到到达那里。 (请记住,“传递”并不是一个昂贵的操作,内容保持不变,您正在传递一个小指针)

如果您确实想要单例路由,请考虑将其作为应用程序委托的属性(已经存在的单例)

记住只有“传入”数据的路线才能提供额外的优势,即稍后您可能会想要多次收集该起始信息,并启动不同的可能的最后视图,并且只需传入不同的视图就很简单了。如果您走全球路线,那么您将不得不开始重写之前访问过的所有地方。

I wouldn't be too quick to circumvent the data encapsulation that is such a good feature of Objective-C.

If you are collecting something you would consider to be "Settings" consider using NSUserDefaults.

If your views proceed in a structured one to the next to the next way, consider making a "data" class, whatever it is you're making, then pass it along from parent view to subview until you get there. (Remember that "passing" is not an expensive operation, the stuff stays put, you're passing a little pointer)

If you really want the singleton route, consider making it a property of the application delegate (an already existing singleton)

Remember that only the route of "passing in" the data gives the added advantage that later, maybe you will want to collect that starting information multiple times, and launch different possible last-views, and it's trivial to just pass in a different one. If you go the global route you'll then have to start re-writing everywhere you accessed it before.

樱娆 2024-10-05 02:34:39

您可以将其用作单例,也可以将其保留在应用程序委托中并像这样调用
[appdelegate getstring];

You can use it as a singleton or you can keep it in the app delegate and call like
[appdelegate getstring];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文