2017-07-12 44 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,因爲這些是操作系統級別的UI元素(如觸摸ID,應用程序權限請求等)。如果您希望具有更多可定製內容的Alert視圖,您將不得不自行構建。我會發佈一個答案,以您需要的方式實現這個程序化的基礎(相信我,以故事板的形式編程自定義彈出視圖更容易)。最終你會想要創建這樣的東西:https://raw.githubusercontent.com/dogo/SCLAlertView/master/ScreenShots/ScreenShot2.png – murphguy

+0

我在顯示同一行上的多個項目時遇到問題,操作表看起來像它只支持每行一個動作,但我想要兩個!非常感謝! – Chris

回答

0

不幸的是,你需要從頭開始構建控制器。雖然看起來令人生畏,但最難的部分是呈現邏輯。之後它只是將這個主類用於每個ui用例的子類,併爲這些子類中的每個子類創建自己的邏輯。

這裏有一個自定義AlertControllers您的最佳方法: https://gist.github.com/murphman300/4a56ace35d0ccdbf3a0c923b4ec7dc96

您可以使用一個主類像在這個要點文件在這裏,要注意各個部分的意見。

然後,當你繼承每種類型

//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. 在設置覆蓋中設置彈出視圖的初始位置。
  2. set()覆蓋:總是記住你要添加子視圖 popUp視圖,而不是view。當然,您可以將彈出窗口重命名爲 。
  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的alpha設置爲0,這是針對您的用例,它是一箇中心屏幕淡入效果。當然,如果你想讓彈出窗口滑入,你可以在set()覆蓋中更改該alpha值。如果您還想在動畫中滑動幻燈片,則只需在set()覆蓋中設置彈出窗口的初始位置,然後將其設置爲在viewDidAppear覆蓋中出現的位置進行動畫處理。因此,從這個方面來看,這種方法確實需要你跟蹤你如何改變你的子類中的主類的屬性,但是,當你掌握它的時候,它的可定製性是非常整潔的。

從這裏,你可以做任何你想做的事情。只需記住在選擇操作時處理彈出窗口的UI過渡,以確保過渡順暢等。

至於你想要的外觀。我認爲你最好的選擇是使用帶有全長邊框的UITableViewUICollectionView。您應該委派您在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 ,但是我希望每行都有兩個動作,一個會更改UI,另一個允許您編輯行的名稱,因爲每一行的名稱都只是您要導航的護牙的名稱。 – Chris

相關問題