插座变量: 表示这是和xib可以链接的
可让从源代码文件引用界面中的对象
IBOutlet UILabel *labelOne;
IBOutlet UIButton *buttonOne;
插座方法:表示这是和xib可以连接的
-(IBAction)FuctionButton:(id)sender;
UIButton
实例化Button:
方法一:手动创建一个Button
#import "ViewController.h"
@interface ViewController (){
//新建一个UIButton
UIButton *myButton;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//手动创建一个Button,四个数值分别对应,以手机界面左上角为0,0坐标,X轴的距离为100,Y轴的距离为100(向下),Button的宽度为50,高度(向下)为50
myButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
//给Button赋上颜色(红色)
myButton.backgroundColor=[UIColor redColor];
//给Button上添加文字,按钮
[myButton setTitle:@"按钮" forState:UIControlStateNormal];
//将上面的设置添加到myButton上
[self.view addSubview:myButton];
}
方法二:直接拖动创建一个Button
先在Main.storyboard(手机界面)拖动一个Button,
然后在.h文件里面创建一个IBOutlet UIButton *myButton;
再返回手机界面,右键点击View Controller,选择myButton后面的小圆圈,使用左键链接手机界面上拖出来的的Button,他们就关联住了。
*m
UIButton设置背景图
#import "ViewController.h"
@interface ViewController (){
//新建一个UIButton
UIButton *myButton;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//给myButton设置位置和大小
myButton =[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
//给myButton设置一张图片,如果图片是.png格式的,可以不用写格式,别的都需要加.格式,如(touxiang.jpg)
[myButton setBackgroundImage:[UIImage imageNamed:@"touxiang"] forState:UIControlStateNormal];
//将上面的设置赋给myButton
[self.view addSubview:myButton];
}
yButton;
UIButton添加触发事件
#import "ViewController.h"
@interface ViewController (){
//新建一个UIButton
UIButton *myButton;
//新建一个UILabel
UILabel *myLabel;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//给myButton设置位置和大小
myButton =[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
//给myButton设置一张图片,如果图片是.png格式的,可以不用写格式,别的都需要加.格式,如(touxiang.jpg)
[myButton setBackgroundImage:[UIImage imageNamed:@"touxiang"] forState:UIControlStateNormal];
//给myButton设置触发事件,和触发方法(haha:)
[myButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside];
//将上面的设置赋给myButton
[self.view addSubview:myButton];
//给myLabel设置位置和大小
myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 500, 300, 50)];
//给myLabel设置内容
myLabel.text=@"快来点我啊";
//将上面的设置赋给myLabel
[self.view addSubview:myLabel];
}
//myButton的触发方法,如果点击了myButton,则myLabel的值变成了“我被点击了”
-(void)haha:(id)a{
myLabel.text=@"我被点击了";
}
UILabel
实例化Label:
方法一:手动创建一个Label
#import "ViewController.h"
@interface ViewController (){
//新定义一个Label
UILabel *myLabel;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置Label的位置和大小
myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
//设置Label的文字内容
[myLabel setText:@"小明"];
//设置Label的颜色
myLabel.backgroundColor = [UIColor blueColor];
//将上面的设置添加到myLabel上
[self.view addSubview:myLabel];
}
方法二:直接拖动创建一个Label
先在Main.storyboard(手机界面)拖动一个Label,
然后在.h文件里面创建一个IBOutlet UIButton *myLabel;
再返回手机界面,右键点击View Controller,选择myLabel后面的小圆圈,使用左键链接手机界面上拖出来的的Label,他们就关联住了。