问题描述
我试图使视图控制器在单击按钮时更改视图,但我不断收到错误消息:
I am trying to make a view controller change views when a button is clicked but I keep getting an error saying:
我想以编程方式进行所有操作,而不是在Xcode中使用Storyboard。我将在下面提供我的代码。 我正在使用Swift。
I want to do everything programmatically rather then using the Storyboards in Xcode. I will provide my code below. I'm using Swift by the way.
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)
}
}
希望有人可以提供帮助。我已经坚持了几个小时,而且我知道这可能是一个简单的解决方法。
Hope someone can help. I've been stuck on this for a few hours now and I know it probably is a easy fix.
已编辑
错误正在以下行中显示在 AppDelegate.swift 中:
Class AppDelegate:UIResponder,UIApplicationDelegate
Error is showing up in AppDelegate.swift on the line: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,多余的':'表示它具有参数。
这篇关于Swift-线程1:Signal SIGABRT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!