当我尝试切换视图时App崩溃

时间:2011-03-07 13:56:36

标签: iphone objective-c xcode

我是Objective-C的新手,我有一些似乎无法解决的问题! 当我启动我的应用程序时,我有4个按钮!这些按钮中的每一个都应该重定向到另一个XIB文件(并且每个这些XIB文件都有一个按钮可以返回)

然而,当我尝试按下其中一个按钮时,我的应用程序“崩溃”! 这是控制台向我显示的内容:

[Session started at 2011-03-07 14:15:38 +0100.]
2011-03-07 14:15:42.169 Google Calendar[1332:207] -[UIApplication gettingStarted:]: unrecognized selector sent to instance 0x4c17fa0
2011-03-07 14:15:42.173 Google Calendar[1332:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[UIApplication gettingStarted:]: unrecognized selector sent to instance 0x4c17fa0'
*** Call stack at first throw:
(
     0   CoreFoundation                      0x0124bbe9 __exceptionPreprocess + 185
     1   libobjc.A.dylib                     0x013a05c2 objc_exception_throw + 47
     2   CoreFoundation                      0x0124d6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
     3   CoreFoundation                      0x011bd366 ___forwarding___ + 966
     4   CoreFoundation                      0x011bcf22 _CF_forwarding_prep_0 + 50
     5   UIKit                               0x00502a6e -[UIApplication sendAction:to:from:forEvent:] + 119
     6   UIKit                               0x005911b5 -[UIControl sendAction:to:forEvent:] + 67
     7   UIKit                               0x00593647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
     8   UIKit                               0x005921f4 -[UIControl touchesEnded:withEvent:] + 458
     9   UIKit                               0x005270d1 -[UIWindow _sendTouchesForEvent:] + 567
     10  UIKit                               0x0050837a -[UIApplication sendEvent:] + 447
     11  UIKit                               0x0050d732 _UIApplicationHandleEvent + 7576
     12  GraphicsServices                    0x01968a36 PurpleEventCallback + 1550
     13  CoreFoundation                      0x0122d064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
     14  CoreFoundation                      0x0118d6f7 __CFRunLoopDoSource1 + 215
     15  CoreFoundation                      0x0118a983 __CFRunLoopRun + 979
     16  CoreFoundation                      0x0118a240 CFRunLoopRunSpecific + 208
     17  CoreFoundation                      0x0118a161 CFRunLoopRunInMode + 97
     18  GraphicsServices                    0x01967268 GSEventRunModal + 217
     19  GraphicsServices                    0x0196732d GSEventRun + 115
     20  UIKit                               0x0051142e UIApplicationMain + 1160
     21  Google Calendar                     0x00002304 main + 102
     22  Google Calendar                     0x00002295 start + 53
)
terminate called after throwing an instance of 'NSException'

以下是我尝试做的事情: 的 h文件

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController {

}
-(IBAction)gettingStarted:(id)sender;
-(IBAction)appointments:(id)sender;

@end

m文件

#import "FirstViewController.h"
#import "AppointmentViewController.h"
#import "GettingStartedViewController.h"

@implementation FirstViewController

-(IBAction)gettingStarted:(id)sender {
     GettingStartedViewController *gettingStartedVC = [[GettingStartedViewController alloc] initWithNibName:nil bundle:nil];
     [self presentModalViewController:gettingStartedVC animated:YES];
}
-(IBAction)appointments:(id)sender {
     AppointmentViewController *appointmentVC = [[AppointmentViewController alloc] initWithNibName:nil bundle:nil];
     [self presentModalViewController:appointmentVC animated:YES];
}
-(void)dealloc {
    [super dealloc];
}

@end

P.S。:创建了两个viewControllers(GettingStartedViewController&amp; AppointmentViewController)!

如果你们需要更多代码(只需发表评论)

THX

凯文

2 个答案:

答案 0 :(得分:0)

似乎gettingStarted:消息被发送到UIApplication对象,而不是FirstViewController对象。在IB中建立连接时,请确保一切正确(文件所有者等)。


这里使用Nib的典型模式是使用Nib创建FirstViewController。您的FirstView_iPhone.xib将具有一个viewcontroller对象,其类名设置为FirstViewController。然后在加载Nib时,将创建FirstViewController对象。您也可以将其方法用作IB的操作。

从技术上讲,您可以向FirstView_iPhone.xib添加外部对象,并将其类设置为FirstViewController,并使用它来建立目标操作连接。

或者,如果要以编程方式创建视图控制器对象,也可以通过编程方式添加目标操作连接(使用setTarget:action:方法)。

答案 1 :(得分:0)

显然,gettingStarted:消息正被发送到错误的对象。打印的控制台输出显示消息正在发送到UIApplication。确保正确连接IB中的操作。按住Ctrl键,将要触发操作的按钮拖动到IB文档中的FirstViewController对象,然后选择gettingStarted:。然后,将NSLog语句添加到方法实现中,以查看它们是否被调用。