【问题标题】:Why my UIPickerview is not working in UIAlertcontroller?为什么我的 UIPickerview 在 UIAlertcontroller 中不起作用?
【发布时间】:2016-05-27 14:41:35
【问题描述】:

我需要在警报视图控制器中显示一个选择器视图,为此我遵循的是

//对于alertview控制器

let alertController = UIAlertController(title: "Hey!", message: "MESSAGE?", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)

//对于pickerview

let monthlyStatement = UIStoryboard(name: "Second", bundle: nil).instantiateViewControllerWithIdentifier("MonthlyStatement")
monthlyStatement.view.frame = CGRectMake(0, 0, 280, 260)
alertController.view.addSubview(monthlyStatement.view)
presentViewController(alertController, animated: true, completion: nil) 

monthlyStatement 选择器视图将完美显示,但不会滚动,

当我尝试单击(选择器视图/选择器视图组件)时,它会立即关闭我的警报视图控制器。 那么如何解决这些问题。 提前谢谢你

【问题讨论】:

    标签: swift uipickerview uialertcontroller


    【解决方案1】:

    你可以这样实现。

     func showPickerInActionSheet(sentBy: String) {
        var title = "picker"
        var message = "Picker controller";
        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;
        }
    
    }
    

    【讨论】:

    • Ankit Deswal 很抱歉最近回复了你的帖子,现在我会处理这个问题。如果有任何问题我会发布你感谢你的代码
    【解决方案2】:
    alertController.view.addSubview(monthlyStatement.view)
    

    永远永远不要那样做。您无需手动修改警报控制器的视图。它不是为此而设计的。如果您需要在警报中显示选择器,请制作您自己的类似警报的显示视图控制器。然后,您将完全控制给您带来麻烦的界面和解除行为。

    【讨论】:

    • 很抱歉最近回复您的帖子并感谢您指出该注释。我现在将按照这种方式工作
    • 我在这里有一个可下载的示例项目:github.com/mattneub/custom-alert-view-iOS7 当然它没有选择器视图,但这是一个小细节。 — 但是请注意,您还做了第二件非法的事情,将视图控制器的视图直接粘贴到界面中,而没有属性视图控制器层次结构。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多