UIButton 的基本用法

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(strong,nonatomic) UIButton *btnTest;

@property(strong,nonatomic) UITextField *txtName;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.btnTest=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.btnTest.frame=CGRectMake(100, 100, 150, 44);

    [self.btnTest setTitle:@"按钮" forState:UIControlStateNormal];

    

    [self.btnTest setBackgroundColor:[UIColor redColor]];

    [self.btnTest addTarget:self action:@selector(testBtn) forControlEvents:UIControlEventTouchCancel];

    [self.view addSubview:self.btnTest];

    

}

-(void)testBtn

{

    NSString *name=self.txtName.text;

    NSLog(@"%@",name);

    NSLog(@"test");

    int len=(int)self.txtName.text.length;

    

    if (len==0)

    {

        NSLog(@"用户名不能为空");

    }

    else if (len>0&&len<8)

    {

        NSLog(@"长度不够");

    }

    else

    {

        NSLog(@"继续");

    }

}

原文地址:https://www.cnblogs.com/tmf-4838/p/5256270.html