1. 新建iOS -> Single View Application.

2. 个性控制器文件YYViewController.m(此处修改为你相应的控制器文件名)

 //
// YYViewController.m
// StudyDynamicButton
//
// Created by yao_yu on 14-5-27.
// Copyright (c) 2014年 yao_yu. All rights reserved.
// #import "YYViewController.h" @interface YYViewController () @end @implementation YYViewController - (void)onAddButtonClicked{
CGRect pframe = self.view.frame;
CGFloat width = ;
CGFloat height = ;
CGRect frame = CGRectMake(pframe.origin.x + (pframe.size.width - width)/, pframe.origin.y + height * , width, height);
UIButton *btnAddedButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnAddedButton.backgroundColor = [UIColor clearColor];
[btnAddedButton setTitle:@"动态添加的按钮" forState:UIControlStateNormal];
btnAddedButton.frame = frame;
[btnAddedButton addTarget:self action:@selector(onDynamicButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnAddedButton];
} -(void) onDynamicButtonClicked{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您点击了动态按钮" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"第二项", nil];
[alert show];
} -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"按钮索引:%ld", buttonIndex);
} - (void)viewDidLoad
{
[super viewDidLoad]; //手动添加按钮
CGRect pframe = self.view.frame;
CGFloat width = ;
CGFloat height = ;
CGRect frame = CGRectMake(pframe.origin.x + (pframe.size.width - width)/, pframe.origin.y, width, height);
UIButton *btnAddDynamicButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnAddDynamicButton.backgroundColor = [UIColor clearColor];
[btnAddDynamicButton setTitle:@"增加动态按钮" forState:UIControlStateNormal];
btnAddDynamicButton.frame = frame;
[btnAddDynamicButton addTarget:self action:@selector(onAddButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnAddDynamicButton];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

3. 运行程序.

iOS: 学习笔记, 动态添加按钮的更多相关文章

  1. IOS学习笔记48--一些常见的IOS知识点+面试题

      IOS学习笔记48--一些常见的IOS知识点+面试题   1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...

  2. IOS学习笔记25—HTTP操作之ASIHTTPRequest

    IOS学习笔记25—HTTP操作之ASIHTTPRequest 分类: iOS2012-08-12 10:04 7734人阅读 评论(3) 收藏 举报 iosios5网络wrapper框架新浪微博 A ...

  3. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  4. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  5. iOS学习笔记06—Category和Extension

    iOS学习笔记06—Category和Extension 一.概述 类别是一种为现有的类添加新方法的方式. 利用Objective-C的动态运行时分配机制,Category提供了一种比继承(inher ...

  6. iOS学习笔记-自己动手写RESideMenu

    代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...

  7. iOS学习笔记-自定义过渡动画

    代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...

  8. iOS学习笔记-地图MapKit入门

    代码地址如下:http://www.demodashi.com/demo/11682.html 这篇文章还是翻译自raywenderlich,用Objective-C改写了代码.没有逐字翻译,如有错漏 ...

  9. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

随机推荐

  1. zookeeper与activemq最新存储replicatedLevelDB整合

    测试环境:三台VM虚拟机centos6.4 64位 mini版,IP分别为192.168.1.191/192/193. 测试目的:测试activemq的failover以及与zookeeper的整合 ...

  2. [C++基础]关键词volatile

    volatile概念 volatile 是易变的.不稳定的意思. volatile关键字和const 一样是一种类型修饰符,用它修饰的变量表示可以被某些编译器未知的因素更改,比如操作系统.硬件或者其它 ...

  3. 编译C++,找不到头文件(fatal error: string: No such file or directory)

    在androidproject中编译C++时,找不到头文件,报错例如以下: fatal error: string: No such file or directory 解决该问题须要在Android ...

  4. [TypeScript] Configuring TypeScript Which Files to Compile with "Files" and "OutDir"

    This lesson shows how to configure the .tsconfig so you only compile the .ts files you want. It then ...

  5. xcode6下使用autolayout+sizeclass实践

    历史车轮滚滚向前,将autolayout配合sizeclass做布局的方式推上了主流,虽然有点晚,但最终还是进行了一次完整的实践,特此记录一下: 因为网上已经有很多博客介绍了autolayout配合s ...

  6. mybatis11 sqlMapConfig.xml文件说明

    1sqlMapConfig.xml SqlMapConfig.xml中配置的内容和顺序如下: properties(属性) settings(全局配置参数) typeAliases(类型别名) typ ...

  7. MVC缓存,使用数据层缓存,添加或修改时让缓存失效

    在"MVC缓存01,运用控制器缓存或数据层缓存"中,在数据层中可以设置缓存的有用时刻.但这个还不够"智能",常常期望在修改或创立的时分使缓存失效,加载新的数据. ...

  8. ubuntu中使用nginx把本地80端口转到其他端口

    ubuntu中使用nginx把本地80端口转到其他端口 因为只是在开发的过程中遇到要使用域名的方式访问, 而linux默认把1024以下的端口全部禁用. 在网上找了N多方式开启80端口无果后, 方才想 ...

  9. 基础之 window-self-top-opener

    今天我都在怀疑,很多项目还用不用iframe这个框架做页面布局. 如果你有兴趣想告诉我,请给我留言. 一. 说明 注:这里top和window.top等价,window是可以省略的,有得情况下不允许省 ...

  10. ListView优化分页优化

    缘由 我们在用ListView展现数据的时候.比如展现联系人,如果联系人太多就会出现卡的现象,比如如果有1000多条数据,从数据库里查询,然后装载到List容器这段时间是比较耗时的.虽然我们可以用as ...