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. DevExpress 用户控件 分页(中)

    说明: 1)具体调用请关注 看DevExpress 用户控件 分页(下) datanavi_ButtonClick 是DataNavigator的ButtonClikc事件 视图设计器: 分页用户控件 ...

  2. PostgreSQL相关的软件,库,工具和资源集合

    PostgreSQL相关的软件,库,工具和资源集合. 备份 wal-e - Simple Continuous Archiving for Postgres to S3, Azure, or Swif ...

  3. HTML5到底能给企业带来些什么?

    一.改变企业网络广告的模式与分布 广告是企业网络营销的主要方式之一.十几年来,无论是展示还是互动,基本被Adobe Flash所主宰.然而,HTML5网页的多媒体特性.三维.图形及特效,超炫的浏览体验 ...

  4. 【iOS-Android开发对照】 之 APP入口

    [iOS-Android开发对照]之 APP入口 [图片 Android vs iOS] 提纲 对照分析iOS,Android的入口, iOS,Android的界面单元 为什么要有那样的生命周期 继承 ...

  5. Linux下jvm、tomcat、mysql、log4j优化配置笔记[转]

    小菜一直对操作系统心存畏惧,以前也很少接触,这次创业购买了Linux云主机,由于木有人帮忙,只能自己动手优化服务器了.... 小菜的云主机配置大致为:centeos6(32位),4核心cpu,4G内存 ...

  6. Linux Apache SVN

    yum install mod_dav_svn subversion  httpd mkdir /var/www/svnsvnadmin create /var/www/svn/puppetcd /v ...

  7. winform鼠标滚轮事件

    #region 进入窗体时加载 public FormAlarmInfoQuery() { InitializeComponent(); pictureBox1.ImageLocation = &qu ...

  8. 如何用js检测判断时间日期的间距

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. C#语法糖之第四篇: 扩展方法

    今天继续分享C#4.0语法糖的扩展方法,这个方法也是我本人比较喜欢的方法.大家先想想比如我们以前写的原始类型不能满足现在的需求,而需要在该类型中添加新的方法来实现时大家会怎么做.我先说一下我没有学习到 ...

  10. 在picture library中取某一图片的大图、小图

    public static string GetPicThumbnail(SPFile file, string type) {     string thumbnail = "" ...