在多视图应用程序中设置类实例(Objective C) [英] Setting up class instances in a multi view app (Objective C)

查看:169
本文介绍了在多视图应用程序中设置类实例(Objective C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective C,Xcode等等的新手。感谢您的帮助,阅读这个论坛,我做了一些正确的方向,但只有在单一视图应用程序。



现在我有我的故事板views:



(FirstViewController.h / m)
(SecondViewController.h / m)



我还创建了一个Objective C类,用于从这两个视图接收数据。



第一次,在FirstViewController中,我有一个IBA ACTION。



按下按钮时:

  controllo * 
control = [[controllo alloc] init];

然后我使用属性设置控制实例..和It工作。 >

现在controllo(控制)的同一个实例应该从我的SecondViewClass接收数据,但是我无法访问它,即使第一个视图的按钮(IBA ACTION)在传递到第二个视图之前按下。



您能告诉我我的项目中需要的所有视图是如何访问的?



感谢!

解决方案

将所有变量放入类中,通过singleton模式访问类,并将类头导入前缀.pch文件



示例:



GlobalClass.h

  #import< Foundation / Foundation.h> 

@interface GlobalClass:NSObject

@property(nonatomic,strong)NSString * globalString;
@property(nonatomic,strong)NSNumber * globalNumber;

+(GlobalClass *)sharedClass;

- (void)methodA;

@end

和GlobalClass.m

  #importGlobalClass.h

@implementation GlobalClass

+(GlobalClass *)sharedClass {

static GlobalClass * _sharedClass = nil;

static dispatch_once_t oncePredicate;

dispatch_once(& oncePredicate,^ {
_sharedClass = [[GlobalClass alloc] init];
});

return _sharedClass;
}

- (id)init {
self = [super init];
if(self){

//这里的初始化变量

}

return self;
}

- (void)methodA {

//在这里做一些事情
NSLog(@this is methodA called);

}

@end

您的.pch文件中的支持文件

  #importGlobalClass.h



现在你可以使用任何类来访问全局类变量:

  [GlobalClass sharedClass] .globalString = @这是一个全局字符串; 

您还可以使用以下方法访问该方法:

  [GlobalClass sharedClass] methodA]; 


I am a newbie in Objective C, Xcode and so on. Thanks to your help, reading this forum, i did some steps in the right direction, but only in a "Single View Application".

Now I have in my storyboard two views:

(FirstViewController.h / m) (SecondViewController.h /m)

I also created an Objective C class whom is meant to receive data from those two views.

At first time, in the FirstViewController i had an IBA ACTION.

When a button was pressed:

controllo *control;
control = [[controllo alloc] init];

and then i use to set "control" instances using properties .. and It worked.

Now The same instance of "controllo" (control) should receive data even from my SecondViewClass, but I cannot access to it, even if the button of the first view (IBA ACTION) is pressed BEFORE passing to the second view.

Could you please show me how have a class accessible from all the views I need in my project?

Thanks!

解决方案

Put all your variables inside a class, access the class via singleton pattern, and import the class header into the prefix.pch file

example :

GlobalClass.h

#import <Foundation/Foundation.h>

@interface GlobalClass : NSObject

@property (nonatomic, strong) NSString *globalString;
@property (nonatomic, strong) NSNumber *globalNumber;

+ (GlobalClass*) sharedClass;

- (void) methodA;

@end

and in GlobalClass.m

#import "GlobalClass.h"

@implementation GlobalClass

+ (GlobalClass *)sharedClass {

 static GlobalClass *_sharedClass = nil;

 static dispatch_once_t oncePredicate;

 dispatch_once(&oncePredicate, ^{
     _sharedClass = [[GlobalClass alloc] init];
 });

 return _sharedClass;
}

- (id)init {
    self = [super init];
    if (self) {

    //init variable here

    }

    return self;
}

- (void) methodA {

   //do something here
   NSLog(@"this is methodA called");

}

@end

put this inside your .pch file in supporting files

#import "GlobalClass.h"

now you can access the global class variable from any class, by using :

[GlobalClass sharedClass].globalString = @"this is a global string";

you can also access the method, by using :

[GlobalClass sharedClass] methodA];

这篇关于在多视图应用程序中设置类实例(Objective C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆