我想將一個方法調用到許多視圖控制器。幫我?我也想在該固定視圖上分配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。 – Poles
我知道先生,但我想要完整的代碼。如果你有,請與我分享。 –
您的項目是基於快速還是客觀的? – Pushkraj