Ios 窗口目标c上的永久视图
我需要将UIView添加到主窗口,UIView包含在带有一些按钮的视图中。 问题是,我放在视图中的按钮对触摸事件没有响应, 就像前面有一些风景一样。 该视图需要是一个单例类,因为我需要在任何类中响应touchevent 以下是UIView的代码:Ios 窗口目标c上的永久视图,ios,objective-c,uiview,window,Ios,Objective C,Uiview,Window,我需要将UIView添加到主窗口,UIView包含在带有一些按钮的视图中。 问题是,我放在视图中的按钮对触摸事件没有响应, 就像前面有一些风景一样。 该视图需要是一个单例类,因为我需要在任何类中响应touchevent 以下是UIView的代码: + (MenuBarView *)sharedMenuBar { static MenuBarView *sharedSingleton; @synchronized(self) { if (!sharedSingle
+ (MenuBarView *)sharedMenuBar
{
static MenuBarView *sharedSingleton;
@synchronized(self) {
if (!sharedSingleton) sharedSingleton = [[MenuBarView alloc] init];
return sharedSingleton;
}
}
-(id)init
{
self = [super init];
if(self)
{
self.userInteractionEnabled = YES;
backgroundBarImage = [UIImage imageNamed:@"Barra.png"];
UIImageView * backgroundBar = [[UIImageView alloc]initWithImage:backgroundBarImage];
backgroundBar.contentMode = UIViewContentModeCenter;
backgroundBar.backgroundColor = [UIColor clearColor];
[backgroundBar setFrame:CGRectMake(0, 0, backgroundBarImage.size.width, backgroundBarImage.size.height)];
[self addSubview:backgroundBar];
UIButton * rootBTN = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[rootBTN setFrame:CGRectMake(0, 8, 100, 40)];
[rootBTN addTarget:self action:@selector(selectedBarButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rootBTN];
UIImage * localizacaoIMG = [UIImage imageNamed:@"Localizador"];
UIImageView * localizacaoView = [[UIImageView alloc]initWithImage:localizacaoIMG];
localizacaoView.contentMode = UIViewContentModeScaleAspectFill;
[localizacaoView setFrame:CGRectMake(backgroundBar.frame.origin.x+130, 8, localizacaoIMG.size.width, localizacaoIMG.size.height)];
[backgroundBar addSubview:localizacaoView];
UIButton * localizacaoBTN = [UIButton buttonWithType:UIButtonTypeCustom];
[localizacaoBTN setFrame:CGRectMake(backgroundBar.frame.origin.x+110, 8, 60, 40)];
localizacaoBTN.tag = 1;
[self addSubview:localizacaoBTN];
}
return self;
}
//The event handling method
-(void)selectedBarButton:(UIButton *)sender
{
NSLog(@"OK");
[self.delegate selectedMenuBar:sender.tag];
}
下面是AppDelegate上的实现:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[InitialViewController alloc] init];
self.navController = [[EzMallNavViewController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
if(IS_IPOD)
{
[[MenuBarView sharedMenuBar]setFrame:CGRectMake(0, self.window.frame.size.height-51, [MenuBarView sharedMenuBar].frame.size.width, [MenuBarView sharedMenuBar].frame.size.height)];
}
else
{
[[MenuBarView sharedMenuBar]setFrame:CGRectMake(0, self.window.frame.size.height, [MenuBarView sharedMenuBar].frame.size.width, [MenuBarView sharedMenuBar].frame.size.height)];
}
[MenuBarView sharedMenuBar].delegate = self;
[self.window addSubview:[MenuBarView sharedMenuBar]];
return YES;
}
#pragma mark MenuBarDelegateMethods
-(void)selectedMenuBar:(int) tag
{
NSLog(@"Here");
}
看起来您的菜单视图的边框大小为零,因此未检测到任何触摸。按钮出现在屏幕上,因为菜单视图未设置为将图形剪裁到其边界。尝试为不同的视图设置不同的颜色并分析视图层次结构,可能是您以错误的顺序添加了视图。设置视图的背景色,以便您可以看到视图的位置。