2017-07-12 15 views
0

私は、ユーザがホームページから複数の項目をコントロールし、矢印ボタンやメニューを使って切り替えることができるアプリを書いています。しかし、ユーザーがメニューを通じてアイテムの名前を編集できるようにしたいので、ユーザーがメニューに移動すると、各行は行に関連付けられたアイテムの名前を持ちます。ユーザーがアイテムの名前を変更できるようにする警告が表示されるボタンがあります。現在、アイテムを生成するためにアクションシートを使用していますが、行に複数のアイテムを表示する方法が見つかりません。アクションシートを生成するための私のコードは以下の通りです:Swiftのアクションシートの行に複数のボタンを作成する

@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

+0

をので、本質的に、あなたはアイコンを表示する問題を抱えていますか? – murphguy

+0

あなたがしようとしているのであれば、UIAlertControllerにアイコンを追加することはできません。それらはOSレベルのUI要素(Touch ID、アプリ許可要求など)ですからです。よりカスタマイズ可能なコンテンツを持つアラートビューが必要な場合は、独自にビルドする必要があります。私はこれをプログラマチックに達成するために必要な基本的な答えを投稿します(私を信頼して、プログラマチックにストーリーボードでカスタムポップアップビューを作成するほうがずっと簡単です)。最終的には次のようなものを作成したいと思うでしょう:https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot2.png – murphguy

+0

同じ行に複数のアイテムを表示するのに問題があります。行ごとに1つのアクションしかサポートしていませんが、2つが必要です!本当にありがとう! – Chris

答えて

0

残念ながら、あなたがゼロからコントローラを構築する必要があります。それは難しいように見えるかもしれませんが、最も難しい部分はプレゼンテーションロジックです。その後で、このメインクラスのユースユースケースをサブクラス化し、それらのサブクラスのそれぞれにビルドに関する独自のロジックを与えることが問題になります。あなたは、各パートのコメントに注意を払って、ここではこの主旨ファイル内の1つのようにメインクラスを使用 https://gist.github.com/murphman300/4a56ace35d0ccdbf3a0c923b4ec7dc96

はここでカスタムAlertControllersするための最良のアプローチです。あなたは、各タイプ

//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と同じようにビューが開かれます。しかし、それの外観は完全にあなたに依存します。

  1. 設定オーバーライドでのpopUpビューの初期位置を設定します。
  2. set()オーバーライド: popUpビューにサブビューを追加していますが、viewではなく常にサブビューを追加しています。もちろん、popUpの名前を に変更することもできます。
  3. コントローラーのプレゼンテーションを変更するには、メインクラスではなく、サブクラスのオーバーライドとプレゼンテーション変数を変更する必要があります( )。その場合、一般的なものをそのまま使用して素早く再利用できますが、 の特定のユースケースは引き続き区別できます。
  4. 私は、以前のポイントと同じ理由で、サブクラスのレベル で行われている場合viewDidAppearをオーバーライドすることはより有用であることがわかりました。 に別のプレゼンテーションメソッドをプラグインできます。
  5. 解散アニメーションを変更するには、オーバーライドでsuperを呼び出さずに、 メソッド自体をオーバーライドする必要があります。

ポイント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 からdidSelectメソッドを作成することもできます(オプション)。 Here's a good tutorial on delegates

また、popUpのアルファを0に設定しました。これは、センター画面のフェードイン効果の使用例です。もちろん、ポップアップをスライドインしたい場合は、set()オーバーライドでそのアルファ値を変更することができます。アニメーションのスライドが必要な場合は、set()のオーバーライドでpopUpの初期位置を設定し、viewDidAppearオーバーライドに表示する場所でアニメーションを作成するだけです。したがって、このアプローチでは、サブクラスでメインクラスのプロパティをどのように変更するかを把握する必要がありますが、そのカスタマイズ性は非常にきれいです。

ここから、あなたが望むものは何でもできます。アクションが選択されたときにポップアップのUIトランジションを処理することを覚えておいてください。

あなたの望み通りです。あなたの最善の方法は、UITableViewと全角の罫線、またはUICollectionViewを使用することです。 PopUpAlertControllerで決定したメソッドを委譲し、選択メソッドをそれぞれdidSelectItemAtメソッドで解釈し、PopUpAlertControllerDelegatepopUp(controller: PopUpAlertController, didDismiss withInfo: Any?)を呼び出します。これにより、各行にカスタムアイコンを設定することができます。

ここだUICollectionViewの良いチュートリアルでは、プログラムで行われています: https://www.youtube.com/watch?v=3Xv1mJvwXok&list=PL0dzCUj1L5JGKdVUtA5xds1zcyzsz7HLj

+0

アクションシートをプログラムで作成してカスタマイズする方法はありますか?可能であれば、このビデオで作成したアクションシートに似たものを作りたいと思っています。https://www.youtube.com/watch?v=wLjxX7PAF6E&t=614sしかし、各行には2つのアクションが必要です.1つはUIを変更し、1つは行の名前を編集できるようにします。各行の名前は、あなたがナビゲートするマウスガードの名前です。 – Chris

関連する問題