新建两个类MainViewController/ButtonView

ButtonView.h

#import <UIKit/UIKit.h>
@interface ButtonView : UIView
//实现target-action设计模式
//点击的时候让谁去执行方法
@property (nonatomic , assign) id target;
//要执行的方法
@property (nonatomic , assign) SEL action;
//模拟一个UIButton的一个方法
- (void)addTarget:(id)target action:(SEL)action;
@end

ButtonView.m

#import "ButtonView.h"
@implementation ButtonView
- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {// Initialization code}return self;
}
//建立一个view,让这个view的作用和UIButton类似 作用;点击能够响应事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{//点击时候就有反应}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{NSLog(@"摸完了,可口可乐,冒泡");//每当view被点击的时候,就让target执行action方法//target\action设计模式核心[_target performSelectorInBackground:self.action withObject:self];
}
//利用方法给view设置对象和对象要执行的方法
- (void)addTarget:(id)target action:(SEL)action
{self.target = target;self.action = action;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{// Drawing code
}
*/
@end

MainViewController.h

#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@end

MainViewController.m

#import "MainViewController.h"
#import "ButtonView.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {// Custom initialization}return self;
}
- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view.ButtonView *button = [[ButtonView alloc] initWithFrame:CGRectMake(120, 50, 80, 30)];button.backgroundColor = [UIColor purpleColor];button.alpha = 0.3;button.layer.cornerRadius = 10;//给view添加一个触发方法[button addTarget:self action:@selector(buttonClicked:)];[self.view addSubview:button];[button release];//UIImageView  //是一个显示图片的viewUIImageView *p_w_picpathView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];[self.view addSubview:p_w_picpathView];[p_w_picpathView release];//给p_w_picpathView设置一个显示的图片UIImage *p_w_picpath = [UIImage p_w_picpathNamed:@"1.png"];p_w_picpathView.p_w_picpath = p_w_picpath;//如果在p_w_picpathView 上添加按钮等视图,需要打开p_w_picpathView的用户交互属性[p_w_picpathView setUserInteractionEnabled:YES];UIImageView *p_w_picpathView1 = [[UIImageView alloc] initWithFrame:CGRectMake(160, 100, 50, 50)];[self.view addSubview:p_w_picpathView1];[p_w_picpathView1 release];UIImage *p_w_picpath1 = [UIImage p_w_picpathNamed:@"2.png"];p_w_picpathView1.p_w_picpath = p_w_picpath1;}
- (void)buttonClicked:(ButtonView *)button
{NSLog(@"button触发后的方法");
}
- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/
@end

AppDelegate.h

#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

AppDelegate.m

#import "AppDelegate.h"
#import "MainViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];// Override point for customization after application launch.self.window.backgroundColor = [UIColor whiteColor];//初始化视图控制器MainViewController *mainVC = [[MainViewController alloc] init];self.window.rootViewController = mainVC;[mainVC release];[self.window makeKeyAndVisible];[_window release];return YES;
}
- (void)dealloc
{[_window release];[super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end

转载于:https://blog.51cto.com/liuyafang/1548166

target-action设计模式--主要为Button的方法重写相关推荐

  1. UI一揽子计划 5 (Target: Action:  、Protocol   Delegate、 UIImageView 、手势识别器)

    一.target/ action 设计模式      耦合是衡量⼀一个程序写的好坏的标准之一,      耦合是衡量模块与模块之间关联程度的指标      "高内聚,低耦合"是⾯面 ...

  2. Struts2中action接收参数的三种方法及ModelDriven跟Preparable接口结合JAVA反射机制的灵活用法...

    Struts2中action接收参数的三种方法及ModelDriven跟Preparable接口结合JAVA反射机制的灵活用法 www.MyException.Cn   发布于:2012-09-15 ...

  3. Struts2中There is no Action mapped for namespace错误解决方法

    Struts2中There is no Action mapped for namespace错误解决方法 参考文章: (1)Struts2中There is no Action mapped for ...

  4. 策略模式和工厂模式的区别_设计模式之工厂模式-工厂方法模式

    设计模式之工厂模式-工厂方法模式 大家好,欢迎来到污污弹公司,今天司小司又接到了一个新活-披萨项目. 来源:凯哥Java(kaigejava) 需求: 披萨项目: 要方便披萨品种的扩展.要便于维护.要 ...

  5. 北风设计模式课程---2、工厂方法模式

    北风设计模式课程---2.工厂方法模式 一.总结 一句话总结: 工厂方法模式相对于简单工厂模式只是修改了 [工厂核心类-将它变成接口],具体的创建产品的工作交给[工厂核心类的子类] 满足了开闭原则:不 ...

  6. java setlayout_Java Button.setLayoutX方法代码示例

    import javafx.scene.control.Button; //导入方法依赖的package包/类 private void buildMainMenu(Group root, Scene ...

  7. 一个action类中写多个方法需要继承MappingDispatchAction

    原本action里只有一个默认execute方法,今天我在action里增加了方法后发现页面卡死无法跳转了,原来如果写多个方法要继承DispatchAction类. 一个action中有一个execu ...

  8. java都要caps标点_Java Button.setAllCaps方法代码示例

    import android.widget.Button; //导入方法依赖的package包/类 protected void onCreate(Bundle savedInstanceState) ...

  9. 对设计模式的总结之工厂方法模式和抽象工厂模式

    前言 面向对象编程追求的本质-提高扩展性.可维护性.灵活性和复用性.合理利用面向对象6个原则,能够很好的达到要求.如何利用好就是至关重要的了,前人总结了23+个设计模式能够让初学者更容易学到其中的精髓 ...

最新文章

  1. 胸闷的原因有哪些? 相关解决偏方
  2. eve 服务器在哪个文件夹,eve服务器地址
  3. 关于python的线程安全的一些理解.
  4. js对象数组(JSON) 根据某个共同字段分组
  5. idea导入ssm项目_一个简洁的适合 Java 小白练手的“秒杀”项目
  6. C#企业级开发案例精解:用经典案例学通 NET技术
  7. C/C++中static关键字的作用
  8. 如何优雅地将Markdon格式文件md转为pdf?(使用typora)
  9. model Ensemble
  10. python与java区别-python(一):python与java语法的异同之处
  11. 将数值位转换为字符位后输出
  12. homebre mysql 启动_Mysql闪退问题图文解决办法
  13. 【钛坦白】清华大学李建:深度学习在时空大数据分析中的应用(转载)
  14. 【Python 基础教程】彻底解决python round函数的四舍五入不精确的问题
  15. 不同走法的象棋能否走完整个棋盘问题
  16. Python:实现greedy knapsack贪婪的背包算法(附完整源码)
  17. 【Unity】Unity下载器下载不下IOS/Android等模块的解决办法
  18. VScode修改html代码后,浏览器页面更新不及时
  19. 异贝,通过移动互联网技术,为中小微实体企业联盟、线上链接、线上线下自定义营销方案推送。案例55
  20. 眼图测试(信号完整性测试)-嵌入式多媒体卡eMMC存储芯片

热门文章

  1. 如何让你的ASO优化效果提升10倍?
  2. GCC依赖库顺序问题
  3. Spring mvc 返回json格式 - 龙企阁 - 博客频道 - CSDN.NET
  4. 【转】php中XML、XSLT的结合运用
  5. 用Qt写软件系列一:QCacheViewer(浏览器缓存查看器)
  6. c# winform窗体边框风格的设计
  7. 读书笔记:非营利组织的管理
  8. Eclipse Tips(2):代码颜色设置
  9. python3正则表达式判断ipv4_Python 正则表达式验证IPv4地址
  10. android.graphics.drawable.Drawable.Callback回调接口