2017-08-09 115 views
0

据我所知,我们可以给showAlert中的“No”和“Exit”一个监听器。 所以,如果我们按下退出或确定,我们可以做我们想要的。在iOS中的showAlert中创建radioButton Swift

Are you sure want to exit 
Exit   No 

是否有可能把radioButton放在showAlert中? 例如:

o Juice 
o Food 
o Furniture 
Exit Go 

这样的事情,如果我选择果汁和压去,我会直接到JuiceController。 这是可以做到的吗?或showAlert只能处理2个动作?

+0

这里有一个堆栈关于在警报控制器中放置表视图的问题。 https://stackoverflow.com/a/39253296/5901353 这可能会让你非常接近你正在寻找的东西。试一试,如果您需要进一步的帮助,请用一些代码发布您的问题。 –

+0

使用操作表或使警报 –

+0

@iOSGeek自定义视图任何参考请? – StevenTan

回答

1

请检查此链接。我使用actionSheet按照您的要求制作了一个项目。你可以利用这个代码,以供参考,并在项目中嵌入它

https://drive.google.com/open?id=0Bz8kF1Gedr7fd0JzcTh6c0JVMGs

仿佛要增加动作片的高度是你能做到这一点

方法1 - 使用此增加actionSheet高度,增加它

func showAleert(){ 
     let alertController = UIAlertController(title: "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet) //if you increase actionSheet height to move default button down increase \n \n count and get your layout proper 


     let margin:CGFloat = 10.0 
     let rect = CGRect(x: margin, y: margin, width: alertController.view.bounds.size.width - margin * 4.0, height: 320) 
     let customView = UIView(frame: rect) 

     customView.backgroundColor = .clear //Background colour as clear 

     customView.addSubview(buttonn) 
     customView.addSubview(buttonn1) 
     customView.addSubview(buttonn2) 

     customView.addSubview(label) 
     customView.addSubview(label1) 
     customView.addSubview(label2) 

     alertController.view.addSubview(customView) 
     let ExitAction = UIAlertAction(title: "Go", style: .default, handler: {(alert: UIAlertAction!) in 

      if self.juiceBool == true { 
       //go to juice Controller 
       print("juice") 
      } 
      if self.FoodBool == true{ 
       //go to food controller 
       print("food") 
      } 
      if self.FurnitureBool == true{ 
       //go to furniture controller 
       print("furniture") 
      } 
      if (self.juiceBool == false) && (self.FurnitureBool == false) && (self.FoodBool == false){ 
       //peform your operation like naything you want safety side 
       print("Not selected Any") 
      } 


     }) 

     let height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.80) //here you can define new height for action Sheet 
     alertController.view.addConstraint(height); 

     let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {(alert: UIAlertAction!) in print("cancel")}) 
     alertController.addAction(ExitAction) 
     alertController.addAction(cancelAction) 
     DispatchQueue.main.async { 
      self.present(alertController, animated: true, completion:{}) 
     } 

    } 

方法2嵌入式的观点高度 - 作为一个视图中添加的是你可以添加滚动视图任何你喜欢的,如果你的选项,则成功更不是像10,30或数量大制作使用方法1,并给予适当的高度,然后在嵌入式视图中添加滚动视图包含多个按钮

你可以添加高达10个选项,如果你修改actionSheet使用方法1

+0

这是最好的。谢谢先生 ! – StevenTan

+0

欢迎,高兴帮助:) –

+0

顺便说一下,我有几个问题,如果我的选择是超过3种类型,然后按钮将填满警告页面,页面可以向上/向下滚动? – StevenTan