无法解决"无法识别的选择器已发送到实例"错误

时间:2014-03-23 16:58:08

标签: objective-c segue nib unrecognized-selector ios7.1

我在其他堆栈溢出帖子上读到这个错误是由于内存管理不当造成的,但我无法弄清楚该问题的原因。我收到以下错误:

Debug Image

2014-03-23 12:47:16.337 Cazenovia High School[11810:60b] -[iPhoneFirstPageView Twitter]: unrecognized selector sent to instance 0x10944bc10
2014-03-23 12:47:16.342 Cazenovia High School[11810:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[iPhoneFirstPageView Twitter]: unrecognized selector sent to instance 0x10944bc10'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010194c495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001016ab99e objc_exception_throw + 43
    2   CoreFoundation                      0x00000001019dd65d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010193dd8d ___forwarding___ + 973
    4   CoreFoundation                      0x000000010193d938 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x0000000100259f06 -[UIApplication sendAction:to:from:forEvent:] + 80
    6   UIKit                               0x0000000100259eb4 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
    7   UIKit                               0x0000000100336880 -[UIControl _sendActionsForEvents:withEvent:] + 203
    8   UIKit                               0x0000000100335dc0 -[UIControl touchesEnded:withEvent:] + 530
    9   UIKit                               0x000000010057d6f7 _UIGestureRecognizerUpdate + 5149
    10  UIKit                               0x0000000100290a15 -[UIWindow _sendGesturesForEvent:] + 928
    11  UIKit                               0x00000001002916d4 -[UIWindow sendEvent:] + 909
    12  UIKit                               0x000000010026929a -[UIApplication sendEvent:] + 211
    13  UIKit                               0x0000000100256aed _UIApplicationHandleEventQueue + 9579
    14  CoreFoundation                      0x00000001018dbd21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x00000001018db5f2 __CFRunLoopDoSources0 + 242
    16  CoreFoundation                      0x00000001018f746f __CFRunLoopRun + 767
    17  CoreFoundation                      0x00000001018f6d83 CFRunLoopRunSpecific + 467
    18  GraphicsServices                    0x0000000103ac3f04 GSEventRunModal + 161
    19  UIKit                               0x0000000100258e33 UIApplicationMain + 1010
    20  Cazenovia High School               0x0000000100002ef3 main + 115
    21  libdyld.dylib                       0x0000000101fe45fd start + 1
    22  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我的代码包含在下面,您也可以在http://github.com/rileylloyd24/cazenovia

上找到我的项目

我的Storyboard(ViewController)中有一个带有嵌入式导航控制器(navigationController)的ScrollView。我还有2个笔尖,其中一个(iPhoneFirstPageView)我试图将其转移到另一个笔尖(TwitterViewController)。

ViewController.h

#import "ViewController.h"
#import "TwitterViewController.h"
#import "iPhoneFirstPageView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Set up View for scrollview with nav controller
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"Cazenovia Central School District";
    self.bottomBar.backgroundColor = [UIColor colorWithRed:9.0f/255.0f green:49.0f/255.0f blue:102.0f/255.0f alpha:1.0f];

    //do some set up on the scrollview
    self.theScrollView.pagingEnabled = YES;
    self.theScrollView.showsHorizontalScrollIndicator = NO;
    self.theScrollView.showsVerticalScrollIndicator = NO;

    //Load the nibs to be paged through
    //NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"iphoneFirstPageView" owner:self options:nil];
    //UIView *firstPageView = (UIView *)[nibArray objectAtIndex:0];

    NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"iphoneFirstPageView" owner:self options:nil];
    iPhoneFirstPageView *firstPageView = (iPhoneFirstPageView *)[nibArray objectAtIndex:0];
    [firstPageView.twitterButton addTarget:self action:@selector(executeSegue) forControlEvents:UIControlEventTouchUpInside];

    NSArray *nibArray2 = [[NSBundle mainBundle] loadNibNamed:@"iPhoneSecondPage" owner:self options:nil];
    UIView *secondPageView = (UIView *)[nibArray2 objectAtIndex:0];

    NSArray *pageArray = @[firstPageView, secondPageView];

    //Add each view to the scroll view
    for(int i=0; i<pageArray.count; i++){
        CGRect frame;
        frame.origin.x = self.theScrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.theScrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        [subview addSubview:[pageArray objectAtIndex:i]];
        [self.theScrollView addSubview:subview];
    }
    self.theScrollView.contentSize = CGSizeMake(self.theScrollView.frame.size.width * pageArray.count, self.theScrollView.frame.size.height);
    self.theScrollView.delegate = self;
    self.thePageControl.numberOfPages = pageArray.count;
    //Twitter segue
}


-(void)executeSegue
{
    TwitterViewController *newController = [[TwitterViewController alloc] initWithNibName:@"TwitterViewController" bundle:nil];
    [self.navigationController pushViewController:newController animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    CGFloat pageWidth = self.theScrollView.frame.size.width;
    int page = floor((self.theScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.thePageControl.currentPage = page;
}

1 个答案:

答案 0 :(得分:1)

我看了你的代码,你需要从twitter图标按钮中删除故事板中的插座。你以前连接过。您已经评论了以前的插播方式-(IBAction)Twitter,但您还需要从storyboard中删除touchupinside动作插座。只需转到iphoneFirstPageView.xib并右键点击Twitter按钮并删除Twitter的动作插座。

-(void)executeSegue

中的另一个问题

您正在从xib加载整个viewcontroller,因此请使用TwitterViewController *newController = [[[NSBundle mainBundle] loadNibNamed:@"TwitterViewController" owner:self options:nil] objectAtIndex:0];而不是initWithNibName并删除文件所有者插座以供查看。然后一切正常。