我是 iOS 的新手,如果您认为这是一件容易的事情,我在实现@protocol时遇到了麻烦。
我一直在搜索stackoverflow.com,网络,也尝试了Google叔叔一段时间,我决定在这里问......
主要思想是从 TopViewController 调用 MyViewController 并做翻转动画我从创建协议开始..
// This is my Delegate header
// MyViewController.h
@protocol MyViewControllerlDelegate
- (void) myViewControllerDidFinish;
@end
@interface MyViewController : UIViewController
{
id <MyViewControllerlDelegate> delegate;
}
@property (nonatomic, assign) id <MyViewControllerlDelegate> delegate;
- (IBAction) done;
@end
以下是关于实施的:
// MyViewController.m
#import "MyViewController.h"
@implementation MyViewController
@synthesize delegate;
// Another Code above
- (IBAction)done
{
[self.delegate myViewControllerDidFinish];
}
// Another Code Below
@end
我使用上面的对象从下面的另一个视图中调用并在其中添加翻转过渡:
// TopViewController.h
#import "MyViewController.h"
@class MapScrollView;
@interface TopViewController : UIViewController <UIScrollViewDelegate,MyViewControllerlDelegate>
{
UIScrollView *topScrollView;
UIButton *infoButton;
}
@end
TopViewController.h 实现 //TopViewController.m
#import "TopViewController.h"
#import "CustomScrollView.h"
#import <QuartzCore/QuartzCore.h>
- (void)loadView
{
// Step 1: make the outer paging scroll view
CGRect topScrollViewRect = [[UIScreen mainScreen] bounds];
topScrollView = [[UIScrollView alloc] initWithFrame:topScrollViewRect];
topScrollView.pagingEnabled = YES;
topScrollView.backgroundColor = [UIColor blackColor];
topScrollView.showsVerticalScrollIndicator = NO;
topScrollView.showsHorizontalScrollIndicator = NO;
topScrollView.contentSize = CGSizeMake(topScrollViewRecte.size.width,
topScrollViewRecte.size.height);
topScrollView.delegate = self;
self.view = pagingScrollView;
CustomScrollView *sView = [[[MapScrollView alloc] init] autorelease];
sView.frame = [[UIScreen mainScreen] bounds];
[sView displayTiledMap];
[pagingScrollView addSubview:sView];
// add Info Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button addTarget:self action:@selector(infoIsTouch:) forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(282.0, 440.0, 18.0, 19.0);
[self.view addSubview:button];
}
-(void)infoIsTouch:(id)sender
{
MyViewController *myView =
[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
myView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//
//Here where the code is crash
//
[myView setDelegate:self];
//
//Code from here to below is not run...
//
[self presentModalViewController:myView animated:YES];
[myView release];
}
- (void) myViewControllerDidFinish
{
[self dismissModalViewControllerAnimated:YES];
}
etc...
@end
下面的代码产生以下编译器错误..
2010-12-06 01:09:07.946 MyViewDelegatePractice[9473:207] -[TopViewController setDelegate:]: unrecognized selector sent to instance 0x5f30fb0
2010-12-06 01:09:07.949 MyViewDelegatePractice[9473:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TopViewController setDelegate:]: unrecognized selector sent to instance 0x5f30fb0'
*** Call stack at first throw:
(
// Cuts...
)
terminate called after throwing an instance of 'NSException'
当我按下调用 -(void)infoIsTouch:(id)sender 方法的 infoButton 时,会引发这些错误,然后在 [myView setDelegate:self] 行停止。谁能给我提示我的错误在哪里?
注意:如果您对我的英语感到厌恶.. 也可以随意发表评论.. 但在那之前.. 对不起,我已经尽力了..