4

我是 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] 行停止。谁能给我提示我的错误在哪里?

注意:如果您对我的英语感到厌恶.. 也可以随意发表评论.. 但在那之前.. 对不起,我已经尽力了..

4

3 回答 3

8

旧帖子,但对于其他有相同问题的人,请确保您将身份检查器中的类设置为正确的类。那是我的问题。可能是 Xcode 中最容易被遗忘的事情。

于 2012-05-17T13:35:00.353 回答
3

你在@synthesize delegate;某个地方吗?哦,是的。德普。

好的——所以——这是一个运行时错误。编译器警告等,该死的(我的意思是——肯定修复它们,但编译器正在生成正在运行和崩溃的代码)。

无法识别的选择器意味着尝试调用对象上的方法,而该对象不响应所述方法。在这种情况下,回溯不太可能非常有用。您已经确定了崩溃位置以及崩溃的原因。

尝试在崩溃的行上设置断点,然后使用调试器来询问所述对象;

po object
po (id) [object class]
p (BOOL) [object respondsToSelector: @selector(setDelegate:)]

... ETC ...

可能有一些非常明显的事情正在发生,但我没有立即看到它。

于 2010-12-05T18:43:24.897 回答
2

我的猜测是你在myView这里命名了一些其他变量,它拦截了MyViewController你对刚刚创建的实例的调用。尝试更改该实例的名称,看看会发生什么。

其他内容:看起来您在MyViewController. 更改此行:

@implementation FanglesMapSettingsViewController

对此:

@implementation MyViewController

你应该很高兴。(您会收到该错误,因为由于拼写错误,头文件与任何实现都不对应,因此您永远无法从@synthesize delegate;调用中受益。

另外,请确保您在以下位置声明了正确的协议TopViewController

@interface TopViewController : UIViewController <UIScrollViewDelegate, MyViewControllerlDelegate> 

并将您的协议声明更改为:

@protocol MyViewControllerlDelegate<NSObject>

除此之外,情况看起来不错。

于 2010-12-05T18:20:05.550 回答