【问题标题】:Unrecognised selector sent to instance of UIViewController (or protocol)?无法识别的选择器发送到 UIViewController(或协议)的实例?
【发布时间】:2014-03-10 22:26:42
【问题描述】:

我是 iOS 开发的新手,在尝试将开源 Wheel Rotating 项目 (https://github.com/funkyboy/How-To-Create-a-Rotating-Wheel-Control-with-UIKit) 嵌入我的项目时遇到了这个问题。

基本上,我把这个开源项目的相关代码文件和资产复制到我的,并做了一些必要的修改,希望它能运行。

“转轮组件”来自原始开源项目,“心情轮盘”是我的项目。

这是项目结构的屏幕截图:

还有堆栈跟踪:

2014-03-10 13:06:12.865 Mood Roulette[13125:60b] cl is 7 | 0.392699, 0.785398, 1.178097
2014-03-10 13:06:12.866 Mood Roulette[13125:60b] -[UCViewController wheelDidChangeValue:]: unrecognized selector sent to instance 0x146d175f0
2014-03-10 13:06:12.869 Mood Roulette[13125:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UCViewController wheelDidChangeValue:]: unrecognized selector sent to instance 0x146d175f0'
*** First throw call stack:
(0x18ba1f09c 0x19799dd78 0x18ba23d14 0x18ba21a7c 0x18b9414ac 0x1000c05fc 0x1000bfb44 0x1000c26f4 0x18e8f42c0 0x18e8f4044 0x18e8fb7dc 0x18e8f8ab0 0x18e96c88c 0x18e9691f0 0x18e9629a0 0x18e8f5530 0x18e8f4720 0x18e9620b0 0x191345128 0x191344c54 0x18b9defc8 0x18b9def28 0x18b9dd14c 0x18b91db38 0x18e9612d4 0x18e95c0e8 0x1000c24b8 0x197f87aa0)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

======

错误点在第100行

[self.delegate wheelDidChangeValue:[self getCloveName:currentValue]];

代码:

https://github.com/funkyboy/How-To-Create-a-Rotating-Wheel-Control-with-UIKit/blob/master/RotaryWheelProject/SMRotaryWheel.m

这是我更改的部分(主要是在ViewControllers 中进行的更改):

UCAppDelegate.h:

#import <UIKit/UIKit.h>

//@class UCViewController;
@interface UCAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
//@property (strong, nonatomic) UCViewController *viewController;

@end

UCAppDelegate.m:

@implementation UCAppDelegate

//@synthesize window;
//@synthesize viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
//    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    self.window.rootViewController = self.viewController;
//    [self.window makeKeyAndVisible];
    return YES;
}

UCViewController.h

#import <UIKit/UIKit.h>
#import "SMRotaryProtocol.h"

@interface UCViewController : UIViewController


@property (nonatomic, strong) UILabel *valueLabel;

@end

UCViewController.m:

#import "UCViewController.h"
#import "SMRotaryWheel.h"

@interface UCViewController ()

@end

@implementation UCViewController

@synthesize valueLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 350, 120, 30)];
    valueLabel.textAlignment = NSTextAlignmentCenter;
    valueLabel.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:valueLabel];

    SMRotaryWheel *wheel = [[SMRotaryWheel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)
                                                    andDelegate:self
                                                   withSections:8];

    wheel.center = CGPointMake(160, 240);
    [self.view addSubview:wheel];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

项目将毫无问题地构建,但会在运行时留下黑屏并给出无法识别的选择器错误。

还有什么我需要改变的吗?

感谢您的帮助。

【问题讨论】:

    标签: ios objective-c unrecognized-selector


    【解决方案1】:

    根据您发布的内容,您的UCViewController 中没有方法wheelDidChangeValue: 的实际实现。因此,当在运行时调用该方法时,没有方法可以“捕获”该调用,这会导致您看到的错误。

    【讨论】:

    • 感谢您的帮助。但是为什么原来的项目没有在他们的ViewController中实现wheelDidChangeValue,即SMViewController呢?
    • 如果他们的应用程序工作正常,那么 delegate 实现了方法调用。不是每个人都会使用UIViewController 来执行此操作。我会检查他们的代码并追踪他们的委托模式。你能下载他们的项目,设置断点并运行它吗?
    • 是的,他们的应用程序可以构建和运行而没有任何问题。然后我会检查委托模式......
    • 是的,在他们的协议 (github.com/funkyboy/…) 中实现了 wheelDidChangeValue 方法。但是我的项目中也包含了相同的协议文件,为什么我的不起作用?
    【解决方案2】:

    我认为你必须设置 SMRotaryWheel 类的委托:

    #import <UIKit/UIKit.h>
    #import "SMRotaryProtocol.h"
    
    @interface UCViewController : UIViewController <SMRotryWheelDelegate>
    
    
    @property (nonatomic, strong) UILabel *valueLabel;
    
    @end
    

    注意:我实际上不知道如何调用代表

    然后别忘了实现委托方法。

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 2012-08-01
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2019-08-28
      相关资源
      最近更新 更多