popover segue 中无法识别的选择器
Posted
技术标签:
【中文标题】popover segue 中无法识别的选择器【英文标题】:unrecognized selector at popover segue 【发布时间】:2014-05-20 17:22:19 【问题描述】:我在我的应用程序中使用此代码来传递具有相应序列的数据:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([segue.identifier isEqualToString:@"readMoreDetail"])
MoreViewController *destinationViewController = segue.destinationViewController;
destinationViewController.openSubject = [self.detailItem description];
else if ([segue.identifier isEqualToString:@"notesSegue"])
NSLog(@"segue");
NotesTableViewController *destinationViewController = segue.destinationViewController;
destinationViewController.openSubject = [self.detailItem description];
else
// Do nothing
我在 NavigationController 中嵌入了一个 UIViewController 并创建了一个从另一个 UIViewController 到 NavigationController 的弹出框 - 但是当我导航时出现此错误:
-[UINavigationController setOpenSubject:]: unrecognized selector sent to instance 0x15e532a0
有什么想法吗?
谢谢!
NotesTableViewController.h
#import <UIKit/UIKit.h>
@interface NotesTableViewController : UITableViewController <UINavigationControllerDelegate>
@property(nonatomic, strong)NSString *openSubject;
@end
NotesTableViewController.m
#import "NotesTableViewController.h"
@interface NotesTableViewController ()
NSMutableArray *_objects;
@end
@implementation NotesTableViewController
@synthesize openSubject;
- (id)initWithStyle:(UITableViewStyle)style
self = [super initWithStyle:style];
if (self)
// Custom initialization
return self;
- (void)viewDidLoad
[super viewDidLoad];
if (openSubject.length == 0)
// There's currently no subject open, write it in the navigationbar
// Prompt = open subject
// Title = notes header
self.navigationItem.prompt = @"No subject selected";
self.navigationItem.title = @"My Notes";
else
// Open the subject
// Prompt = notes header
// Title = open subject
self.navigationItem.prompt = openSubject;
self.navigationItem.title = @"My notes";
// Load the notes data
// Create the key
NSString *partOfKey = @"-notes";
NSString *notesKey = [NSString stringWithFormat:@"%@%@", openSubject, partOfKey];
// Load the _objects from NSUserdefaults
_objects = [[NSUserDefaults standardUserDefaults] objectForKey:notesKey];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// Register a class or nib file using registerNib:forCellReuseIdentifier
// o registerClass:forCellReuiseIdentifier: method before calling this method
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
- (void)awakeFromNib
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
[super awakeFromNib];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (IBAction)addNote:(id)sender
// Create a new note
if (openSubject.length == 0)
// The openSubject is nil, can't add a subject - tell the user
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"No subject" message: @"Please select a subject prior to adding a note" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show];
else
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New note" message:@"Enter a note" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
// The user created a new subject, add it
if (buttonIndex == 1)
// Get the input text
NSString *newNote = [[alertView textFieldAtIndex:0] text];
// Initialize objects
if (!_objects)
_objects = [[NSMutableArray alloc] init];
// Check if the note already exist
if ([_objects containsObject:newNote])
// Tell the user this note already exists
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Already exists" message: @"This note already exist, sorry" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
else
// The note doesn't exist, add it
[_objects insertObject:newNote atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// Save the new _objects
[self saveObjects];
-(void)saveObjects
// Create the key
NSString *partOfKey = @"-notes";
// Save the new objects
NSString *notesKey = [NSString stringWithFormat:@"%@%@", openSubject, partOfKey];
[[NSUserDefaults standardUserDefaults] setObject:_objects forKey:notesKey];
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// Return the number of rows in the section.
return _objects.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
if (editingStyle == UITableViewCellEditingStyleDelete)
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Save the new objects
[self saveObjects];
else if (editingStyle == UITableViewCellEditingStyleInsert)
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
// Return NO if you do not want the specified item to be editable.
return YES;
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
// Return NO if you do not want the item to be re-orderable.
return YES;
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
*/
@end
(我相信每个人都不会窃取此代码)
【问题讨论】:
顺便说一句:*** 上的所有用户帖子均在此CC license 下获得许可。所以每个人都可以steal
您发布的代码...
基本上,您在某处所称的 NotesTableViewController 不是。如果您查看异常下方的堆栈跟踪,您将被指向错误的行。
【参考方案1】:
如果您触发对UINavigationController
的转场,segue.destinationViewController
将是导航控制器,不是导航控制器中包含的视图控制器。
但是,您可以参考您的 NotesTableViewController
:
UINavigationController *navController = segue.destinationViewController;
NotesTableViewController *notesTableVC = (NotesTableViewController *)[navController topViewController];
然后你就可以使用了
notesTableVC.openSubject = [self.detailItem description];
等等。在NotesTableViewController
中设置属性。
【讨论】:
现在试过了,但它说:“UINavigationController”没有可见的@interface声明了“rootViewController”的选择器 好的,该属性似乎不存在。更新了我的答案。请改用topViewController
。或者 - 如果导航堆栈中有其他视图控制器 - [[navController viewControllers] objectAtIndex:0]
.【参考方案2】:
确保您的 segue 标识符被正确识别为 "notesSegue" 和 "readMoreDetail" ,有时您必须清理项目并再次运行它。
【讨论】:
我已经检查过了——动作确实会触发,因为 NSLog 也会记录,但是这行被指责:destinationViewController.openSubject = [self.detailItem description]; 发布您的 NotesTableViewController。你是如何初始化 openSubject 的? @user3653447 @user3653447 看起来不错。好奇“[self.detailItem description]”是否返回不是字符串的东西,这会导致无法识别的选择器错误。尝试 NSLog "[self.detailItem description]" NSLog 记录一个字符串,就像它应该做的那样:) 很高兴知道您找到了答案@user3653447以上是关于popover segue 中无法识别的选择器的主要内容,如果未能解决你的问题,请参考以下文章
执行 segue 时出错:-[UITextField 长度]:无法识别的选择器发送到实例 [关闭]
无法识别的选择器发送到实例...使用 segues 将字符串传递给另一个视图控制器