【发布时间】: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