zoukankan      html  css  js  c++  java
  • FirstApp,iphone开发学习总结3,UIButton简单的操作

    相同的,为了Tab init:

    - (id)init {
        self = [super init];
        if (self) {
            [self setTitle:@"按钮展示"];
            
            UIImage *img = [UIImage imageNamed:@""];
            [[self tabBarItem] setImage:img];
        }
        return self;
    }

    在- (void)viewDidLoad中,创建2个按钮(Left and Right),设置Left的Tag为0,Right的Tag为1(区分谁点击),点击执行onClick事件:

    - (void)viewDidLoad
    {
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        leftBtn.frame = CGRectMake(40.0100.0100.030.0);
        [leftBtn setTitle:@"Left" forState:UIControlStateNormal];
        [leftBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [leftBtn setTag:0];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        rightBtn.frame = CGRectMake(180.0100.0100.030.0);
        [rightBtn setTitle:@"Right" forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [rightBtn setTag:1];
       
        [[self view] addSubview:leftBtn];
        [[self view] addSubview:rightBtn];
    }

    添加onClick://@selector(onClick:),添加了:,如果事件为- (void)onClick,则不需要添加

    - (void)onClick:(UIButton *)sender
    {
        switch ([sender tag]) {
            case 0:
                [self alert:@"左键"];
                break;
            case 1:
                [self alert:@"右键"];
                break;
        }
    }

    添加alert方法:

    - (void)alert:(NSString *)str
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您点击了" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
        [alert show];
        [alert release];
    }


    实现的效果就是谁点击,提示谁。

    求指点。

  • 相关阅读:
    网站统计中的数据收集原理及实现
    启动hadoop报ERROR org.apache.hadoop.hdfs.server.namenode.FSImage: Failed to load image from FSImageFile
    淘宝(大数据库应用)--转载
    MapReduce作业的map task和reduce task调度参数
    Spark和Hadoop作业之间的区别
    分析MapReduce执行过程
    MapReduce框架Partitioner分区方法
    LVS+keepalived实现负载均衡
    Tomcat 详解
    linux Tomcat restart脚本简单版
  • 原文地址:https://www.cnblogs.com/maxfong/p/2481951.html
Copyright © 2011-2022 走看看