UIActionSheet in swift puts Cancel button top in iOS 7

ε祈祈猫儿з 提交于 2019-12-19 13:04:10

问题


I am in the process of rewriting my app from objective c to Swift and I noticed that UIActionSheet behaves differently in Swift version than in obj-c version.

Obj-c version

Swift version

This is a problem only on iOS 7, it works fine (meaning the cancel is on the bottom) on iOS 8 for both Swift and Obj-c versions

Here is relevant piece of code:

var sheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil)
sheet.addButtonWithTitle("Camera")
sheet.addButtonWithTitle("Photo Library")
sheet.showInView(self.controller.view)

Any idea how to fix it?


回答1:


Turns out this was yet another case of finding answer right after asking the question.

All I had to do was add Cancel button as another button and then specify its index:

var sheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)
sheet.addButtonWithTitle("Camera")
sheet.addButtonWithTitle("Photo Library")
sheet.addButtonWithTitle("Cancel")
sheet.cancelButtonIndex = 2
sheet.showInView(self.controller.view)

Not sure if they changed the way how UIActionSheet is supposed to work in Swift or if it's bug that nobody cares to fix since it's deprecated in iOS 8 anyway




回答2:


Here's what I've got on the viewDidLoad() or on the button action put the code like this:

let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done", otherButtonTitles: "Yes", "No")
          actionSheet.showInView(self.view)

then add another function for their actions like this:

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
    {
        switch buttonIndex{

        case 0:
            NSLog("Done");
            break;
        case 1:
            NSLog("Cancel");
            break;
        case 2:
            NSLog("Yes");
            break;
        case 3:
            NSLog("No");
            break;
        default:
            NSLog("Default");
            break;
            //Some code here..

        }

    }


来源:https://stackoverflow.com/questions/28116417/uiactionsheet-in-swift-puts-cancel-button-top-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!