Swift - Displaying Actionsheet For Ipad As Well As Iphone

Swift - Displaying Actionsheet for Ipad as well as Iphone

Influenza A virus subtype H5N1 uncomplicated tutorial on making an alarm persuasion of type activity canvass that industrial plant for iphone as well as ipad.



1.) First you'll ask a push where you lot tin attach your popular over when you're using iPad (in iPhone it non necessary). Then you'll accept to practise an outlet for the push (in my example i named it "btn_button") thus nosotros tin decide the popular over location based on where the push is placed within the viewcontroller.

2.) Inside the button's action, glue the next lines of code:

 allow optionMenu = UIAlertController(title: "Choose Your Option", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)                  allow option1 = UIAlertAction(title: "Option 1", style: .Default, handler: {                          (alert: UIAlertAction!) -> Void inwards             println("Option 1")         })                  allow option2 = UIAlertAction(title: "Option ", style: .Default, handler: {                          (alert: UIAlertAction!) -> Void inwards             println("Option 2")         })                  allow cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {             (alert: UIAlertAction!) -> Void inwards             println("Cancelled")         })                  optionMenu.addAction(option1)         optionMenu.addAction(option2)         optionMenu.addAction(cancelAction)                           if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad )         {             if allow currentPopoverpresentioncontroller = optionMenu.popoverPresentationController{                 currentPopoverpresentioncontroller.sourceView = btn_button                 currentPopoverpresentioncontroller.sourceRect = btn_button.bounds;                 currentPopoverpresentioncontroller.permittedArrowDirections = UIPopoverArrowDirection.Up;                 self.presentViewController(optionMenu, animated: true, completion: nil)             }         }else{             self.presentViewController(optionMenu, animated: true, completion: nil)         }                 
The code is pretty forthwith forward:
-First nosotros declare a UIAlertController where nosotros tin position our buttons.
-Second nosotros created the buttons (Option1, Option2, as well as Cancel) as well as and thus nosotros add together the buttons within the alarm controller.
-Last nosotros operate the UI_USER_INTERFACE_IDIOM to decide if the device/emulator is an ipad or an iphone as well as practise the necessary presentation for each item device.
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.