#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//创建view对象
    UIView *myview = [[UIView alloc]init];
    myview.frame = CGRectMake(50, 100, 150, 100);
    [self.view addSubview:myview];
   
    //设置背景颜色
    myview.backgroundColor = [UIColor greenColor];
    
    //设置是否能接受事件,默认是YES
    myview.userInteractionEnabled = NO;
    
    //创建button
    UIButton * btn = [[UIButton alloc]init];
    btn.frame = CGRectMake(50, 100, 250, 40);
    [myview addSubview:btn];
    btn.backgroundColor = [UIColor redColor];
    
    //注册事件
    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];
    
    //1.如果父视图不能接受事件,则子视图也不能接受事件
    //2.子视图超出父视图的部分不能接受事件
    
    [btn removeFromSuperview];
    [self.view addSubview:btn];
    
    [self.view insertSubview:btn belowSubview:myview];
    //3.如果上面视图覆盖下面视图而且能接受事件,则下面视图不会收到事件了
    
    //4.UILabel和UIImageView 默认不能接受事件
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - button处理方法
- (void)onClick:(UIButton *)btn
{
    NSLog(@"button press!");
}

@end

UIView-4-EventForViews(在view上加入button时候的事件处理)的更多相关文章

  1. view上添加点手势 button无法响应点击事件

    在view 上添加手势 有的时候  会把Button的 点击事件盖掉,这个 时候 我们用UITapGestureRecognizer的代理方法 //手势的代理方法 - (BOOL)gestureRec ...

  2. iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang

    转载请注明出处. 今天在调试代码的时候,在tableviewcell上添加button,发现button快速点击的话,是看不出点击效果的,查找资料发现, ios7上UITableViewCell子层容 ...

  3. 关于iOS自定义控件:在view上实现事件和代理

    自定义控件.h #import <UIKit/UIKit.h> #import "PPViewtouchesBeginDelegate.h" @interface PP ...

  4. 一个可以将 json 字符串 直接绑定到 view 上的Android库

    android-data-binding 这是一个可以将 json 字符串 直接绑定到 view 上的库, 不用先将 json 转换为 model 类. 传送门(https://github.com/ ...

  5. 给View添加手势,防止点击View上其他视图触发点击效果

    在开发过程中,我们可能会遇到这个问题. 当我们给一个view添加了手势,但是我们又不想点击view上面的视图也触发手势.如下图: 我们在红色view上添加了手势,但是又不想点击黄色view也触发.其实 ...

  6. ios上的 button和input-button为什么不水平居中的

    在iphone6plus上的button中文本上不居中,如下图: 造成的原因,是button的padding不为零,造成的,因而设置padding: 0:就可以解决

  7. iOS constraint被应用于view上的时间

    在viewdidload时,constraint是没有被应用的,之后在layoutSubviews时,系统应用了constraint.但是我感觉在viewWillLayoutSubviews函数时就已 ...

  8. IOS开发中--点击imageView上的Button没有任何反应

    点击imageView上的Button没有任何反应:    解决方法:设置图片的userInteractionEnabled为YES,使该imageView可以与用户进行交互

  9. 遍历父视图上的button

    for (UIView * thebtn in [self.view subviews]) { if ([thebtn isKindOfClass:[UIButton class]]) { //*** ...

随机推荐

  1. usr/bin/ld: cannot find 错误解决方法

    参考:http://blog.siyebocai.cn/20100324_5p424qs7.html 通常在软件编译时出现的usr/bin/ld: cannot find -lxxx的错误,主要的原因 ...

  2. ADO.NET 快速入门(八):处理 Errors

    除了 Try/Catch 和 Exceptions 以外,新的 ADO.NET 数据框架也允许在 DataSet 的每行数据添加错误信息.如果 Updates 或者其他操作失败,SqlDataAdap ...

  3. J2EE程序员应该要掌握的linux知识

    J2EE程序员应该要掌握的linux知识 大型J2EE应用都在建构在linux环境下的.开发环境下我们可以通过samba映射成本地的网络驱动器,直接在windows环境下进行编程调试.但是最后的发布还 ...

  4. spring中的ResourceBundleMessageSource

    1 首先创建两个资源文件    messages_en_US.properties customer.name=Yong Mook Kim, age : {0}, URL : {1} messages ...

  5. 文献阅读笔记——group sparsity and geometry constrained dictionary

    周五实验室有同学报告了ICCV2013的一篇论文group sparsity and geometry constrained dictionary learning for action recog ...

  6. StarlingMVC简介,原理解说及示例源码

    StarlingMVC简介 StarlingMVC是一个为使用Starling来开发游戏的MVC框架.这个框架的特性方面,很像Swiz和RobotLegs,原理亦像Mate.其特性列表如下: 依赖注入 ...

  7. java_Oralce

    简单范例 create or replace procedure delete_table is i number(10); begin for x in (select * from emp whe ...

  8. Cent OS安装TL-WN725N 2.0 USB驱动

    TP Link官方没有提供TL-WN725N 2.0的Linux驱动下载,折腾了我半天,试了各种方法.也有一部分原因是因为这机器还不能联网,导致有一些驱动因为缺少依赖并不成功安装. 后来终于在gith ...

  9. 关于通过adb启动Activity、activity、service以及发送broadcast的命令

    一.启动activity: $ adb shell$ am start -n {包名}/{包名}.{活动名称} 如:启动一个名叫MainActivity的活动 # am start -n com.ex ...

  10. 小白日记29:kali渗透测试之Web渗透-扫描工具-Vega

    WEB扫描工具-Vega 纯图形化界面,Java编写的开源web扫描器.两种工作模式:扫描模式和代理模式[主流扫描功能].用于爬站.处理表单,注入测试等.支持SSL:http://vega/ca.cr ...