ios Button
2015-06-11 11:16
489 查看
展现效果如下:
功能说明:1添加来图片背景,
2动态展现百度网页,
3动态添加按钮,
4展现提示框,展现你点击提示框得index
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
我成功来你也快来试试!
1 具体得项目创建与拖动按钮到storyboard 就不在详述
storyboard 上添加来按钮,同时也增加来背景!
2 按住ctrl键拖拽到ViewController.m文件空白处,生成someButtonClicked,填充代码后如下
-(void)someButtonClicked{ // NSLog(@"点击成功!"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您点击了动态按钮!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",@"点击index",nil]; [alert show]; }
3 动态生成button 点击事件(提示框)
- (IBAction)btnTouch:(id)sender { CGRect frame = CGRectMake(90, 100, 200, 60); UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; someAddButton.backgroundColor = [UIColor clearColor]; [someAddButton setTitle:@"点击试试看!" forState:UIControlStateNormal]; someAddButton.frame = frame; [someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:someAddButton]; }
4 整体代码预览
//
// ViewController.m
// btn_move
//
// Created by selfimprovement on 15-6-10.
// Copyright (c) 2015年 sywaries@sina.cn. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
//监听方法
-(void)someButtonClicked{ // NSLog(@"点击成功!"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您点击了动态按钮!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消",@"点击index",nil]; [alert show]; }
//委托 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"buttonIndex:%d", buttonIndex); }
//按钮action
- (IBAction)btnTouch:(id)sender { CGRect frame = CGRectMake(90, 100, 200, 60); UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; someAddButton.backgroundColor = [UIColor clearColor]; [someAddButton setTitle:@"点击试试看!" forState:UIControlStateNormal]; someAddButton.frame = frame; [someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:someAddButton]; }
//立马加载百度,可是被背景覆盖
- (void)viewDidLoad {
[super viewDidLoad];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 240, 360)];
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
[self.view addSubview: webView];
[webView loadRequest:request];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)loadWebPageWithString:(NSString*)urlString{
}
@end
5 为了知道你点击提示框得那一个index ,增加委托,和实现委托
UIAlertViewDelegate 就是添加委托
6 最后就是实现委托方法,目的就是知道您点击那一个index
//委托 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"buttonIndex:%d", buttonIndex); }
相关文章推荐
- iOS-设置navigation导航栏标题的颜色
- iOS-自定义Navigation左侧的leftBarButtonItem
- iOS中对时间的处理(一)——获取当前时间戳
- 那些在学习iOS开发前就应该知道的事(part 2)
- iOS各机型参数对比
- ios-html-get/post的区别,一言以蔽之(MS)CheckST
- iOS开发debug跟release版本屏蔽NSLog方法
- iOS-自定义button
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- 创意搞笑大集合 iOS趣味应用精选
- iOS开发-检测用户截屏, 并获取所截图片
- IOS-蓝牙后台连接
- NO 1 ios开发前准备
- xcode6+ios8最新真机调试教程
- iOS游戏截图或广告图尺寸要求
- 适配iOS 8
- iOS开发之Pch预编译文件的创建
- ios 概况了解
- 关于nios下载出现(Connected system ID hash not found..)问题的解决
- NSSet全部API学习。