我正在编写一个应用程序,用户可以从主页控制多个项目,并可以使用箭头按钮或菜单在它们之间切换。但是,我希望能够让用户通过菜单编辑项目的名称,这样当用户导航到菜单时,每行都有与该行相关联的项目的名称,并且在右侧它有一个按钮,可以提取一个警报,让用户更改项目的名称。目前我正在使用操作表生成项目,但我找不到在行上显示多个项目的方法。我生成活动表的代码如下:
@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)
}
答案 0 :(得分:0)
不幸的是,您需要从头开始构建控制器。虽然看起来令人生畏,但最难的部分是表示逻辑。之后,只需要对这个主类per-ui用例进行子类化,并为每个子类提供自己的构建逻辑。
这是自定义AlertControllers的最佳方法: 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.
}
}
因此,这将以与UIAlertControllers相同的方式弹出视图。但是,它的外观完全取决于你。使用这种类时需要记住的一些事项:
set()
覆盖:始终记住您正在添加子视图
popUp
视图,而不是view
。当然,您可以将popUp重命名为
无论你想要什么.. viewDidAppear
会更有用
子类级别,原因与前一点相同。您
可以在那里插入单独的演示方法。第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
中的didSelect方法作为可选方法。 Here's a good tutorial on delegates
我还将popUp的alpha设置为0,这是针对您的用例,这是一个中心屏幕淡入效果。当然,如果您希望弹出窗口滑入,则可以在set()
覆盖中更改该Alpha值。如果您还想要动画幻灯片,则只需在set()
覆盖中设置popUp的初始位置,并将其设置为您希望它显示在viewDidAppear
覆盖中的位置。因此,从这个方法来看,这种方法确实需要您跟踪如何更改主要类别'但是,当你掌握它的时候,你的子类中的属性可以自定义。
从这里开始,你几乎可以随心所欲。只需记住在选择动作时处理弹出窗口的ui转换,以确保转换顺利等。
至于你想要的样子。我认为您最好的方法是使用带有全长边框的UITableView
或UICollectionView
。您应该委托您在PopUpAlertController中决定使用哪一个,使用各自的didSelectItemAt
方法解释选择方法并调用PopUpAlertControllerDelegate
' s popUp(controller: PopUpAlertController, didDismiss withInfo: Any?)
。这样就可以为每一行设置自定义图标。
这是一个关于UICollectionView以编程方式完成的好教程: https://www.youtube.com/watch?v=3Xv1mJvwXok&list=PL0dzCUj1L5JGKdVUtA5xds1zcyzsz7HLj