iOS开发_Button的外部类方法

2016-08-29  本文已影响19人  格蓝_

Button的外部类方法使用的运用情景:需要在A类里面的Button点击之后,需要在B类里面监听或执行某些操作的时候。具体代码如下:

首先创建一个BassViewController;
#import 〈UIKit/UIKit.h〉

@interface BassViewController : UIViewController

+ (void)buttonAction;

@end

@implementation BassViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

// Do any additional setup after loading the view.

}

+ (void)buttonAction {

NSLog(@"Are you OK?");

}

然后再建一个FirstViewController 去创建Button

#import "FirstViewController.h"

@interface FirstViewController ()

@property (nonatomic, retain)UIButton *button;

@end

@implementation FirstViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

[self buttonAction:@selector(testAction)];

}

- (void)buttonAction:(SEL)button{

self.button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

self.button.backgroundColor = [UIColor redColor];

self.button.showsTouchWhenHighlighted = YES;

[self.button addTarget:self action:button forControlEvents:6];

[self.view addSubview:self.button];

}

- (void)testAction{

[BassViewController buttonAction];

}

ps:这样就可以在BassViewController 中写点击Button 之后 BassViewController 的一些操作事宜,不需要监听之类的操作;

上一篇 下一篇

猜你喜欢

热点阅读