Swift - 带按钮的枚举?

时间:2014-09-29 16:50:24

标签: objective-c swift enums ios8

我在ObjC中有这个代码 我想或试图将其转换为swift

typedef NS_ENUM(NSInteger, BB3Photo) {
kirkenType = 10 ,
festenType = 20 ,
praestType = 30
};

@property (nonatomic, assign) BB3Photo selectedPhotoType;


- (IBAction)changeImage:(id)sender {
if ([sender isKindOfClass:[UIButton class]]) {
    UIButton *button = sender;
    _selectedPhotoType = button.tag;
}
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Vælg Billed"
                                                   delegate:self
                                          cancelButtonTitle:nil
                                     destructiveButtonTitle:nil
                                          otherButtonTitles:@"Vælg fra Biblioteket", @"Vælg Kamera", nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
[sheet showInView:[self.view window]];

}

这是我从中做出的

    enum BBPhoto1: Int {
    case kommunen = 10
    case sagsbehandler = 20
    case festen = 30
}

var selectedPhoto = BBPhoto1.self

@IBAction func changeImage(sender: AnyObject){
    if sender .isKindOfClass(UIButton){
        let button: UIButton = sender as UIButton
        selectedPHoto = (sender as UIButton).tag as BBPhoto1 // doesent work "cannot assign that result as expression"
        selectedPHoto = button.tag // doesnt work either "cannot assign that result as expression"
        self.selectedPhoto = BBPhoto1.fromRaw((sender as UIButton).tag) // nope "cannot convert the expressions type () to type UIButton"

    }
}

我希望能够将一个带有按钮标签的switch语句添加到同一个funktion但代码中有所不同

4 个答案:

答案 0 :(得分:2)

您希望使用tag作为BBPhoto1枚举的原始值。您可以使用条件展开来执行此操作:

@IBAction func changeImage(sender: AnyObject){
    if let button = sender as UIButton {
        if let photoType = BBPhoto1.fromRaw(button.tag) {
            self.selectedPhoto = photoType
        }
    }
}

您的selectedPhoto财产声明也存在问题。它应该是:

var selectedPhoto: BBPhoto1?

现在你拥有它的方式它不会保留BBPhoto1值,而是BBPhoto1本身的类型


请注意,fromRaw语法已更改为Xcode 6.1中的初始值设定项:

@IBAction func changeImage(sender: AnyObject){
    if let button = sender as UIButton {
        if let photoType = BBPhoto1(rawValue: button.tag) {
            self.selectedPhoto = photoType
        }
    }
}

答案 1 :(得分:1)

怎么样:

@IBAction func changeImage(sender: AnyObject){
    if sender .isKindOfClass(UIButton){
        let button: UIButton = sender as UIButton
        selectedPHoto = BBPhoto1.fromRaw(button.tag)
    }
}

或(更短):

@IBAction func changeImage(sender: UIButton){
    selectedPHoto = BBPhoto1.fromRaw(sender.tag)
}

答案 2 :(得分:1)

好的,不一样的代码;如验证文本字段,但在Swift 4,iOS 11,xCode 9.2中的原理相同。

enum SFT: Int {
    case StudentNameFieldTag
    case StudentIDFieldTag
    case StudentClassFieldTag
    case ChallengeCodeFieldTag
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField.tag == SFT.StudentClassFieldTag.rawValue {
        let whitespaceSet = CharacterSet.whitespaces
        if let _ = string.rangeOfCharacter(from: whitespaceSet) {
            return false
        } else {
            return true
        }
    }
    return true
}

答案 3 :(得分:0)

fromRaw()方法返回一个可选项,因此您必须将属性声明为可选:

var selectedPHoto: BBPhoto1?

并使用此代码:

self.selectedPhoto = BBPhoto1.fromRaw((sender as UIButton).tag)

或者,您可以打开fromRaw返回值:

self.selectedPHoto = BBPhoto1.fromRaw((sender as UIButton).tag)!

但请注意,在这种情况下,如果原始值未映射到枚举,则会引发运行时异常。