简体   繁体   中英

XCode 6/Swift button label

The code below shows what I have found by searching for help but have been unable to find something that shows me what I am really looking for. I'm trying to get the value of the button's label that shows in the Identity Inspector under the Document section and on the scene tree.

在此输入图像描述

@IBAction func pressedKeypad(sender: UIButton) {
    print(sender.titleLabel?.text)
    print(sender.titleLabel)
    print(" keytitle is \(keytitle).")        
}

The above code yields these three outputs, but they are not what I want.

Optional("3")

Optional(<UIButtonLabel: 0x7b7b10a0; frame = (27 14; 18.5 36); text = '3'; alpha = 0.2; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7b7b11c0>>)

keytitle is 3

The label shown under the Document section ( btn02 in your screen shot) is purely internal to Interface Builder and cannot be retrieved via code. When the app runs and the nib loads, the label doesn't even exist . It is purely for your use in identifying a view within Interface Builder.

If the problem is that you are looking for a way to identify which button sent you the pressedKeypad message, you can use something like its tag (which you can also assign in Interface Builder: you assign it as a string but it arrives to you as a number).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM