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. php获取机器网卡的物理(MAC)地址

    <?php /** 获取网卡的MAC地址原码:目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetMacAddr{ var $return_array = ...

  2. git push 报错

    git push报错误: Git push error: RPC failed; result=56, HTTP code = 200 fatal: The remote end hung up un ...

  3. ASP.NET- Web.Config配置大文件上传

    在web.config中的<system.web></system.web>内加入如下代码: <httpRuntime executionTimeout="60 ...

  4. 解决Qt5使用SSL的“qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method”错误

    在使用Qt的网络组件连接某些服务器时, 会提示"qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method"的错误 ...

  5. VM下的linux系统上不了网?? 使用putty远程登录不上linux的解决方法?

    背景:昨晚想尝试一下用putty远程登录我的linux系统,悲剧的是,我竟然连接不上,显示 connection refused   ,连接被拒绝.于是我就想看看能不能在linux下看看能不能访问百度 ...

  6. jquerymobile知识点:select的动态帮定

    代码: <div data-role="navbar"> <ul> <li> <select name="select-choi ...

  7. C# 动态创建出来的窗体间的通讯 delegate3

    附件1:http://files.cnblogs.com/xe2011/CSharp_WindowsForms_delegate03.rar 一个RTF文件管理器 描述 Form2,Form3,For ...

  8. C++ Virtual介绍 分类: C/C++ 2015-06-16 21:36 26人阅读 评论(0) 收藏

    参考链接:http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html 学过C++的人都知道在类Base中加了Virtual关键字的函 ...

  9. Java大数类介绍

    java能处理大数的类有两个高精度大整数BigInteger 和高精度浮点数BigDecimal,这两个类位于java.math包内,要使用它们必须在类前面引用该包:import java.math. ...

  10. Solr配置与简单Demo[转]

    Solr配置与简单Demo 简介: solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目.它的官方网址在http://lucene.apache.org/sol ...