@interface MyView : UIView @property (strong, nonatomic) UIButton *myBtn; @end
#import "MyView.h"
@implementation MyView
//view的初始化方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{ //初始化按钮
_myBtn = [[UIButton alloc] initWithFrame:CGRectMake(140, 100, 100, 50)];
_myBtn.backgroundColor = [UIColor redColor];
//将按钮添加到自身
[self addSubview:_myBtn];
}
return self;
}
@end
#import <UIKit/UIKit.h> @interface MyViewController : UIViewController @end
#import "MyViewController.h"
#import "MyView.h"
@interface MyViewController ()
@property (strong, nonatomic) MyView *myview;
@end
@implementation MyViewController
- (void)loadView
{
MyView *myView = [[MyView alloc] initWithFrame: [[UIScreen mainScreen] bounds] ];
self.view = myView;
self.myview = myView;
//在controller中设置按钮的目标-动作,其中目标是self,也就是控制器自身,动作是用目标提供的BtnClick:方法,
[self.myview.myBtn addTarget:self
action:@selector(BtnClick:)
forControlEvents:UIControlEventTouchUpInside];
}
//MyView中的按钮的事件
- (void)BtnClick:(UIButton *)btn
{
NSLog(@"Method in controller.");
NSLog(@"Button clicked.");
}
#import "AppDelegate.h"
#import "MyViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [ [UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds ] ];
MyViewController *myVC = [[MyViewController alloc] init];
self.window.rootViewController = myVC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有