18

enter image description hereयूआईपीओपीओवर मैं इस तरह के बटन के साथ पॉपओवर कैसे बना सकता हूं?

मुझे आश्चर्य है कि मैं इस तरह के बटन के साथ एक पॉपओवर कैसे बना सकता हूं।

उत्तर: आपके सौंपे वस्तु वर्ग में और

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil 
                  delegate: self 
               cancelButtonTitle: nil 
              destructiveButtonTitle: nil 
               otherButtonTitles: @"Take Photo", 
                    @"Choose Existing Photo", nil]; 

[actionSheet showFromRect: button.frame inView: button.superview animated: YES]; 

कहीं ...

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     // take photo... 
    } 
    else if (buttonIndex == 1) { 
     // choose existing photo... 
    } 
} 

उत्तर

43

यह एक UIActionSheet है। आईफोन पर, यह नीचे से एनिमेट करता है। आईपैड पर यह एक पॉपओवर में दिखाई देता है।

आप एक बटन के प्रेस पर यह कर रहे हैं मान लिया जाये:

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil 
                  delegate: self 
               cancelButtonTitle: nil 
              destructiveButtonTitle: nil 
               otherButtonTitles: @"Take Photo", 
                    @"Choose Existing Photo", nil]; 

[actionSheet showFromRect: button.frame inView: button.superview animated: YES]; 

में iOS8 +, आप नए UIAlertController वर्ग का उपयोग करना चाहिए:

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil 
                      message: nil 
                    preferredStyle: UIAlertControllerStyleActionSheet]; 
[alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    // Handle Take Photo here 
}]]; 
[alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    // Handle Choose Existing Photo here 
}]]; 

alertController.modalPresentationStyle = UIModalPresentationPopover; 

UIPopoverPresentationController * popover = alertController.popoverPresentationController; 
popover.permittedArrowDirections = UIPopoverArrowDirectionUp; 
popover.sourceView = sender; 
popover.sourceRect = sender.bounds; 

[self presentViewController: alertController animated: YES completion: nil]; 

या स्विफ्ट

में
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet) 
alertController.addAction(UIAlertAction(title: "Take Photo", style: .Default, handler: { alertAction in 
    // Handle Take Photo here 
    })) 
alertController.addAction(UIAlertAction(title: "Choose Existing Photo", style: .Default, handler: { alertAction in 
    // Handle Choose Existing Photo 
    })) 
alertController.modalPresentationStyle = .Popover 

let popover = alertController.popoverPresentationController! 
popover.permittedArrowDirections = .Up 
popover.sourceView = sender 
popover.sourceRect = sender.bounds 

presentViewController(alertController, animated: true, completion: nil) 
+0

क्या मैं इसे पॉपओवर व्यू में जोड़ता हूं? – ManOx

+0

नहीं, बस यूआईएक्शनशीट शो में से एक का उपयोग करें ... तरीकों से। उदाहरण के लिए मेरा अद्यतन उत्तर देखें –

+0

ठीक है और 1 और सवाल, मैं बटन पर ईवेंट हैंडलर कैसे सेट अप करूं? – ManOx

2

अन्य प्रतिक्रियाओं के समान, लेकिन तुलना में इसे कार्यान्वित करना बहुत आसान है।

अपनी कक्षा UIActionSheetDelegate का उपयोग करें।

उदाहरण:

@interface ExampleViewController : UIViewController <UIActionSheetDelegate> 

फिर एक बटन दबाया घटना में

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //Get the name of the current pressed button 
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex]; 
if ([buttonTitle isEqualToString:@"Remove"]) { 
    NSLog(@"Remove this actionSheet"); } 
if ([buttonTitle isEqualToString:@"Button 1"]) { 
    NSLog(@"Button 1 pressed"); } 
if ([buttonTitle isEqualToString:@"Button 2"]) { 
    NSLog(@"Button 2 pressed"); } 
if ([buttonTitle isEqualToString:@"Button 3"]) { 
    NSLog(@"Button 3 pressed"); } 
if ([buttonTitle isEqualToString:@"Cancel"]) { 
    NSLog(@"Cancel clicked (anywhere away from it)"); } } 

अब आप अपने ExampleViewController.mm/m को जोड़ने के लिए या इस फोन निम्नलिखित पॉपअप के लिए जहां/जब आप चाहते हैं:

- (IBAction)aButtonPressed:(id)sender { 
    NSString *actionSheetTitle = @"Action Sheet"; // Title 
    NSString *destroyTitle = @"Destroy"; // Button titles 
    NSString *button1 = @"Button 1"; 
    NSString *button2 = @"Button 2"; 
    NSString *button3 = @"Button 3"; 
    NSString *cancelTitle = @"Cancel"; 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:actionSheetTitle 
                                  delegate:self 
                                   cancelButtonTitle:cancelTitle 
                                  destructiveButtonTitle:destroyTitle 
                                  otherButtonTitles:button1, button2, button3, nil]; [actionSheet showInView:self.view]; 
} 

और इस बारे में अधिक जानकारी @: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html

+1

जोड़ने के लिए मत भूलना [actionSheet showFromRect: [(UIButton *) प्रेषक फ्रेम] inView: self.view एनिमेटेड: हाँ]; प्रेषक बटन पर पॉपओवर संलग्न करने के लिए। –