ios开发日志-button+UIAlertView

去年学习过一段时间的ios开发 后来不知道为什么就没学了 31204556_qFpA.gif   最近准备重新开始学习ios开发
准备每天发一篇日志记录学习过程,也锻炼下自己的语言组织能力,代码写久了话不会说了31204556_H4cG.gif 

    CGRect frame = CGRectMake(10.0f, 10.0f, 120.0f, 50.0f);

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];//初始化一个UIButton

    button.frame = frame;//设置button的大小和位置

    [button setTitle:@"title"forState:UIControlStateNormal];//设置button正常状态下的文字

    button.tag = 2000;  //区分同种控件的标记 这个和android 控件里面的getId一样 在事件里面用来区分是那个控件的事件

    

    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];//对应处理的事件方法 和那个事件

    [self.view addSubview:button];//把button添加到view里面显示
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    

    CGRect frame2 = CGRectMake(100, 100, 100, 60);

    button2.frame=frame2;

    [button2 setTitle:@"test"forState:UIControlStateNormal];

    button2.tag = 2001; 

    [button2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];//selector后面的方法后一定不要忘了要加个冒号 ,因为定义的buttonClicked方式后面是带了类型为id的参数

    [ self . view   addSubview :button2];  


-(IBAction)buttonClicked:(UIButton*)sender//事件处理的方法 sender对应的按钮

{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title"message:@"message"delegate:self cancelButtonTitle:@"确定"otherButtonTitles:@"otherbuttontitles", nil];//提示框  delegate 对应的是事件处理在.h 文件加上UIViewController<UIAlertViewDelegate>

    switch ([sender tag]) {

        case 2000:

            [alert show];

         break;

        case 2001:

            [sender setTitle:@"sender" forState:UIControlStateNormal];

            break;

    };
    NSLog(@"button Clicked!");

 } 

-(void) alertView:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex{//alertView 事件处理的方法  buttonIndex 对应buton的下标

    NSLog(@"%@=====%d",alertview.title,buttonIndex);

}  

转载于:https://my.oschina.net/liyang2l/blog/134908

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值