【问题标题】:Make one method in view controller globally and call it to many view controllers在全局视图控制器中创建一种方法并将其调用到多个视图控制器
【发布时间】:2016-12-01 10:39:49
【问题描述】:

我想创建一个方法并将其调用给许多视图控制器。帮我?我还想在该固定视图上分配 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];

}

但我的自定义视图没有显示在我调用此方法的视图控制器类中。

【问题讨论】:

  • 使用 Singleton 类或在 appdelegate 中声明方法。
  • 我知道先生,但我想要完整的代码。如果你有,请与我分享。
  • 你的项目是基于 swift 还是目标 c ?
  • 我的代码是基于Objective-C的
  • 亲爱的您可以在 View Controller 类中创建自定义视图,创建单例和优化很容易

标签: ios objective-c methods global


【解决方案1】:

像这样使用singleton

创建一个类,例如GlobalClass,类型为NSObject

在 .h 类中创建此方法

+ (GlobalClass *) sharedGlobalClass;
- (void) yourMethod : (UIView *) view;

现在在 .m 类中

+ (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"

【讨论】:

  • 先生,我还想在 "yourMethod" 上添加视图或按钮或标签或任何内容。我添加它但没有任何反应。请给我建议。
  • 如果你想添加一些东西,那么你需要传递你想要添加按钮或 UIViews 的 UIView
  • 我做到了,但 UIView 不可见。
  • 顺便说一句,这是另一个问题,您的问题中没有提到
  • 用你已经实现的正确代码和你期望的代码更新你的问题
【解决方案2】:

在目标 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];

如果您遇到任何问题,请告诉我。

【讨论】:

  • 当我在“全局方法”中分配 UIVIEW 时。我的视图不可见。方法被调用,但没有任何反应。
【解决方案3】:

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{}];

【讨论】:

    【解决方案4】:

    您可以为此目的使用单例类:

    创建一个 NSObject 类:

    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;
    }
    

    您可以在所有类中全局使用 someProperty。 它定义了一个静态变量,在 sharedManager 中只初始化一次。

    更多信息:http://www.galloway.me.uk/tutorials/singleton-classes/ https://code.tutsplus.com/articles/design-patterns-singletons--cms-23886

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多