当前位置: 首页 > 面试题库 >

有什么办法可以在Swift中将UIPickerView添加到UIAlertController(Alert或ActionSheet)中?

乜嘉悦
2023-03-14
问题内容

我对Swift(和iOS编程)完全陌生,但是我开始把它弄乱了(当所有东西仍然是beta版本:D时,这不是一个好主意)。所以我试图自己弄清楚,但还是没有。甚至尝试添加包含选择器的子视图都没有成功。那有人可以帮我吗?


问题答案:

好吧,这是我的最终代码。这是一些想法的混合。我接受答案的主要原因是我的代码在Swift中,我的代码使用UIAlertController,我的代码用于选择器。我要感谢Jageen-
我的回答是基于他的想法。

    func showPickerInActionSheet(sentBy: String) {
    var title = ""
    var message = "\n\n\n\n\n\n\n\n\n\n";
    var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.ActionSheet);
    alert.modalInPopover = true;


    //Create a frame (placeholder/wrapper) for the picker and then create the picker
    var pickerFrame: CGRect = CGRectMake(17, 52, 270, 100); // CGRectMake(left), top, width, height) - left and top are like margins
    var picker: UIPickerView = UIPickerView(frame: pickerFrame);

    /* If there will be 2 or 3 pickers on this view, I am going to use the tag as a way
    to identify them in the delegate and datasource. /* This part with the tags is not required.
    I am doing it this way, because I have a variable, witch knows where the Alert has been invoked from.*/
    if(sentBy == "profile"){
        picker.tag = 1;
    } else if (sentBy == "user"){
        picker.tag = 2;
    } else {
        picker.tag = 0;
    }

    //set the pickers datasource and delegate
    picker.delegate = self;
    picker.dataSource = self;

    //Add the picker to the alert controller
    alert.view.addSubview(picker);

    //Create the toolbar view - the view witch will hold our 2 buttons 
    var toolFrame = CGRectMake(17, 5, 270, 45);
    var toolView: UIView = UIView(frame: toolFrame);

    //add buttons to the view
    var buttonCancelFrame: CGRect = CGRectMake(0, 7, 100, 30); //size & position of the button as placed on the toolView

    //Create the cancel button & set its title
    var buttonCancel: UIButton = UIButton(frame: buttonCancelFrame);
    buttonCancel.setTitle("Cancel", forState: UIControlState.Normal);
    buttonCancel.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal);
    toolView.addSubview(buttonCancel); //add it to the toolView

    //Add the target - target, function to call, the event witch will trigger the function call
    buttonCancel.addTarget(self, action: "cancelSelection:", forControlEvents: UIControlEvents.TouchDown);


    //add buttons to the view
    var buttonOkFrame: CGRect = CGRectMake(170, 7, 100, 30); //size & position of the button as placed on the toolView

    //Create the Select button & set the title
    var buttonOk: UIButton = UIButton(frame: buttonOkFrame);
    buttonOk.setTitle("Select", forState: UIControlState.Normal);
    buttonOk.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal);
    toolView.addSubview(buttonOk); //add to the subview

    //Add the tartget. In my case I dynamicly set the target of the select button
    if(sentBy == "profile"){
        buttonOk.addTarget(self, action: "saveProfile:", forControlEvents: UIControlEvents.TouchDown);
    } else if (sentBy == "user"){
        buttonOk.addTarget(self, action: "saveUser:", forControlEvents: UIControlEvents.TouchDown);
    }

    //add the toolbar to the alert controller
    alert.view.addSubview(toolView);

    self.presentViewController(alert, animated: true, completion: nil);
}

func saveProfile(sender: UIButton){
    // Your code when select button is tapped

}

func saveUser(sender: UIButton){
    // Your code when select button is tapped
}

func cancelSelection(sender: UIButton){
    println("Cancel");
    self.dismissViewControllerAnimated(true, completion: nil);
    // We dismiss the alert. Here you can add your additional code to execute when cancel is pressed
}

// returns number of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    if(pickerView.tag == 1){
        return self.profilesList.count;
    } else if(pickerView.tag == 2){
        return self.usersList.count;
    } else  {
        return 0;
    }
}

// Return the title of each row in your picker ... In my case that will be the profile name or the username string
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    if(pickerView.tag == 1){

            var selectedProfile: Profiles = self.profilesList[row] as Profiles;
            return selectedProfile.profileName;

    } else if(pickerView.tag == 2){

            var selectedUser: Users = self.usersList[row] as Users;
            return selectedUser.username;

    } else  {

        return "";

    }

}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if(pickerView.tag == 1){
            var choosenProfile: Profiles = profilesList[row] as Profiles;
            self.selectedProfile = choosenProfile.profileName;
    } else if (pickerView.tag == 2){
            var choosenUser: Profiles = usersList[row] as Users;
            self.selectedUsername = choosenUser.username;
    }

}


 类似资料:
  • 问题内容: 我正在尝试显示带有的。当我添加行时: 我收到一个错误: 致命错误:解开Optional值时意外发现nil 这通过呈现在我的内部。 我已经从这里下载了代码,并且工作正常。我将该方法复制并粘贴到我的代码中,但它中断了。最后一行的 自我 存在没有影响。 问题答案: 因此,我开始检查看看我的代码与工作代码之间可能有什么不同。我注意到我的ViewController扩展了 显然这意味着我需要设置

  • 问题内容: 目前,我的ActionSheet遇到了大麻烦。在iPhone上效果很好,但在iPad上只会崩溃 我只用一个按钮创建一个新项目 正如我在iPhone上所说的那样,它可以工作,但是如果我单击iPad上的按钮,应用程序将崩溃, 2014-09-25 14:54:52.784 test [9541:1970048] 由于未捕获的异常“ NSGenericException”而终止应用程序,原因

  • 问题内容: 有什么方法可以将MouseListener添加到Graphic对象。 我有一个绘制椭圆的简单GUI。 我想要的是在用户单击椭圆形时处理事件 这是有效的,但当单击位于椭圆周围的虚拟框中时会触发。 当点击完全位于椭圆形上时,有人可以帮助我触发它。 提前致谢。 问题答案: 我想到的最简单的方法是避免fillOval并使用java.awt.geom包中的几何。因此,您可以声明一个椭圆,因为看起

  • 我一直在关注快板5平台和他的文件管理器使用的教程!openFile.eof(),我听说它不好,我很确定它是什么让我的矢量下标超出范围错误。除了它,还有什么我可以使用的吗?另外,你能检查一下我的图层类,以防我的矢量下标超出范围错误吗?我想不出来,我很确定它来自文件管理器,但我不知道。 它仅输出地图的第一行。当我把它改成“而”(标准:::getline(打开文件,行))时,我甚至从未去过标准::cou

  • 问题内容: 我正在寻找一种替换Swift中字符的方法。 示例:“这是我的字符串” 我想用“ +”替换“”以获取“ This + is + my + string”。 我该如何实现? 问题答案: 此答案已 针对Swift 4和5 进行了 更新 。如果您仍在使用Swift 1、2或3,请参阅修订历史记录。 您有两种选择。您可以按照@jaumard的建议进行操作并使用 而且,正如下面@cprcrack所

  • 问题内容: 在可选数组的末尾附加元素的正确方法是什么?假设我有一个可选数组myArray,我想在末尾附加“ 99”。Append()在nil数组上不起作用,因此我可以找到的唯一解决方案是以下方法,但是它看起来并不十分优雅: 问题答案: 您可以使用的事实,通过可选的链接调用的方法总是返回一个可选的值,即如果它是 不可能的 调用方法: 如果则则追加新元素并返回,以便不执行if块。 如果则不执行任何操作