#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. iOS 抖动动画

    -(void)animationWithCell:(WaterLevelCollectionCell *)cell{ // 添加摇晃动画 { CAKeyframeAnimation *frame=[C ...

  2. Android Studio安装及主题字体配置

    在2013 Google I/O 大会上,谷歌推出了自家全新的安卓软件集成开发工具 Android Studio,这是 Google 基于 IntelliJ IDEA 改动而来. 谷歌称 Androi ...

  3. Hanoi Tower问题分析

    前言 回家休息第3天了,状态一直不是太好,主要是要补牙,检查身体,见同学见亲戚,心里又着急校招,难得能腾出时间来好好思考,这里也是看<cracking the coding interview& ...

  4. 使用QQ账号的来发送邮件

    第一步,看图操作如下: 打开QQ邮箱,在设置-帐户,然后再看下图示: 把:POP3/SMTP服务,IMAP/SMTP服务,Exchange服务,CardDAV/CalDAV服务都选择上,保存这样,便可 ...

  5. SuperToolTips

    https://github.com/nhaarman/supertooltips supertooltips-master.zip

  6. [Redux] Accessing Dispatch and State with Redux -- connect

    If you have props and actions, you want one component to access those props and actions, one solutio ...

  7. NSRange类详解

    NSRange的定义 { NSUInteger location; NSUInteger length; } NSRange; NSRange是一个结构体,其中location是一个以0为开始的ind ...

  8. MHA手动切换 原创2 (主参与复制)

    monitor 执行下面命令后, --orig_master_is_new_slave :原主变为新从,即server1变成了slave1的从.slave2跟据app1.conf中配制也变成了slav ...

  9. 网络IPC:套接字之套接字选项

    套接字机制提供两个套接字选项接口来控制套接字的行为.一个接口用来设置选项,另一个接口允许查询一个选项的状态.可以获取或设置的三种选项: (1)通用选项,工作在所有套接字类型上. (2)在套接字层次管理 ...

  10. Debian5.04安装oracle11g 笔记

    新安装了Debian5,成功安装了oracle11g.记录过程如下. 1.升级一下系统    #apt-get update    #apt-get upgrade 2.安装需要的一些组件:    # ...