我不完全知道最有效的方式來做到這一點(因爲我現在也是這種語言),但它是肯定的,我已經解決了你的問題。我爲此做了一個簡單的程序。在我的例子中涉及的三個類是BaseViewController(它將顯示兩個視圖),LabelView和PickerView(根據您的要求)。
在LabelView.h
@protocol LabelViewDelegate
-(void)didTapTwiceLabelView;
@end
@interface LabelView : UIView {
id <LabelViewDelegate> delegate;
}
@property(nonatomic,retain)id <LabelViewDelegate> delegate;
-(void)didTouch;
@end
在LabelView.m
@synthesize delegate;
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self)
{
UILabel* labl = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, frame.size.width-20,20)];
labl.text = @"Some Text";
[self addSubview:labl];
[labl release]; labl = nil;
self.backgroundColor = [UIColor grayColor];
UITapGestureRecognizer* ges = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouch)] autorelease];
ges.numberOfTapsRequired = 2;
[self addGestureRecognizer:ges];
}
return self;
}
-(void)didTouch
{
[delegate didTapTwiceLabelView];
}
// ====================== =======================================
In Pickerview.h
@protocol PickerViewDelegate
-(void)didTapBackButton;
@end
@interface PickerView : UIView <UIPickerViewDelegate,UIPickerViewDataSource>{
id <PickerViewDelegate> delegate;
}
@property(nonatomic,retain)id <PickerViewDelegate> delegate;
@end
在Pickerview.m
@implementation PickerView
@synthesize delegate;
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self)
{
UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 30, 320, 216)];
picker.delegate = self;
picker.dataSource = self;
[self addSubview:picker];
[picker release]; picker = nil;
self.frame = CGRectMake(frame.origin.x, frame.origin.y, 320, 250);
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10, 1, 50, 27)];
[btn setTitle:@"Back" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
return self;
}
-(void)backButton
{
[delegate didTapBackButton];
}
// ==================================== ================================
在BaseViewController.h
#import "LabelView.h"
#import "PickerView.h"
@interface VarticalLabel : UIViewController<UITextFieldDelegate,PickerViewDelegate,LabelViewDelegate> {
PickerView* myPickerView;
LabelView* myLabelView;
}
@end
在BaseViewController
。 m
-(void)viewDidLoad
{
[super viewDidLoad];
myPickerView= [[PickerView alloc] initWithFrame:CGRectMake(0, 50, 320, 250)];
[self.view addSubview:myPickerView];
myPickerView.delegate = self;
myLabelView= [[LabelView alloc] initWithFrame:CGRectMake(0, 50, 320, 250)];
[self.view addSubview:myLabelView];
myLabelView.delegate = self;
myPickerView.hidden = YES;
}
#pragma mark PickerViewDelgate
-(void)didTapBackButton
{
myPickerView.hidden = YES;
myLabelView.hidden = NO;
}
#pragma mark LabelViewDelegate
-(void)didTapTwiceLabelView
{
myPickerView.hidden = NO;
myLabelView.hidden = YES;
}
是的,我的意思是和我在地圖上一樣的動畫。我的問題在這裏更多,如果應用程序的架構是正確的使用UIView和UIViewController。我不確定是否應該改爲創建三個UIViewControllers。 – dbrasco 2011-02-18 11:55:39
Apple對此有一些建議,通常是正確的:對於一個屏幕的東西,有一個UIViewController。儘管蘋果在各個地方實際上違反了他們自己的規則(!),但作爲'最終用戶'程序員,通常這是個好主意。 – occulus 2011-02-18 12:07:42