ios - swift -线程 1 : Signal SIGABRT

标签 ios swift cocoa-touch

我试图让 View Controller 在单击按钮时更改 View ,但我一直收到错误提示:

Thread 1: signal 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 中出现错误: 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,额外的“:”表示它有一个参数。

关于ios - swift -线程 1 : Signal SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35628892/

相关文章:

ios - 从应用内苹果获取productId-s列表

swift 4 : Change/Clear PDFView content

json - 无法在 Swift 中解析 JSON

ios - 如何使用 map 在 Swift 中改变结构?

ios - 有什么可以阻止 -[NSData bytes] 成为悬空指针吗?

ios - 有没有办法利用自定义动画的 CoreAnimation 完成曲线数学?

iphone - CLLocationManager 没有获取城市名称

iphone - NSString - 如何将 LONDON 转换为伦敦

html - iOS 邮件客户端不呈现 html 电子邮件

iphone - 如何设置模态视图 Controller 的标题栏标题?