代码之家  ›  专栏  ›  技术社区  ›  user7812593

在Swift中为行动表创建一行多个按钮

  •  0
  • user7812593  · 技术社区  · 8 年前

    @IBAction func tapMGName(_ sender: Any) {
        let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    
        for index in 1...5{
            let addMGSelectAction: UIAlertAction = UIAlertAction(title: "Mouthguard \(index)", style: .default){action -> Void in
                let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let SettingsViewController : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "SettingsViewController") as UIViewController
                self.present(SettingsViewController, animated: false, completion: nil)
            }
            actionSheetController.addAction(addMGSelectAction)
        }
        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .destructive, handler: { (action) -> Void in })
        actionSheetController.addAction(cancelAction)
        self.present(actionSheetController, animated: true, completion: nil)
    
    }
    

    What I want the menu to look like

    1 回复  |  直到 8 年前
        1
  •  0
  •   jlmurph    8 年前

    不幸的是,您需要从头开始构建控制器。虽然这看起来令人畏惧,但最困难的部分是表示逻辑。之后,只需根据ui用例对这个主类进行子类化,并在构建时为每个子类提供自己的逻辑。

    https://gist.github.com/murphman300/4a56ace35d0ccdbf3a0c923b4ec7dc96

    你使用了一个主类,就像这个gist文件中的那个,注意每个部分的注释。

    然后当你对每种类型进行子分类时

    //An example subclass of PopUpAlertController
    class SpecificAlertControllerSubclass : PopUpAlertController {
    
        override func set() {
            super.set() //Good idea to call the super., since you might want to add basic stuff all the time, in that case you would do this in the original set() method, not here in the subclass' set() override.
    
            //Here you add your own content.. that you instantiate in the subclass. The fact set() is being called in viewDidLoad() means you don't need to worry about calling it later on.
    
        }
    
    }
    
    class TestController : UIViewController, PopUpAlertControllerDelegate {
    
    
        var popup : SpecificAlertControllerSubclass?
    
    
        func bringUpPopUp() {
            popup = SpecificAlertControllerSubclass()
            popup?.delegate = self
            present(popup!, animated: true) { 
                print("Popup Presented")
            }
        }
    
    
        /*
         The Delegate methods you will always need to consider when using it.
        */
    
        func popUp(controller: PopUpAlertController, didDismiss withInfo: Any?) {
            if let pop = popup, controller == pop {
                //checks if the delegated controller is popup..
                popup = nil
                let info = withInfo != nil ? String(describing: withInfo!) : "unknown"
                print("Dismissed PopUp, with reason: \(info)")
            }
        }
    
        func popUp(controller: PopUpAlertController, selected item: [String : Any]?) {
            //Here is where you would handle the user selecting one of your options. Dismissing the popup andPresenting another controller. or if you want the popup subclass to handle the logic and the next controller, you don't call this method, but handle it in the subclass object.
        }
    
    }
    

    1. 在“设置覆盖”中设置弹出视图的初始位置。
    2. set() 覆盖:始终记住要将子视图添加到 popUp 查看,而不是 view 你想要什么都行。。
    3. 具体用例仍然可以区分。
    4. 我发现这是最重要的 viewDidAppear 子类级别,原因与上一点相同。你
    5. 要更改解雇动画,您必须覆盖

    第3点和第4点意味着 SpecificAlertControllerSubclass 变成这样:

    class SpecificAlertControllerSubclass : PopUpAlertController {
    
        override func set() {
            super.set() //Good idea to call the super., since you might want to add basic stuff all the time, in that case you would do this in the original set() method, not in the subclasses.
    
            //Here you add your own content..
    
            modalPresentationStyle = //which ever one you want
    
            modalTransitionStyle = //which ever one you want
    
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .allowAnimatedContent, animations: {
                self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
                self.popUp.alpha = 1
            }) { (v) in
    
            }
        }
    
    }
    

    正如第二个委托方法上的注释所解释的那样,在我看来,您需要决定哪个控制器应该处理动作逻辑,这取决于在按下这些动作时是否显示新的控制器,或者您是否只是在切换UI。在后一种情况下,您可以从 PopUpAlertController protocol 可选方法。 Here's a good tutorial on delegates .

    推翻如果你还想在动画中滑动,你只需要在 集合() 覆盖,并在希望其显示在中的位置设置动画

    从这里,你几乎可以做任何你想做的事。只要记住在选择动作时处理弹出窗口的ui过渡,以确保过渡平滑等。

    至于你想要的样子。我认为你最好的方式是使用 UITableView 具有全长边框,或 UICollectionView 。您应该委派您在PopUpAlertController中决定的选项,用它们各自的 didSelectItemAt PopUpAlertControllerDelegate popUp(controller: PopUpAlertController, didDismiss withInfo: Any?) 。这将允许您为每行设置自定义图标。

    这里有一个关于UICollectionView的很好的教程,它是通过编程完成的: https://www.youtube.com/watch?v=3Xv1mJvwXok&list=PL0dzCUj1L5JGKdVUtA5xds1zcyzsz7HLj