在全局视图控制器中创建一个方法并将其调用到多个视图控制器
Make one method in view controller globally and call it to many view controllers
我想制作一个单一的方法并调用它给许多视图控制器。帮我?我还想在该固定视图上分配 UIView 或按钮或标签。
这是我的代码
.h
#import <UIKit/UIKit.h>
@interface UIOnlyView : UIView
+ (UIOnlyView *) sharedGlobalClass;
-(void)yourMethod;
@end
.m
+(UIOnlyView *)sharedGlobalClass {
static dispatch_once_t pred;
static id shared = nil;
dispatch_once(&pred, ^{
shared = [[super alloc] init];
});
return shared;
}
-(void)yourMethod{
NSLog(@"Method called");
UIView *customView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
customView.backgroundColor=[UIColor redColor];
[self addSubview:customView];
}
但是我的自定义视图没有显示在我调用此方法的视图控制器 class 上。
像这样使用singleton
创建一个 class 例如 GlobalClass
类型 NSObject
在.h中class创建这个方法
+ (GlobalClass *) sharedGlobalClass;
- (void) yourMethod : (UIView *) view;
现在在 .m class
+ (GlobalClass *) sharedGlobalClass {
static dispatch_once_t pred;
static id shared = nil;
dispatch_once(&pred, ^{
shared = [[super alloc] init];
});
return shared;
}
- (void) yourMethod : (UIView *) view {
UIView *customView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
customView.backgroundColor=[UIColor redColor];
[view addSubview:customView];
}
现在你可以像这样调用这个方法
[[GlobalClass sharedGlobalClass] yourMethod:self.view];
从您的任何 ViewController,您只需导入
#import "GlobalClass.h"
您可以为此目的使用单例 class:
创建一个 NSObject class:
import <foundation/Foundation.h>
@interface MyManager : NSObject {
NSString *someProperty;
}
@property (nonatomic, retain) NSString *someProperty;
+ (id)sharedManager;
@end
在您的 .m 文件中:
导入“MyManager.h”
@implementation MyManager
@synthesize someProperty;
+ (id)sharedManager {
static MyManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
您可以在所有 class 中全局使用 someProperty。
它定义了一个在 sharedManager 中初始化一次且仅初始化一次的静态变量。
更多信息:http://www.galloway.me.uk/tutorials/singleton-classes/
https://code.tutsplus.com/articles/design-patterns-singletons--cms-23886
在Objective C中:
@interface OUCSCalendarManager ()
- (void)globalMethod;
@end
@implementation OUCSCSharedManager
/// ------------------------------------------------------------------------
/// Singleton Method
///--------------------------------------------------------------------------
+(instancetype)SharedManager{
@synchronized(self) {
static OUCSCalendarManager *sharedInstance = nil;
static dispatch_once_t pred;
@synchronized (self) {
dispatch_once(&pred, ^{
sharedInstance = [[OUCSCalendarManager alloc]init];
});
return sharedInstance;
}
}
}
- (void)globalMethod {
}
}
要从项目中的任何位置调用该方法,您需要创建单例对象并像这样调用方法
[[OUCSCalendarManager calendarSharedManager]globalMethod];
如果您遇到任何问题,请告诉我。
ClassA.h
#import "ClassA.h"
@interface ClassA : NSObject
+(id)sharedInstance;
-(void)customMethod;
@end
现在实施ClassA.m
#import "ClassA.h"
@implementation ClassA
+(id)sharedInstance
{
static ClassA *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[ClassA alloc] init];
});
return instance;
}
-(void)customMethod
{
//Method body
}
现在在其他视图控制器中导入头文件并像使用它一样
[[ClassA sharedInstance] customMethod{}];
我想制作一个单一的方法并调用它给许多视图控制器。帮我?我还想在该固定视图上分配 UIView 或按钮或标签。
这是我的代码
.h
#import <UIKit/UIKit.h>
@interface UIOnlyView : UIView
+ (UIOnlyView *) sharedGlobalClass;
-(void)yourMethod;
@end
.m
+(UIOnlyView *)sharedGlobalClass {
static dispatch_once_t pred;
static id shared = nil;
dispatch_once(&pred, ^{
shared = [[super alloc] init];
});
return shared;
}
-(void)yourMethod{
NSLog(@"Method called");
UIView *customView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
customView.backgroundColor=[UIColor redColor];
[self addSubview:customView];
}
但是我的自定义视图没有显示在我调用此方法的视图控制器 class 上。
像这样使用singleton
创建一个 class 例如 GlobalClass
类型 NSObject
在.h中class创建这个方法
+ (GlobalClass *) sharedGlobalClass;
- (void) yourMethod : (UIView *) view;
现在在 .m class
+ (GlobalClass *) sharedGlobalClass {
static dispatch_once_t pred;
static id shared = nil;
dispatch_once(&pred, ^{
shared = [[super alloc] init];
});
return shared;
}
- (void) yourMethod : (UIView *) view {
UIView *customView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
customView.backgroundColor=[UIColor redColor];
[view addSubview:customView];
}
现在你可以像这样调用这个方法
[[GlobalClass sharedGlobalClass] yourMethod:self.view];
从您的任何 ViewController,您只需导入
#import "GlobalClass.h"
您可以为此目的使用单例 class:
创建一个 NSObject class:
import <foundation/Foundation.h>
@interface MyManager : NSObject {
NSString *someProperty;
}
@property (nonatomic, retain) NSString *someProperty;
+ (id)sharedManager;
@end
在您的 .m 文件中:
导入“MyManager.h”
@implementation MyManager
@synthesize someProperty;
+ (id)sharedManager {
static MyManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
您可以在所有 class 中全局使用 someProperty。 它定义了一个在 sharedManager 中初始化一次且仅初始化一次的静态变量。
更多信息:http://www.galloway.me.uk/tutorials/singleton-classes/ https://code.tutsplus.com/articles/design-patterns-singletons--cms-23886
在Objective C中:
@interface OUCSCalendarManager ()
- (void)globalMethod;
@end
@implementation OUCSCSharedManager
/// ------------------------------------------------------------------------
/// Singleton Method
///--------------------------------------------------------------------------
+(instancetype)SharedManager{
@synchronized(self) {
static OUCSCalendarManager *sharedInstance = nil;
static dispatch_once_t pred;
@synchronized (self) {
dispatch_once(&pred, ^{
sharedInstance = [[OUCSCalendarManager alloc]init];
});
return sharedInstance;
}
}
}
- (void)globalMethod {
}
}
要从项目中的任何位置调用该方法,您需要创建单例对象并像这样调用方法
[[OUCSCalendarManager calendarSharedManager]globalMethod];
如果您遇到任何问题,请告诉我。
ClassA.h
#import "ClassA.h"
@interface ClassA : NSObject
+(id)sharedInstance;
-(void)customMethod;
@end
现在实施ClassA.m
#import "ClassA.h"
@implementation ClassA
+(id)sharedInstance
{
static ClassA *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[ClassA alloc] init];
});
return instance;
}
-(void)customMethod
{
//Method body
}
现在在其他视图控制器中导入头文件并像使用它一样
[[ClassA sharedInstance] customMethod{}];