iOS应用程序上的Swift按钮错误,但在操场上没有

时间:2016-08-26 11:36:19

标签: ios swift

我正在努力学习迅速为我的国家制作免费的教育应用程序。

我只了解编程和swift的基础知识,但我试图了解iOS开发。

我认为我的范围有问题,但不知道该怎么做。

根据我读过的书籍,didMoveToView是(必需的),我无法编译和执行代码。因此,当我在Xcode iOS App Project中运行它时,我的应用程序崩溃了(即使它在游乐场中运行)。

代码:

import UIKit
class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    class fooo {
        static var bar = "hello world!"

        @IBAction func hello_button(sender: AnyObject) {
            print(fooo.bar)
        }
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

以下是崩溃日志(按钮触摸时发生):

2016-08-26 07:22:00.512 k[2074:37791] -[k.ViewController hello_button:]: unrecognized selector sent to instance 0x7aa2e670
2016-08-26 07:22:00.521 k[2074:37791] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[k.ViewController hello_button:]: unrecognized selector sent to instance 0x7aa2e670'
*** First throw call stack:
(
    0   CoreFoundation                      0x00200494 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01f17e02 objc_exception_throw + 50
    2   CoreFoundation                      0x0020a253 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0013f89d ___forwarding___ + 1037
    4   CoreFoundation                      0x0013f46e _CF_forwarding_prep_0 + 14
    5   libobjc.A.dylib                     0x01f2c0b5 -[NSObject performSelector:withObject:withObject:] + 84
    6   UIKit                               0x009f2e38 -[UIApplication sendAction:to:from:forEvent:] + 118
    7   UIKit                               0x009f2db7 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    8   UIKit                               0x00b96f3b -[UIControl sendAction:to:forEvent:] + 79
    9   UIKit                               0x00b972d4 -[UIControl _sendActionsForEvents:withEvent:] + 433
    10  UIKit                               0x00b962c1 -[UIControl touchesEnded:withEvent:] + 714
    11  UIKit                               0x00a7352e -[UIWindow _sendTouchesForEvent:] + 1095
    12  UIKit                               0x00a745cc -[UIWindow sendEvent:] + 1159
    13  UIKit                               0x00a15be8 -[UIApplication sendEvent:] + 266
    14  UIKit                               0x009ea769 _UIApplicationHandleEventQueue + 7795
    15  CoreFoundation                      0x00112e5f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    16  CoreFoundation                      0x00108aeb __CFRunLoopDoSources0 + 523
    17  CoreFoundation                      0x00107f08 __CFRunLoopRun + 1032
    18  CoreFoundation                      0x00107846 CFRunLoopRunSpecific + 470
    19  CoreFoundation                      0x0010765b CFRunLoopRunInMode + 123
    20  GraphicsServices                    0x04717664 GSEventRunModal + 192
    21  GraphicsServices                    0x047174a1 GSEventRun + 104
    22  UIKit                               0x009f0eb9 UIApplicationMain + 160
    23  k                                   0x00012581 main + 145
    24  libdyld.dylib                       0x02935a25 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

2 个答案:

答案 0 :(得分:4)

你是对的,问题是范围(假设你按照别人提到的那样正确连接你的按钮):

import UIKit

class ViewController: UIViewController {

    class fooo {
        var bar = "hello world!"
        func mSayBar() { print(self.bar)}
    }

    var foo = fooo()

    @IBAction func hello_button(sender: AnyObject) {
        foo.mSayBar()
    }

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

viewDidLoad是它自己的本地范围 - 它不会比结束支撑更进一步延伸#34; }" 在第一个(毫秒?)之后,在VDL中声明的所有内容都不再存在;因此,最好是初始化或调用,而不是声明。

要声明全局,只需将所有内容放在新的swift文件中,或将其放在所有类范围之外。 Swift使全局范围变得容易(但要小心)

您说viewDidLoad是必需的,这是真的,但仅适用于设置程序。您的大多数逻辑/函数都超出viewDidLoad

答案 1 :(得分:2)

listViewDoc.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int myPosition = position; String id_doc_from_lv = listViewDoc.getItemAtPosition(myPosition).toString(); // selected item String tv_ID_Str = ((TextView)view.findViewById(R.id.tvLvDocID)).getText().toString(); Toast.makeText(getApplicationContext(), "ID is : " + tv_ID_Str, Toast.LENGTH_LONG).show(); } }); } catch (Exception e) { e.printStackTrace(); } 表示该按钮已链接到代码中未定义的unrecognized selector sent to instance 0x7aa2e670插座。转到ViewController的Storyboard模式,然后右键单击子视图列表中的按钮。检查是否有 Touch Up Inside 事件的插座,以及它是否与您要调用的代码中的功能同名(在您的情况下为IBAction

如果你不理解上面的段落,这是一张图片。

enter image description here