Swift – 线程1:信号SIGABRT

我试图让一个视图控制器更改视图,当一个button被点击,但我不断收到一个错误说:

线程1:信号SIGABRT

我想以编程方式完成一切,而不是使用Xcode中的Storyboard。 我将在下面提供我的代码。 顺便说一下,我正在使用Swift。

AppDelegate.swift

import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. self.window = UIWindow(frame:UIScreen.mainScreen().bounds) let navController = UINavigationController() let mainViewController = ViewController(nibName:nil, bundle:nil) // NavigationBar/Title colour navController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] UINavigationBar.appearance().barTintColor = UIColor(red:3/255, green:60/255, blue:115/255, alpha:1); // Push the viewcontroller onto the navigation controller navController.pushViewController(mainViewController, animated:false) self.window!.rootViewController = navController self.window?.makeKeyAndVisible() return true } func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } } 

ViewController.swift

 import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.title = "Title" self.view.backgroundColor = UIColor.whiteColor() // button let btn_Test = UIButton() btn_Test.backgroundColor = UIColor.redColor() btn_Test.setTitle("Test Button", forState: UIControlState.Normal) btn_Test.frame = CGRectMake(0, 100, 100, 100) btn_Test.addTarget(self, action:"onTestButtonClicked", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(btn_Test) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //========================================================================= // Button Click Events //========================================================================= func onTestButtonClicked(sender: UIButton) { let webviewController = WebviewController() self.navigationController?.pushViewController(webviewController, animated:true) } } 

希望有人能帮忙。 我已经坚持了几个小时,我知道这可能是一个简单的修复。

编辑

错误显示在AppDelegate.swift上: Class AppDelegate: UIResponder, UIApplicationDelegate

控制台输出:

 2016-02-25 14:07:56.526 MyApp_NativeSwift[1027:6035] -[MyApp_NativeSwift.ViewController onTestButtonClicked]: unrecognized selector sent to instance 0x7fb7c1f13530 2016-02-25 14:07:56.530 MyApp_NativeSwift[1027:6035] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyApp_NativeSwift.ViewController onTestButtonClicked]: unrecognized selector sent to instance 0x7fb7c1f13530' *** First throw call stack: ( 0 CoreFoundation 0x000000010f38de65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001112f5deb objc_exception_throw + 48 2 CoreFoundation 0x000000010f39648d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000010f2e390a ___forwarding___ + 970 4 CoreFoundation 0x000000010f2e34b8 _CF_forwarding_prep_0 + 120 5 UIKit 0x000000010fdd4194 -[UIApplication sendAction:to:from:forEvent:] + 92 6 UIKit 0x000000010ff436fc -[UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x000000010ff439c8 -[UIControl _sendActionsForEvents:withEvent:] + 311 8 UIKit 0x000000010ff42af8 -[UIControl touchesEnded:withEvent:] + 601 9 UIKit 0x000000010fe4349b -[UIWindow _sendTouchesForEvent:] + 835 10 UIKit 0x000000010fe441d0 -[UIWindow sendEvent:] + 865 11 UIKit 0x000000010fdf2b66 -[UIApplication sendEvent:] + 263 12 UIKit 0x000000010fdccd97 _UIApplicationHandleEventQueue + 6844 13 CoreFoundation 0x000000010f2b9a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 14 CoreFoundation 0x000000010f2af95c __CFRunLoopDoSources0 + 556 15 CoreFoundation 0x000000010f2aee13 __CFRunLoopRun + 867 16 CoreFoundation 0x000000010f2ae828 CFRunLoopRunSpecific + 488 17 GraphicsServices 0x00000001148eaad2 GSEventRunModal + 161 18 UIKit 0x000000010fdd2610 UIApplicationMain + 171 19 MyApp_NativeSwift 0x000000010f1abe3d main + 109 20 libdyld.dylib 0x0000000111dfe92d start + 1 21 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) 

你需要改变这一行:

  btn_Test.addTarget(self, action:"onTestButtonClicked", forControlEvents: UIControlEvents.TouchUpInside) 

对此:

  btn_Test.addTarget(self, action:"onTestButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside) 

你已经指定目标的行为作为一个函数名onTestButtonClicked没有参数,额外的':'表示它有一个参数。