使用Storyboard将无法识别的选择器发送到实例 [英] Unrecognized selector sent to instance using Storyboards

查看:88
本文介绍了使用Storyboard将无法识别的选择器发送到实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用故事板作为iOS应用程序,我的故事板如下所示: http://d.pr/7yAY (droplr url)

I'm using storyboard for an iOS application, my storyboard looks like this: http://d.pr/7yAY (droplr url)

问题是当我点击登录按钮时,我将捕获的用户名发送到事件表视图控制器。为此,我使用 prepareForSegue 函数,但显然当我尝试设置用户名时抛出异常。

The problem is when I click the login button, I send the username captured to the events table view controller. To do that I use prepareForSegue function, but apparently when I try to set the username in throws an exception.

我的代码如下:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction) logintButton:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *username_tf; // textfield

@end

ViewController。 m

#import "ViewController.h"
#import "EventsTableViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize username_tf;


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"ToMainApp"])
    {
        EventsTableViewController * dest = (EventsTableViewController *)[segue destinationViewController];
        NSString * username = [[NSString alloc] initWithFormat:@"%@", username_tf.text];
        [dest setUsername:username];
    }
}


- (IBAction) logintButton:(id)sender
{
    //NSLog(@"Logint button pressed");
    [self performSegueWithIdentifier:@"ToMainApp" sender:sender];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setUsername_tf:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

EventsTableViewController.h

#import <UIKit/UIKit.h>

@interface EventsTableViewController : UITableViewController
{
    NSString * username;
}

@property (nonatomic, retain) NSString * username;

@end

EventsTableViewController.m

#import "EventsTableViewController.h"

@interface EventsTableViewController ()
@end

@implementation EventsTableViewController

@synthesize username;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

...

@end

抛出的例外是:

2012-03-15 14:19:27.304 StoryboardAssistance[30989:f803]
-[UINavigationController setUsername:]: unrecognized selector sent to instance 0x68abf60 2012-03-15 14:19:27.306
StoryboardAssistance[30989:f803] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason:
'-[UINavigationController setUsername:]: unrecognized selector sent to
instance 0x68abf60'
*** First throw call stack: (0x13c9022 0x155acd6 0x13cacbd 0x132fed0 0x132fcb2 0x28e6 0x43e4be 0xdb5ab 0x2974 0x13cae99 0x1614e 0x160e6
0xbcade 0xbcfa7 0xbc266 0x3b3c0 0x3b5e6 0x21dc4 0x15634 0x12b3ef5
0x139d195 0x1301ff2 0x13008da 0x12ffd84 0x12ffc9b 0x12b27d8 0x12b288a
0x13626 0x253d 0x24a5) terminate called throwing an exception(lldb)

有任何建议吗?

推荐答案

ÿ我们的segue的目标视图控制器是您的导航控制器,而不是您的事件表视图控制器。

Your segue's destination view controller is your Navigation Controller, not your Events Table View controller.

您可以通过访问导航控制器的 topViewController来获取事件控制器 property。

You can get the Events controller by accessing the Navigation controller's topViewController property.

试试这个:

UINavigationController *navController = (UINavigationController*)[segue destinationViewController];
EventsTableViewController *eventsController = [navController topViewController];

NSString * username = [[NSString alloc] initWithFormat:@"%@", username_tf.text];
[eventsController setUsername:username];

这篇关于使用Storyboard将无法识别的选择器发送到实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆