0

我需要在主窗口中添加一个 UIView,UIView 包含在一个带有一些按钮的视图中。问题是,我放在该视图中的按钮没有响应触摸事件,就像它前面有一些视图一样。该视图需要是一个单例类,因为我需要在任何类中响应触摸事件。

这是 UIView 的代码:

+ (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");
}
4

2 回答 2

1

看起来您的菜单视图的框架大小为零,因此未检测到触摸。这些按钮出现在屏幕上,因为菜单视图未设置为将绘图剪辑到其边界。

于 2013-06-03T17:25:15.963 回答
0

尝试为不同的视图设置不同的颜色并分析视图层次结构,可能您添加的视图顺序错误。

于 2013-06-03T17:54:00.607 回答