2012-03-17 67 views
0

我已经添加了一个UIView(其中包含UIImageView的背景,三个UIButtons说“测试”和最后一个UIButton关闭名为“Finished”的视图)作为我的UIActionSheet的子视图。为什么UIButtons在UIActionSheet的UIView中检测不到触摸?

这些按钮为什么不会任何检测到触摸?我的UIView已启用用户交互启用。

我非常感谢这方面的帮助,因为我一直在拉我的头发(当然不是真的)!

这里是我的设立:

enter image description here

+0

为按钮设置操作,那么你可以做你的行动方法 – Narayana 2012-03-17 12:00:36

+0

我认为这是不可能的,因为我已经尝试过什么都想要,同样的问题然后我在导航栏中放置了两个按钮和一个PickerView。如果你想呈现完整的视图,那么我的建议是使用presentModelViewController。 – 2012-03-17 12:23:27

+0

我只是想模仿UIActionSheet的动画,在底部打开一个小对话框。我真的不想要一个全尺寸的视图来显示。有什么办法仍然可以做到这一点? – 2012-03-17 12:31:29

回答

2

如果你只是希望模仿UIActionSheet,我刚刚创建UIVew子类,称为像“ CoolActionSheet“并以编程方式将按钮放在那里。然后,当你按下按钮时,它会触发一个协议中的委托方法,这将在你的主视图控制器中实现,所以你可以做一些事情。

显示和隐藏操作选择器中使用的UIView动画在CoolActionSheet类,像这样:

-(void)showSheet { 
    NSLog(@"Showing sheet..."); 
    //Set the x/y position of the action sheet to JUST off-screen 
    CGFloat xPos = parentView.frame.origin.x; 
    CGFloat yPos = parentView.frame.size.height+kActionSheetHeight; 

    [self setFrame:CGRectMake(xPos, yPos, kActionSheetWidth, kActionSheetHeight)]; 

    /*Here is where you would add your other UI objects such as buttons 
    and set their @selector to a method in your CoolActionSheet protocol. You could then implement this delegate method in 
    your main view controller to carry out a custom action. You might also want to add a background image to the view or something else. 
    For example: */ 
    UIButton *coolButton = [[UIButton alloc] initWithFrame:buttonDimensions]; 
    [coolButton addTarget:self action:@selector(didDismissActionSheet) forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:coolButton]; 

    [self.parentView addSubview:self.view]; 

    //Slide the sheet up from the bottom of the screen 
    [UIView animateWithDuration:0.2 animations:^(void) { 
     //Slide banner in from left to right 
     [self setFrame:CGRectMake(0, yPos-kActionSheetHeight, kActionSheetWidth, kActionSheetHeight)]; 
    }]; 
} 

,并隐藏:

-(void)hideSheet { 
    NSLog(@"Hiding"); 
    CGFloat xPos = parentView.frame.origin.x; 
    CGFloat yPos = parentView.frame.size.height+kActionSheetHeight; 

    [UIView animateWithDuration:0.2 animations:^(void) { 
     [self setFrame:CGRectMake(xPos, yPos, 320, 65)]; 
    }completion:^(BOOL finished) { 
     [self removeFromSuperview]; //Clean up 
    }]; 
} 

您可能还需要为灰色时的父视图。再次,在CoolActionSheet.m:

-(void)shadeParentView { 
    UIView *shadedView = [[UIView alloc] initWithFrame:CGRectMake(, 0, 320, 480)]; 

    [shadedView addGestureRecognizer:gestureRecognizer]; 
    [shadedView setBackgroundColor:[UIColor blackColor]]; 
    [shadedView setAlpha:0.0]; 

    [self addSubview:shadedView]; 

    [UIView animateWithDuration:0.5 animations:^{ 
     [shadedView setAlpha:0.5]; 
    }]; 

}

您将需要您的parentView设置视图控制器的看法。于是,打电话的动作片在你的主视图控制器你会:

CoolActionSheet *coolSheet = [[CoolActionSheet alloc] init]; 
[coolSheet setParentView:self.view]; 
[coolSheet setSheetDelegate:self]; //set the delegate to implement button press methods in this view controller 

这似乎有点啰嗦,但它是一个良好的MVC模式,将其分离出来到另一个这样的视图类。你现在有一个自定义类,你可以导入任何其他项目,它会工作!

我还没有机会测试这个特定的代码,但整个方法是好的。点从此带走:

  • 使用自定义UIView类
  • 实现委托方法时,你的子视图按下一个按钮在主视图控制器来执行任务。
  • 实现一个好的MVC结构来避免意大利面代码。

让我知道如果这能帮助:)

+1

这是一个非常基本的示例项目: http://cl.ly/0Z2I3w1g461h16302C0c 希望这提供了一些清晰。 – 2012-03-18 12:06:04

0

试试这个:

[actionSheet showInView:self.view.window]; 
+0

不幸的是... – 2012-03-17 12:46:30