popover segue中无法识别的选择器

时间:2014-05-20 17:22:19

标签: ios objective-c uiviewcontroller uinavigationcontroller segue

我在我的应用中使用此代码传递带有相应seque的数据:

-(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创建了一个popover segue到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

(我相信每个人都不会窃取此代码)

2 个答案:

答案 0 :(得分:1)

如果您触发segue到UINavigationControllersegue.destinationViewController将成为导航控制器而导航控制器中包含的视图控制器。

但您可以参考NotesTableViewController

UINavigationController *navController = segue.destinationViewController;
NotesTableViewController *notesTableVC = (NotesTableViewController *)[navController topViewController];

然后你可以使用

notesTableVC.openSubject = [self.detailItem description];

等。在NotesTableViewController

中设置属性

答案 1 :(得分:0)

确保您的segue标识符被正确识别为“notesSegue”和“readMoreDetail”,有时您必须清理项目并再次运行它。

enter image description here