View和Controller代码组织

//
// MyView.h
//

#import <UIKit/UIKit.h>

@interface MyView : UIView {
UIButton
* m_button;
}

/**
* 关联event,delegate
*/
-(void)connection:(id)target;
-(void)setButtonText:(NSString*)text;

@end

//
// MyView.m
//

#import "MyView.h"
@implementation MyView


- (id)initWithFrame:(CGRect)frame
{
self
= [super initWithFrame:frame];
if (self) {
/**
* 组装view
*/
m_button
= [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[m_button setTitle:
@"tap me!" forState:UIControlStateNormal];
[self addSubview:m_button];
}
return self;
}

-(void)connection:(UIController*)target{
[m_button addTarget:target action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)setButtonText:(NSString*)text{
[m_button setTitle:text forState:UIControlStateNormal];
}

/**
* 调整布局
*/
-(void)layoutSubviews{
m_button.frame
= CGRectMake(100, 100, 64, 24);
}

- (void)dealloc
{
[m_button release];
[super dealloc];
}

@end

//
// MyController.h
//

#import <UIKit/UIKit.h>

@class MyView;
@interface MyController : UIViewController {
/**
* 关联view
*/
MyView
* m_myView;

/**
* 模型数据
*/
NSInteger m_tapCount;
}

@end

//
// MyController.m
//

#import "MyController.h"
#include
"MyView.h"

@implementation MyController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self
= [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
/**
* 初始化controller
*/
self
->m_tapCount = 0;
}
return self;
}

- (void)dealloc
{
[m_myView release];
[super dealloc];
}

//-(void)didReceiveMemoryWarning
//{
// [super didReceiveMemoryWarning];
//}

#pragma mark - event

-(void)buttonClick:(id)sender{
[m_myView setButtonText:[NSString stringWithFormat:
@"%d", ++self->m_tapCount]];
}

/**
* 加载自定义view
*/
- (void)loadView
{
m_myView
= [[MyView alloc] initWithFrame:CGRectZero];
self.view
= m_myView;
[m_myView connection:self];
}

//- (void)viewDidLoad
//{
// [super viewDidLoad];
//}
//
//- (void)viewDidUnload
//{
// [super viewDidUnload];
//}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

  

posted @ 2011-07-23 17:12  Delon  阅读(536)  评论(0编辑  收藏  举报