FirstApp,iphone开发学习总结3,UIButton简单的操作
2012-05-03 23:10
447 查看
相同的,为了Tab init:
- (id)init {
self = [super init];
if (self) {
[self setTitle:@"按钮展示"];
UIImage *img = [UIImage imageNamed:@""];
[[self tabBarItem] setImage:img];
}
return self;
}在- (void)viewDidLoad中,创建2个按钮(Left and Right),设置Left的Tag为0,Right的Tag为1(区分谁点击),点击执行onClick事件:
- (void)viewDidLoad
{
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
leftBtn.frame = CGRectMake(40.0, 100.0, 100.0, 30.0);
[leftBtn setTitle:@"Left" forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[leftBtn setTag:0];
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
rightBtn.frame = CGRectMake(180.0, 100.0, 100.0, 30.0);
[rightBtn setTitle:@"Right" forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[rightBtn setTag:1];
[[self view] addSubview:leftBtn];
[[self view] addSubview:rightBtn];
}添加onClick://@selector(onClick:),添加了:,如果事件为- (void)onClick,则不需要添加
- (void)onClick:(UIButton *)sender
{
switch ([sender tag]) {
case 0:
[self alert:@"左键"];
break;
case 1:
[self alert:@"右键"];
break;
}
}添加alert方法:
- (void)alert:(NSString *)str
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您点击了" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
}
实现的效果就是谁点击,提示谁。
求指点。
- (id)init {
self = [super init];
if (self) {
[self setTitle:@"按钮展示"];
UIImage *img = [UIImage imageNamed:@""];
[[self tabBarItem] setImage:img];
}
return self;
}在- (void)viewDidLoad中,创建2个按钮(Left and Right),设置Left的Tag为0,Right的Tag为1(区分谁点击),点击执行onClick事件:
- (void)viewDidLoad
{
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
leftBtn.frame = CGRectMake(40.0, 100.0, 100.0, 30.0);
[leftBtn setTitle:@"Left" forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[leftBtn setTag:0];
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
rightBtn.frame = CGRectMake(180.0, 100.0, 100.0, 30.0);
[rightBtn setTitle:@"Right" forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
[rightBtn setTag:1];
[[self view] addSubview:leftBtn];
[[self view] addSubview:rightBtn];
}添加onClick://@selector(onClick:),添加了:,如果事件为- (void)onClick,则不需要添加
- (void)onClick:(UIButton *)sender
{
switch ([sender tag]) {
case 0:
[self alert:@"左键"];
break;
case 1:
[self alert:@"右键"];
break;
}
}添加alert方法:
- (void)alert:(NSString *)str
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您点击了" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
}
实现的效果就是谁点击,提示谁。
求指点。
相关文章推荐
- FirstApp,iphone开发学习总结2,简单的表
- FirstApp,iphone开发学习总结11,表操作(移动、删除)
- FirstApp,iphone开发学习总结13,方向感应和通告
- FirstApp,iphone开发学习总结1,UIview添加UIimage
- FirstApp,iphone开发学习总结5,Slider改变Label的值
- FirstApp,iphone开发学习总结0,创建TabBar
- FirstApp,iphone开发学习总结6,Navigation的使用
- FirstApp,iphone开发学习总结9,旋转
- FirstApp,iphone开发学习总结8,自定义TablevViewCell
- FirstApp,iphone开发学习总结12,播放音乐、视频
- FirstApp,iphone开发学习总结
- FirstApp,iphone开发学习总结4,UITextField的值To UILabel
- FirstApp,iphone开发学习总结7,相机
- FirstApp,iphone开发学习总结10,归档(序列化、固化)
- Linux内核与驱动开发学习总结:原子操作实现(六)
- App后台开发运维和架构实践学习总结(8)——后台产品设计的4个原则
- App后台开发运维和架构实践学习总结(3)——RestFul架构下API接口设计注意点
- 近期的安卓学习开发SOS APP的一些总结文档 (拨打电话 加载首页图片的异步代码 判断是否有网络代码)
- 逐步学习 iPhone App 开发(1)
- PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [2] 首页 APP 接口开发方案 ① 读取数据库方式