zoukankan      html  css  js  c++  java
  • ios学习:UIButton,初识委托

     1 - (void)viewDidLoad
     2 {
     3     [super viewDidLoad];
     4     // Do any additional setup after loading the view, typically from a nib.
     5     
     6     //第一种方法创建
     7     UIButton *newBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     8     
     9     newBtn.frame = CGRectMake(50.0f, 50.0f, 100.0f, 50.0f);
    10     [self.view addSubview:newBtn];
    11     
    12     [newBtn setTitle:@"正常状态" forState:UIControlStateNormal];
    13     [newBtn setTitle:@"选中状态" forState:UIControlStateHighlighted];
    14     /*setTitle方法:设置button在某个状态下的的title
    15     第二个参数 forState 在定义button的文字或者图片在何种状态下才会显示,是一个枚举类型
    16     enum {
    17     UIControlStateNormal               = 0,        常规状态显示
    18     UIControlStateHighlighted          = 1 << 0,   高亮状态显示.点击时是高亮状态
    19     UIControlStateDisabled             = 1 << 1,   禁用状态才会显示
    20     UIControlStateSelected             = 1 << 2,   选中状态.什么时候才是选中状态?
    21     UIControlStateApplication          = 0x00FF0000,  当应用程序标志时
    22     UIControlStateReserved             = 0xFF000000 内部预留
    23     };*/
    24 
    25     [newBtn addTarget:self action:@selector(click:event:) forControlEvents:UIControlEventTouchUpInside];
    26     /*给button添加事件,最后一个参数指明了事件类型,第二个参数指明了哪个方法对button的事件进行响应,该方法带两个参数,第一个是事件发生者,第二个是发生的事件,好像最多只能有两个.
    27      第一个参数指明了谁提供响应方法,当第一个参数为nil时,将会从当前对象开始递归询问响应链直至找到方法提供者或者到跟响应链?
    28      */
    29     
    30     
    31     [self.view addSubview:newBtn];
    32     
    33     
    34     //第二种方法创建.这种方法在创建是没有指定button的type(风格),默认是UIButtonTypeCustom,重点在于这个属性只能在初始化时指定,初始化后是没法更改的
    35     //所以一般不用这种创建方法
    36     UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 200.0f, 50.0f)];
    37     
    38     btn.tag = 100;
    39     [btn setTitle:@"默认显示风格" forState:UIControlStateNormal];
    40     
    41     [self.view addSubview:btn];
    42     [btn release];
    43 }
    44 
    45 
    46 /*
    47  提供给UIButton的事件响应方法
    48  */
    49 -(void) click:(id)sender event:(UITouch *) touchEvent
    50 {
    51     /*
    52      指定委托对象为当前类,则表明当前类实现了UIAlertViewDelegate协议时,应该实现了协议里的一些方法,则这些方法可以对alertview的一些事件进行响应
    53      问题在于,当前类也可以不继承UIAlertViewDelegate协议啊,只是不进行对alertview的事件响应而已
    54      */
    55     UIAlertView *alerView = [[UIAlertView alloc]
    56                              initWithTitle:@"Attention"
    57                              message:@"test"
    58                              delegate:self   
    59                              cancelButtonTitle:@"OK"
    60                              otherButtonTitles:@"Cancel",
    61                              nil];
    62     
    63     [alerView show];
    64     [alerView release];
    65 
    66 }
    67 
    68 
    69 /*
    70  当前类继承了UIAlertViewDelegate协议,实现协议里的其中一个方法,可以提供来响应alertview的事件响应
    71  */
    72 -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    73 {
    74     UIAlertView *alerView = [[UIAlertView alloc]
    75                              initWithTitle:@"Attention123"
    76                              message:[NSString stringWithFormat:@"index at %d",buttonIndex]
    77                              delegate:nil
    78                              cancelButtonTitle:@"OK123"
    79                              otherButtonTitles:nil,
    80                              nil];
    81     
    82     [alerView show];
    83     [alerView release];
    84 }
    85 
    86 - (void)didReceiveMemoryWarning
    87 {
    88     [super didReceiveMemoryWarning];
    89     // Dispose of any resources that can be recreated.
    90 }
  • 相关阅读:
    【Phalapi】新加Namespace (模块)
    【PHP】 curl 上传文件 流
    牛掰!我是这么把个人博客粉丝转到公众号的
    Stack Overflow 上 250W 浏览量的一个问题:你对象丢了
    为什么人到中年就危机了呢?
    Java 并发编程(三):如何保证共享变量的可见性?
    如果有人问你 JFinal 如何集成 EhCache,把这篇文章甩给他
    国庆佳节第四天,谈谈我月收入增加 4K 的故事
    为什么要将局部变量的作用域最小化?
    面试官:兄弟,说说基本类型和包装类型的区别吧
  • 原文地址:https://www.cnblogs.com/zouzf/p/2781723.html
Copyright © 2011-2022 走看看