ios 使用故事板发送到实例的无法识别的选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9727549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:20:19  来源:igfitidea点击:

Unrecognized selector sent to instance using Storyboards

iosstoryboard

提问by JohnnyAce

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

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

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 prepareForSeguefunction, but apparently when I try to set the username in throws an exception.

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

My code is as follows:

我的代码如下:

ViewController.h

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

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

@end

ViewController.m

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

EventsTableViewController.h

#import <UIKit/UIKit.h>

@interface EventsTableViewController : UITableViewController
{
    NSString * username;
}

@property (nonatomic, retain) NSString * username;

@end

EventsTableViewController.m

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

The throwed exception is:

抛出的异常是:

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)

Any suggestions?

有什么建议?

回答by jonkroll

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

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

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

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

Try this:

尝试这个:

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

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

回答by Patrick

Alternatively, try this:-

或者,试试这个:-

(It does the same as jonkroll's answer but in one line and removes the warning "Incompatible pointer types initializing 'ViewController *__strong' with an expression of type UIViewController *"

(它与 jonkroll 的答案相同,但在一行中删除了警告“不兼容的指针类型用类型的表达式初始化 'ViewController *__strong' UIViewController *

 NameOfViewController *vc = (NameOfViewController *)[[segue destinationViewController] topViewController];

回答by Scott D

Another thing to verify is that you have properly assigned your destination ViewController as the proper class in Interface Builder. By default this will be UIViewControlleror UITableViewControlleretc. If you have a custom class with getters/setters, you need to remember to change the class in the Interface Builderto reflect accordingly, otherwise you will receive an invalid selector error message.

另一件要验证的事情是您已将目标 ViewController 正确分配为 Interface Builder 中的正确类。默认情况下,这将是UIViewControllerUITableViewController等。如果您有一个带有 getter/setter 的自定义类,您需要记住更改 中的类以Interface Builder进行相应的反映,否则您将收到无效选择器错误消息。