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

古い投稿ですが、同じ問題を抱えている他の人は、ID インスペクターでクラスを正しいクラスに設定してください。それが私の問題でした。おそらく、Xcode で最も忘れられていることです。

于 2012-05-17T13:35:00.353 に答える
3

@synthesize delegate;どこかにいますか?ああ、あります。Derp。

OK-そう-これはランタイムエラーです。コンパイラの警告などは気にしないでください(つまり、間違いなく修正しますが、コンパイラは実行中でクラッシュするコードを生成しています)。

認識されないセレクターは、オブジェクトがそのメソッドに応答しないオブジェクトでメソッドを呼び出そうとしたことを意味します。この場合、バックトレースはそれほど有用ではない可能性があります。クラッシュの場所と、クラッシュする理由をすでに特定しました。

クラッシュしている行にブレークポイントを設定してから、デバッガーを使用して上記のオブジェクトに問い合わせてみてください。

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

...など..。

ひどく明白なことが起こっている可能性がありますが、私はそれを手に負えないと思います。

于 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 に答える