主要练习实例
// AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
// AppDelegate.m
#import "AppDelegate.h"
//引入自己创建的视图控制器
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
//创建视图控制器
ViewController *vc = [[ViewController alloc]init];
//用我们创建的视图控制器代替自身的视图控制器
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
// YDview.h
#import <UIKit/UIKit.h>
@interface YDview : UIView
@property(nonatomic,retain)UILabel *lable;
@property(nonatomic,retain)UITextField *textfield;
@property(nonatomic,retain)UIButton *button;
@end
// YDview.m
#import "YDview.h"
@implementation YDview
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
[self addAllable];
}
return self;
}
-(void)addAllable{
_lable = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 70, 30)];
_lable.backgroundColor = [UIColor whiteColor];
_lable.text = @"用 户 名:";
[self addSubview:_lable];
_textfield = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_lable.frame)+20, CGRectGetMinX(_lable.frame), CGRectGetWidth(_lable.frame)+60, CGRectGetHeight(_lable.frame))];
_textfield.backgroundColor = [UIColor whiteColor];
[self addSubview:_textfield];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(CGRectGetMinX(_lable.frame), CGRectGetMaxY(_lable.frame)+20, CGRectGetWidth(_lable.frame)+20, CGRectGetHeight(_lable.frame));
[_button setTitle:@"登陆" forState:UIControlStateNormal];
[_button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
_button.backgroundColor = [UIColor greenColor];
[self addSubview:_button]; }/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code}*/
@end
// ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
#import "ViewController.h"
#import "YDview.h"
@interface ViewController ()<UITextFieldDelegate>
//声明一个
@property(nonatomic,retain)YDview *yview;
@end
@implementation ViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_yview = [[YDview alloc]initWithFrame:[UIScreen mainScreen].bounds];
// _yview.backgroundColor = [UIColor yellowColor];
}
return self;
}
-(void)loadView{
self.view = _yview;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
_yview.textfield.delegate = self;
[_yview.button addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
}
-(void)abc{
NSLog(@"登陆成功呢了");
}
- (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