2016-03-25 13 views
3

Wie kann ich UIAlertController Alert-Aktionsschaltflächen mit Action-Handlern mit Ziel-c verknüpfen? Ich benutze Xcode 7.1.Wie gehe ich mit den Aktionen eines UIAlertControllers um?

Hier ist mein Code:

- (IBAction)selectbtn:(UIButton *)sender { 

    UIAlertController *alert=[ UIAlertController alertControllerWithTitle:@"NEW" message:@"button pressed" preferredStyle:UIAlertControllerStyleActionSheet]; 
    UIAlertAction *cameraaction=[UIAlertAction actionWithTitle:@"From camera" style:UIAlertActionStyleDefault handler:nil ]; 
    [alert addAction:cameraaction]; 
    UIAlertAction *libraryaction=[UIAlertAction actionWithTitle:@"From photo library" style:UIAlertActionStyleDefault handler:nil ]; 
    [alert addAction:libraryaction]; 
    UIAlertAction *cancelaction=[UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDestructive handler:nil]; 
    [alert addAction:cancelaction]; 
    [self presentViewController:alert animated:YES 
        completion:nil]; 
    } 
+3

Sie müssen die Dokumente lesen. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/#//apple_ref/doc/uid/TP40014538-CH1-SW4 – Dare

+0

Ich lese das bereits. Aber ich möchte Tasten mit Aktion verknüpfen . – ghoshghosh

+0

Dafür ist das da. - actionWithTitle: style: handler: Sie fügen diese Aktionen dem UIAlertController hinzu. – Dare

Antwort

21

Objective-C

UIAlertController funktioniert wie folgt:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"text mssg" preferredStyle:UIAlertControllerStyleAlert]; 
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){ 
    // Ok action example 
}]; 
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"Other" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){ 
    // Other action 
}]; 
[alert addAction:okAction]; 
[alert addAction:otherAction]; 
[self presentViewController:alert animated:YES completion:nil]; 

Ich glaube, Sie das bedeutete.

Swift 3,0/4,0

let myalert = UIAlertController(title: "Titulo mensaje", message: "Mi mensaje.", preferredStyle: UIAlertControllerStyle.alert) 

myalert.addAction(UIAlertAction(title: "Aceptar", style: .default) { (action:UIAlertAction!) in 
     print("Selected") 
    }) 
myalert.addAction(UIAlertAction(title: "Cancelar", style: .cancel) { (action:UIAlertAction!) in 
     print("Cancel") 
    }) 

    self.present(myalert, animated: true) 
3

Sie beliebigen Code hinzufügen, können Sie in der Prozedur des Aktionsmethode möchten, Codebeispiel kann wie folgt sein:

@interface ViewController() <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 
@property (weak, nonatomic) IBOutlet UIImageView *imageView; 
@property (strong, nonatomic) UIAlertController *alertCtrl; 
@property (strong, nonatomic) UIImagePickerController *imagePicker; 
@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self setupAlertCtrl]; 
} 

- (void) setupAlertCtrl 
{ 
    self.alertCtrl = [UIAlertController alertControllerWithTitle:@"Select Image" 
                 message:nil 
                preferredStyle:UIAlertControllerStyleActionSheet]; 
    //Create an action 
    UIAlertAction *camera = [UIAlertAction actionWithTitle:@"From camera" 
                style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction *action) 
                { 
                 [self handleCamera]; 
                }]; 
    UIAlertAction *imageGallery = [UIAlertAction actionWithTitle:@"From Photo Library" 
                style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction *action) 
                { 
                 [self handleImageGallery]; 
                }]; 
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" 
                  style:UIAlertActionStyleCancel 
                 handler:^(UIAlertAction *action) 
            { 
             [self dismissViewControllerAnimated:YES completion:nil]; 
            }]; 


    //Add action to alertCtrl 
    [self.alertCtrl addAction:camera]; 
    [self.alertCtrl addAction:imageGallery]; 
    [self.alertCtrl addAction:cancel]; 


} 

- (IBAction)selectImagePressed:(UIButton *)sender 
{ 
    [self presentViewController:self.alertCtrl animated:YES completion:nil]; 
} 

- (void)handleCamera 
{ 
#if TARGET_IPHONE_SIMULATOR 
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" 
                    message:@"Camera is not available on simulator" 
                  preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" 
               style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction *action) 
               { 
                [self dismissViewControllerAnimated:YES completion:nil]; 
               }]; 

    [alert addAction:ok]; 
    [self presentViewController:alert animated:YES completion:nil]; 
#elif TARGET_OS_IPHONE 
    //Some code for iPhone 
    self.imagePicker = [[UIImagePickerController alloc] init]; 
    self.imagePicker.delegate = self; 
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    [self presentViewController:self.imagePicker animated:YES completion:nil]; 

#endif 
} 

- (void)handleImageGallery 
{ 
    self.imagePicker = [[UIImagePickerController alloc] init]; 
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    self.imagePicker.delegate = self; 
    [self presentViewController:self.imagePicker animated:YES completion:nil]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1); 
    UIImage *img = [[UIImage alloc] initWithData:dataImage]; 
    [self.imageView setImage:img]; 
    [self.imagePicker dismissViewControllerAnimated:YES completion:nil]; 

} 

Ref Link

Verwandte Themen