gpt4 book ai didi

objective-c - 无法分配 self.delegate 它会产生无法识别的选择器发送

转载 作者:可可西里 更新时间:2023-11-01 05:03:35 25 4
gpt4 key购买 nike

我是 iOS 的新手,我在实现 @protocol 时遇到了麻烦,如果您认为这是一件容易的事,我深表歉意。

我一直在 stackoverflow.com、网络上搜索,也尝试过谷歌叔叔一段时间,我决定在这里问...

主要思想是从 TopViewController 调用 MyViewController 并做翻转动画我从创建协议(protocol)开始......

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

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

// 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] 行停止。任何人都可以提示我我的错误在哪里吗?

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

最佳答案

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

关于objective-c - 无法分配 self.delegate 它会产生无法识别的选择器发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4360372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com