单选UIbutton的实现

来源:互联网 发布:英文写作软件 编辑:程序博客网 时间:2024/05/22 01:40

.h文件:


#import <UIKit/UIKit.h>


@interface ViewController :UIViewController{

   UIButton *sourceBt;

}

@property(nonatomic,retain)IBOutletUIButton *sourceBt;

-(IBAction)select:(id)sender;


@end



.m文件

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


-(IBAction)select:(id)sender

{

   if (self.sourceBt)

    {

       self.sourceBt.selected =NO;

        self.sourceBt.backgroundColor=[UIColorwhiteColor];

    }

   UIButton * bt = (UIButton *)sender;

    bt.selected =YES;

    bt.backgroundColor=[UIColorredColor];

    

   self.sourceBt = bt;

   NSLog(@"%d",bt.tag);    //获取选择的btn的tag值

}


@end



0 0