(iOS软件开发UI部分)UIButton基本使用

来源:互联网 发布:四大台柱 知乎 编辑:程序博客网 时间:2024/04/27 21:20

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];


/******************** UIButton *********************/

// 创建一个button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// 为button设置大小
button.frame =CGRectMake(100, 100, 50, 40);
// button背景颜色
button.backgroundColor = [UIColor grayColor];
// 设置button字体
[button setTitle:@"点击" forState:UIControlStateNormal];
// 设置颜色
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// 设置图片(前景)
[button setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
// 获取指定前景照片
UIImage *image = [button imageForState:UIControlStateNormal];
// 设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
// 获取指定背景图片
UIImage *image1 = [button backgroundImageForState:UIControlStateNormal];
// 判断是否被点击
button.selected = YES;
// 按钮是否可用
button.enabled = YES;

/**
* 为button添加点击事件

*/
// 1.添加
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
/**
为button移除事件

*/
[button removeTarget:self action:@selector(removeButtonAction:) forControlEvents:UIControlEventTouchUpInside];


/******************** UIAlertView *********************/
// 警告框
//(1)创建
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您有一百万元到账!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//(2)显示
[alertView show];
//(3)释放
[alertView release];
[self.window makeKeyAndVisible];
return YES;
}
// 2.实现事件
- (void)buttonAction:(UIButton *)button{

self.window.backgroundColor = [UIColor yellowColor];

}

0 0
原创粉丝点击