2016-12-01 78 views
-1

我想將一個方法調用到許多視圖控制器。幫我?我也想在該固定視圖上分配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]; 

} 

但是我的自定義視圖不會在我的視圖控制器類,其中顯示我稱這種方法。

+0

使用Singleton類或聲明的方法的appdelegate。 – Poles

+0

我知道先生,但我想要完整的代碼。如果你有,請與我分享。 –

+0

您的項目是基於快速還是客觀的? – Pushkraj

回答

0

使用一個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" 
+0

先生,我還想添加視圖或按鈕或標籤或任何關於「yourMethod」。我添加它,但沒有發生。請建議我。 –

+0

如果你想添加一些東西,那麼你需要傳遞你想添加的UIView按鈕或UIViews – Rajat

+0

我做到了,但UIView不可見。 –

0

您可以使用單獨的類用於此目的:

創建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

0

在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]; 
單身對象,並調用方法

讓我知道你是否面臨任何問題。

+0

當我在你的「全局方法」中分配UIVIEW。我的視圖不可見。方法被調用,但沒有發生。 –

0

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 
} 

現在導入的頭文件在其他viewcontrollers和使用它像

[[ClassA sharedInstance] customMethod{}]; 
相關問題