分配self.delegate失败会导致发送无法识别的选择器 [英] Fail in assigning self.delegate it produce unrecognized selector sent

查看:170
本文介绍了分配self.delegate失败会导致发送无法识别的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个iOS版的新手,我在执行 @protocol 时遇到困难,所以抱歉,如果你认为这是一件容易的事情..

I'm a new in iOS, and I have trouble in implementing @protocol so sorry if you think this is an easy thing..

我一直在搜索stackoverflow.com,网站,并尝试舅舅谷一段时间,我决定在这里问...

i've been searching around stackoverflow.com, the webs and also try uncle Google for a while and I decided to ask here...

主要想法正在从TopViewController调用MyViewController并执行翻转动画
我从创建协议开始。

The main idea is calling a MyViewController from TopViewController and do flip animation I Start with creating the protocols..

    // 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

我使用上面的对象从下面的另一个视图调用,并在其中添加翻转过渡:

I use above object to be called from another view below and add flip transition in it :

// TopViewController.h

#import "MyViewController.h"

@class MapScrollView;

@interface TopViewController : UIViewController <UIScrollViewDelegate,MyViewControllerlDelegate> 
{
    UIScrollView *topScrollView;
    UIButton *infoButton;
}

@end

TopViewController.h实现
//TopViewController.m

TopViewController.h Implementation //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

以下代码产生以下编译器错误..

Below code produce following compiler error..

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'

当我按下infoButton调用 - (void)infoIsTouch:(id)sender方法,然后停在[myView setDelegate:self]行..任何人都可以给我一个提示我的错误在哪里?

Those error is raised when i press infoButton which call -(void)infoIsTouch:(id)sender method, then stop at [myView setDelegate:self] line.. could anyone give me a hint where is my mistakes is ?

注意:如果你厌恶我的英文..自由发表评论,也是..但是之前..我很抱歉,我已经尽力了。

NOTE : If you disgust with my english.. fell free to comment abut that also.. but before that.. i'm sorry i've try my best..

推荐答案

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

My guess is that you have some other variable named myView here, that's intercepting your call to the MyViewController instance you've just created. Try changing the name of that instance, and see what happens.

其他的东西:像你在执行 MyViewController 。更改这一行:

Other stuff: ooks like you have a typo in your implementation of MyViewController. Change this line:

@implementation FanglesMapSettingsViewController

@implementation MyViewController

你应该很好去。 (你得到这个错误,因为头文件与任何实现不符,因为打字错误,所以你永远不会得到你的 @synthesize委托的好处; 调用

And you should be good to go. (You're getting that error because the header file doesn't correspond to any implementation, due to the typo, so you never get the benefit of your @synthesize delegate; call.

另外,请确保您在 TopViewController 中声明正确的协议:

Also, make sure you declare the proper protocol in TopViewController:

@interface TopViewController : UIViewController <UIScrollViewDelegate, MyViewControllerlDelegate> 

将协议声明更改为:

@protocol MyViewControllerlDelegate<NSObject>

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

Other than that, things are looking good.

这篇关于分配self.delegate失败会导致发送无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆