自定义表视图控制器因“initWithCoder:无法识别的选择器发送到实例”而崩溃
Posted
技术标签:
【中文标题】自定义表视图控制器因“initWithCoder:无法识别的选择器发送到实例”而崩溃【英文标题】:Custom Table View Controller crashes with "initWithCoder: unrecognized selector sent to instance" 【发布时间】:2015-07-20 08:27:31 【问题描述】:基于此处提供的示例代码 (Show Timelines),我创建了一个自定义 TWTRSearchViewController:
.h:
#import <TwitterKit/TwitterKit.h>
@interface TWTRSearchViewController : TWTRTimelineViewController
@end
.m:
#import "TWTRSearchViewController.h"
@interface TWTRSearchViewController ()
@end
@implementation TWTRSearchViewController
- (instancetype)init
TWTRAPIClient *APIClient = [[Twitter sharedInstance] APIClient];
TWTRSearchTimelineDataSource *searchTimelineDataSource = [[TWTRSearchTimelineDataSource alloc] initWithSearchQuery:@"#twitterflock" APIClient:APIClient];
return [super initWithDataSource:searchTimelineDataSource];
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
这是 TWTRTimelineViewController.h 的头文件:
//
// TWTRTimelineViewController.h
// TwitterKit
//
// Copyright (c) 2015 Twitter. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TWTRTimelineDataSource.h"
/**
This class is a `UITableViewController` subclass that displays `TWTRTweetTableViewCell` cells. It handles cell-reuse, cell-configuration, and loading more tweets from the given timeline once the last cell is reached.
## Usage
Initialize this class with any object that conforms to the `TWTRTimelineDataSource` protocol. We provide two such classes, `TWTRUserTimelineDataSource` and `TWTRSearchTimelineDataSource`. These provide `TWTRTweet` objects to this table view which then configures the instances of `TWTRTweetTableViewCell`.
// Create the data source
TWTRAPIClient *client = [Twitter sharedInstance].APIClient;
TWTRUserTimelineDataSource *dataSource = [[TWTRUserTimelineDataSource alloc] initWithScreenName:@"jack" APIClient:client];
// Create the timeline view controller
TWTRTimelineViewController *timeline = [[TWTRTimelineViewController alloc] initWithDataSource:dataSource];
## Loading More
This class loads the first batch of `TWTRTweet` objects from the Twitter API when `viewWillAppear:` is received. It also handles loading more tweets automatically once the last cell has been shown.
*/
@interface TWTRTimelineViewController : UITableViewController
/**
Initializes a timeline view controller. Does not start loading tweets until
`viewWillAppear:` is called.
This method must be used to initialize this class. The `init` method is unavailable.
@param dataSource Required. A timeline data source object that conforms to the `TWTRTimelineDataSource` protocol.
@return A fully initialized `TWTRTimelineViewController` or nil if the data source is missing.
*/
- (instancetype)initWithDataSource:(id<TWTRTimelineDataSource>)dataSource __attribute((nonnull)) NS_DESIGNATED_INITIALIZER;
/**
The source of `TWTRTweet` objects for this `TWTRTimelineViewController`.
May be set to update the tweets being shown by this table view. Must be set on the main thread.
*/
@property (nonatomic, strong) id<TWTRTimelineDataSource> dataSource;
- (instancetype)initWithStyle:(UITableViewStyle)style __attribute__((unavailable("Use -initWithDataSource: instead")));
- (id)initWithCoder:(NSCoder *)aDecoder __attribute__((unavailable("Use -initWithDataSource: instead")));
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil __attribute__((unavailable("Use -initWithDataSource: instead")));
@end
应用启动后立即崩溃并显示错误消息:-[TWTRSearchViewController initWithCoder:]: unrecognized selector sent to instance 0x7a88f1d0
。应用甚至在调用 init
或 viewDidLoad
之前就崩溃了。
我的代码有什么问题?知道我可能错过了什么吗?
谢谢
【问题讨论】:
【参考方案1】:在您的子类中覆盖 initWithCoder:
方法并执行与当前 init
中相同的操作
【讨论】:
难道没有比使用情节提要更好的方法来实例化我的视图控制器吗?【参考方案2】:我也遇到过类似的错误,但也没有合乎逻辑的解释。即使滑动派生数据等也不起作用。我最终所做的是,创建一个名称中带有“2”的新类以将其与第一个类区分开来,从崩溃的视图控制器中复制代码,然后它就可以工作了。
【讨论】:
以上是关于自定义表视图控制器因“initWithCoder:无法识别的选择器发送到实例”而崩溃的主要内容,如果未能解决你的问题,请参考以下文章
你在哪里设置子视图的图层属性?为啥不在 initWithCoder 中
当覆盖 initWithCoder 时,总是需要调用 [super initWithCoder: coder]
为啥 UITableViewCell 初始化在 initWithCoder 中不起作用
将我的 viewController 移到 NavController 下,现在 awakeFromNib 和 initWithCoder 不会触发?