来自其他视图控制器的 xcode 访问类变量/属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5795155/
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 20:55:12  来源:igfitidea点击:

xcode access class variable/property from other view controller

xcodeclassvariablespropertiesinstance

提问by crimi

I have a UITabBar application with 4 tabs. Each tab has it's own ViewController.

我有一个带有 4 个选项卡的 UITabBar 应用程序。每个选项卡都有自己的 ViewController。

Sometimes I want to access variables from Tab1ViewController in Tav2ViewControllerTab3.

有时我想从 Tav2ViewControllerTab3 中的 Tab1ViewController 访问变量。

For example:

例如:

Tab1ViewController.h

Tab1ViewController.h

#import <UIKit/UIKit.h>
@interface Tab1ViewController : UIViewController {
NSMutableArray *contentArray;
}
@property (nonatomic, retain) NSMutableArray *contentArray;
@end

Tab2ViewController.m

Tab2ViewController.m

#import "Tab2ViewController.h"
#import "Tab1ViewController.h"
@implementation Tab2ViewController
- (void) viewDidLoad {
NSLog(@"Data count Tab1: %@", [Tab1ViewController.contentArray count]);
}

This is not working, because it seems as xcode expect contentArray to be a method. But how can I access the property from another ViewController? What am I doing wrong?

这是行不通的,因为 xcode 似乎希望 contentArray 是一种方法。但是如何从另一个 ViewController 访问该属性?我究竟做错了什么?

Thanks, Pat

谢谢,帕特

回答by Tejas

Do you have an instance of Tab1ViewController? You are trying to access the property on the class, not the instance of the class. This would only work if the property were static. I should also warn you that tabs that communicate with each other may indicate some lack of clarity in your app's design. Ideally, your view controllers should talk to a central place.

你有实例Tab1ViewController吗?您正在尝试访问类上的属性,而不是类的实例。这仅在属性是静态的情况下才有效。我还应该警告您,相互通信的选项卡可能表明您的应用程序设计缺乏清晰度。理想情况下,您的视图控制器应该与一个中心位置对话。

回答by Jonah

I suggest that you try to decouple your controllers from each other. When any controller can be aware of any other controller then a change to any one controller can require a change to all other controllers. That doesn't scale and makes your life as a developer unnecessarily difficult.

我建议您尝试将控制器彼此分离。当任何控制器可以知道任何其他控制器时,对任何一个控制器的更改都可能需要对所有其他控制器进行更改。这不会扩展,并使您作为开发人员的生活变得不必要地困难。

Instead there are a number of effective patterns for breaking your app up into self-contained modules which can still communicate but don't need that sort of application wide tight coupling.

相反,有许多有效的模式可以将您的应用程序分解为独立的模块,这些模块仍然可以进行通信,但不需要那种应用程序范围内的紧密耦合。

If you want to send events between controllers then consider the delegate pattern to allow one controller to communicate with a second, delegate, controller without requiring that delegate to be aware of the first controller. Better yet fire notifications to allow controllers to announce these events without any need to know about the other controller(s) listening for them.

如果您想在控制器之间发送事件,请考虑委托模式以允许一个控制器与第二个委托控制器进行通信,而无需该委托知道第一个控制器。更好的是触发通知,以允许控制器宣布这些事件,而无需知道其他控制器正在侦听它们。

If you want to share data between controllers then construct a shared model object (or service for obtaining models like a ManagedObjectContext) to store this data and give each controller a reference to it. That way your app delegate might construct this model and pass it to each controller. Each controller can observe changes to the model and make modifications to it as necessary without needing to be aware of the other controllers also using that model.

如果您想在控制器之间共享数据,则构造一个共享模型对象(或用于获取模型的服务,如 ManagedObjectContext)来存储此数据并为每个控制器提供对它的引用。这样你的应用程序委托就可以构建这个模型并将它传递给每个控制器。每个控制器都可以观察模型的变化并根据需要对其进行修改,而无需知道其他控制器也在使用该模型。

Apple's WWDC talk on MVC might be a useful guide for you.

Apple 的 WWDC 关于 MVC 的演讲对您来说可能是一个有用的指南。