xcode 在大量 UIVIewControllers 之间传递一个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3984702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:51:58  来源:igfitidea点击:

Pass a variable between lots of UIVIewControllers

iphoneobjective-cxcodeuiviewcontrolleruinavigationcontroller

提问by fabian789

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.

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

采纳答案by user362178

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:

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

#import <UIKit/UIKit.h>

extern NSString *myGlobalString;

@interface MyFirstViewController : UIViewController {
...

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

然后,您可以在没有 extern 的情况下在 .m 文件中重新声明它。

#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.

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

回答by Ajay Sharma

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

是的,有很多简单的方法可以解决这个问题......

You can take a Global Variable

您可以使用全局变量

In your Delegate.h file declare your variable:

在你的 Delegate.h 文件中声明你的变量:

@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

其次在 Delegate.m 文件中

@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

现在在你的班级.m

- (void)viewDidLoad {
    [super viewDidLoad];

    appDel=[[UIApplication sharedApplication]delegate];

}

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

现在你可以通过这个对象在你班级的任何地方访问它:

appDel.messageString

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,

过着轻松的生活,

回答by starbugs

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

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

Declare a new class named SetupConfig, for example.

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

Your SetupConfig.h should then look as follows:

您的 SetupConfig.h 应如下所示:

@interface SetupConfig : NSObject {
    NSString *_myString;
}

@property (retain) NSString *myString;

+ (id)sharedSetupConfig;

@end

And the corresponding SetupConfig.m:

以及相应的 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:

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

@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.

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

回答by Francois

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

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

Singleton in objective-C, Wikipedia

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

回答by Alex Gosselin

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

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

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

如果您正在收集一些您认为是“设置”的内容,请考虑使用 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.

请记住,只有“传入”数据的路线才能带来额外的优势,以后可能您会想要多次收集该起始信息,并启动不同的可能的最后一次视图,而只需传入不同的视图即可。如果你走全球路线,你将不得不开始重写你之前访问过的任何地方。

回答by raghul

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

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