添加动态按钮(UIAlertButton)

本文介绍如何在iOS应用中实现按钮的添加及其点击事件响应,并通过按钮触发一个简单的警告框来展示交互效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在根视图控制器类的.h文件中

#import <UIKit/UIKit.h>


@interface liViewController : UIViewController

//添加按钮

- (void) addButton;


@end

在.m文件中

@implementation liViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

//调用自己的方法

    [self addButton];

}


- (void) addButton

{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    

    button.frame = CGRectMake(100, 200, 120, 40);

    

    [button setTitle:@"点击按钮" forState:UIControlStateNormal];

    //给按钮添加关联动作

    [button addTarget:self action:@selector(addAlertButton:) forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:button];

    

   // [button release];

}


- (void)addAlertButton:(id)sender

{   //添加动态按钮

    UIAlertView  *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"点击了动态按钮" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    //动态按钮的显示

    [alertView show];

    

    [alertView release];

    

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值