ios - 掌握 Xcode + Swift 的高级方法

标签 ios swift xcode

最近开始用story board、Xcode等学习Swift。 我很难理解 ViewController 代码的原理,因为它似乎遗漏了很多基本要素——大概是为了尝试让事情变得更简单——但它不适合来自其他地方的程序员。

import UIKit

class ViewController: UIViewController {

    //Properties

    @IBOutlet weak var textFieldName: UITextField!
    @IBOutlet weak var submit: UIButton!
    @IBOutlet weak var submit1: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    //Actions

    @IBAction func onSubmit(sender: UIButton) {
        switch(sender){
            case submit:
                print("only 1");
            case submit1:
                print("222");
            default:
                print("nothing");
        }

    }
}

在学习了基础教程之后,我尝试自己动手来测试我的理解力。定义 Storyboard元素的 IBOutlets 需要我实际将其拖放到代码中,以便它正确对应于元素。当我刚打字时

@IBOutlet weak var submit1: UIButton!

没有将它从 Storyboard 中拖出 - 它不起作用/对应。所以本质上还有更多我无法控制的后端?

还有, Action 。为什么它在没有调用方法的情况下打印出来?!我一定是遗漏了一些明显的东西——与属性有关的东西?

我希望能够自己编写代码,自己识别元素/按钮/等而无需拖放,也看不到关联 - submit1submit,目前,设置为完全相同的东西:UIButton!

我知道并接受我的困惑和解释显然有缺陷和错误。但是有人可以阐明 ViewController 的较低级别事件吗?

谢谢。

最佳答案

如果你不想拖放你可以写,我看不出问题在哪里

let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = .greenColor()
button.setTitle("Test Button", forState: .Normal)
button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)

self.view.addSubview(button)

关于ios - 掌握 Xcode + Swift 的高级方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38700652/

相关文章:

ios - TableView NSInternalInconsistencyException

ios - 如何使用 Sentry 获取 IOS SDK 开发的崩溃日志

ios - 我所有应用程序的 Apple Mach-O 链接器错误

ios - 如何使用 Objective-C 在 iOS 中的 PDF 上绘制平滑的墨水注释

ios - didReceiveMemoryWarning : what type of memory?

ios - 如何将 uitextfield 字符范围限制为 10 位

swift - 结合AVCaptureSession翻UIPageViewController页面

ios - SKLightNodes 导致 SpriteKit 中的帧速率问题

repeatActionForever : "missing argument for parameter ' completion'"? 的 Swift 错误

ios - 如何在Objective-C中将二维二维整数数组对象添加到NSMutableArray?