Swift - 线程1:信号SIGABRT

时间:2016-02-25 13:38:33

标签: ios swift cocoa-touch

我试图在单击按钮时让视图控制器更改视图,但我一直收到错误消息:

  

线程1:信号SIGABRT

我希望以编程方式完成所有操作,而不是在Xcode中使用Storyboards。我将在下面提供我的代码。 我顺便使用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)

1 个答案:

答案 0 :(得分:1)

您需要更改此行:

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

到此:

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

您已将目标的操作指定为onTestButtonClicked上没有参数的函数名称,额外的“:”表示它有一个参数。