当前位置:编程学习 > wap >>

IOS开发(16)之UIButton控件

1 前言
UIButton为按钮控件,在IOS开发中十分常见,可以为其设置事件。

2 代码实例
ZYViewController.m:

 

[plain]
@synthesize myButton; 
 
- (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.view.backgroundColor = [UIColor whiteColor]; 
    myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];//设置按钮样式为圆角矩形 
    myButton.frame = CGRectMake(110.0f, 200.0f, 100.0f, 37.0f); 
    [myButton setTitle:@"Press Me" forState:UIControlStateNormal];//按下按钮时候的标题 
    [myButton setTitle:@"I'm Pressed" forState:UIControlStateHighlighted];//按下按钮后抬手时候的标题 
    [myButton addTarget:self action:@selector(buttonIsPressed:) forControlEvents:UIControlEventTouchDown];//按下按钮触发事件 
    [myButton addTarget:self action:@selector(buttonIsTapped:) forControlEvents:UIControlEventTouchUpInside];//按下按钮后抬手时候触发事件 
    [self.view addSubview:myButton]; 

 
-(void)buttonIsPressed:(UIButton *)paramSender{ 
    NSLog(@"Button is pressed"); 

 
-(void)buttonIsTapped:(UIButton *)paramSender{ 
    NSLog(@"Button is tapped"); 

@synthesize myButton;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];//设置按钮样式为圆角矩形
    myButton.frame = CGRectMake(110.0f, 200.0f, 100.0f, 37.0f);
    [myButton setTitle:@"Press Me" forState:UIControlStateNormal];//按下按钮时候的标题
    [myButton setTitle:@"I'm Pressed" forState:UIControlStateHighlighted];//按下按钮后抬手时候的标题
    [myButton addTarget:self action:@selector(buttonIsPressed:) forControlEvents:UIControlEventTouchDown];//按下按钮触发事件
    [myButton addTarget:self action:@selector(buttonIsTapped:) forControlEvents:UIControlEventTouchUpInside];//按下按钮后抬手时候触发事件
    [self.view addSubview:myButton];
}

-(void)buttonIsPressed:(UIButton *)paramSender{
    NSLog(@"Button is pressed");
}

-(void)buttonIsTapped:(UIButton *)paramSender{
    NSLog(@"Button is tapped");
}
运行结果:

 \
 


点击按钮时候:

 


 

\
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,