Ios 通用类更新ViewController中的控件

Ios 通用类更新ViewController中的控件,ios,xcode,Ios,Xcode,我是一名经验丰富的开发人员,精通多种语言,但对使用Xcode for IOS进行开发还相当陌生 我有一个问题,我已经努力解决了几个星期了。我试过这个网站和其他网站上的几个概念,但都没有用 与其把我尝试过的每件事都一一列举出来,不如让我来概括一下我正在努力实现的目标。有人能提供一些搜索关键词的提示,或者任何与我正在寻找的内容类似的链接吗 我有几个视图控制器和一个用于处理的公共类。在公共类中有一个计时器,它在触发时调用同一类中的一些代码。从那里,我需要能够获取和设置视图控制器中控件的属性 我发现的最

我是一名经验丰富的开发人员,精通多种语言,但对使用Xcode for IOS进行开发还相当陌生

我有一个问题,我已经努力解决了几个星期了。我试过这个网站和其他网站上的几个概念,但都没有用

与其把我尝试过的每件事都一一列举出来,不如让我来概括一下我正在努力实现的目标。有人能提供一些搜索关键词的提示,或者任何与我正在寻找的内容类似的链接吗

我有几个视图控制器和一个用于处理的公共类。在公共类中有一个计时器,它在触发时调用同一类中的一些代码。从那里,我需要能够获取和设置视图控制器中控件的属性

我发现的最接近的示例依赖于视图控制器调用公共类的代码,因此我们有一个句柄,允许我更新标签控件文本。但是这不是我想要的,因为我需要获取和设置多个视图控制器的属性,而不必让视图控制器调用公共类

我尝试过委托、协议和其他,但我尝试过的所有示例都不符合这一点

非常感谢

[编辑以添加最新尝试的示例] 我在vcHome.m中将一个公共类实例化为common,然后在该公共类中调用一个方法

vcHome.h:

@interface vcHome : UIViewCOntroller
{
  // Make this control available to other classes
  UILabel *label1;
}

@property (nonatomic, retain) IBOutlet UILabel *label1;
vcHome.m:

-(void)viewDidLoad
{  
  ...
  //Pass vcHome to clsCommon
  [common saveVCHome:self];
}
然后在clsCommon.h中:

// Declare out shared instance global var storage
+(clsCommon *) sharedInstance; 

// Property to store a pointer to vcHome via global var storage
@property (nonatomic) UIViewController *vcHome;

-(void)saveVCHome:(UIViewController *)vcHome;
和clsCommon.m:

#import <vcHome.h>
...

// The global var storage
+(clsCommon) sharedInstance
{
  static clsCommon *myInstance = nil;
  if(myInstance == nil) {myInstance = [[self class] alloc] init];}
}

-(void)saveVCHome:(vcHome *)vcHomePassed
{
  [clsCommon sharedInstance].vcHome = &vcHomePassed;
}
-(void)timerFire
{
  [clsCommon sharedInstance].vcHome.label1.text = @"updated from clsCommon";
}
- (void)processSomething {
  // Some processing changes information that is ultimatley displayed on the Info screen.  Tell the Info screen to update.
    [[NSNotificationCenter defaultCenter]postNotificationName:@"updateInfoScreen" object:nil];
  // Some processing changes information that is ultimatley displayed on the History screen.  Tell the History screen to update.
    [[NSNotificationCenter defaultCenter]postNotificationName:@"updateInfoScreen" object:nil];
}
关于这一点,有两个问题。 1.我收到一条关于间接指针和objective-c指针的错误消息,所以我的语法是关闭的。你知道我做错了什么吗? 2.这可能吗


我尝试过重新实例化vcHome视图控制器的方法,但这将是一个新实例,当然不允许我设置在最初实例化的视图控制器中显示的控件属性,因此,通过尝试在此处保存指向原始对象的指针。

如果您希望您的公共类向所有视图控制器发出“做点什么”消息,您有两个选择,我可以立即想到:

一,

您可以创建UIViewController的子类,例如JohnViewController,然后将所有后续视图控制器基于该子类,这意味着它们都可以继承相同的属性以及setter和getter

二,


您可以让视图控制器子类注册,以便在设置和/或获取时观察您的公共类发出的消息。

非常感谢Michael为代表们提供的建议。这样做是可行的,但在这种情况下不行,因为除非我错了,否则一个类我的视图控制器可以是多个委托,但一个类我的公共类不能委托给多个视图控制器

我最终非常简单地通过通知实现了这一点。公共类不是试图让公共类直接获取/设置视图控制器中的控件,而是在共享实例中为所有视图控制器/类设置要使用的属性,然后根据需要向相应的视图控制器发送通知

请注意,这不仅非常容易设置,而且还可以处理多个post-listen对

clsCommon.m:

#import <vcHome.h>
...

// The global var storage
+(clsCommon) sharedInstance
{
  static clsCommon *myInstance = nil;
  if(myInstance == nil) {myInstance = [[self class] alloc] init];}
}

-(void)saveVCHome:(vcHome *)vcHomePassed
{
  [clsCommon sharedInstance].vcHome = &vcHomePassed;
}
-(void)timerFire
{
  [clsCommon sharedInstance].vcHome.label1.text = @"updated from clsCommon";
}
- (void)processSomething {
  // Some processing changes information that is ultimatley displayed on the Info screen.  Tell the Info screen to update.
    [[NSNotificationCenter defaultCenter]postNotificationName:@"updateInfoScreen" object:nil];
  // Some processing changes information that is ultimatley displayed on the History screen.  Tell the History screen to update.
    [[NSNotificationCenter defaultCenter]postNotificationName:@"updateInfoScreen" object:nil];
}
并在视图控制器类中侦听这些通知

vcInfo.m:

- (void) viewDidLoad {
 //Listen for notifications
   [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateInfo) name:@"updateInfoScreen" object:nil];
}

- (void) dealoc {
  //This method handles stopping the listener when the app is shut down (or this view controller is dismissed)
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"updateInfoScreen" object:nil];
}

- (void) updateInfo {
  //Put code here that does whatever you want to update this screen
}
vcHistory.m:

- (void) viewDidLoad {
 //Listen for notifications
   [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateHistory) name:@"updateHistoryScreen" object:nil];
}

- (void) dealoc {
  //This method handles stopping the listener when the app is shut down (or this view controller is dismissed)
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"updateHistoryScreen" object:nil];
}

- (void) updateHistory {
  //Put code here that does whatever you want to update this screen
}

嘿,迈克尔,谢谢你的回答。我喜欢你的第一个建议。我在尝试中遇到的问题是,当我从公共类实例化视图控制器时,这是一个新实例。您知道引用视图控制器原始实例的方法吗?每个视图控制器必须是一个新实例和一个新对象。如果您想保留某种指向原始实例的句柄或指针,您可以在NSNotification中将其作为发送通知的进程传递,我已经在测试中做到了这一点。然而,我的问题是,也许我应该在一开始就说,我使用的是故事板和选项卡控制器。因此,所有ViewController都是在我的控制范围之外实例化的-没有机会传递任何信息。也许问题是如何在不接收调用的情况下获取指向已实例化viewcontroller的指针?