ventanas ventana modales modal hacer formulario ejemplos desde con como bootstrap actualizar abrir objective-c xcode uinavigationcontroller modalviewcontroller

objective c - ventana - Descartar controlador de hoja de formulario de vista modal en tap exterior



formulario modal php (13)

Swift 3

class ModalParentViewController: UIViewController, UIGestureRecognizerDelegate { private var tapOutsideRecognizer: UITapGestureRecognizer! override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) if(self.tapOutsideRecognizer == nil) { self.tapOutsideRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTapBehind)) self.tapOutsideRecognizer.numberOfTapsRequired = 1 self.tapOutsideRecognizer.cancelsTouchesInView = false self.tapOutsideRecognizer.delegate = self appDelegate.window?.addGestureRecognizer(self.tapOutsideRecognizer) } } override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) if(self.tapOutsideRecognizer != nil) { appDelegate.window?.removeGestureRecognizer(self.tapOutsideRecognizer) self.tapOutsideRecognizer = nil } } func close(sender: AnyObject) { self.dismiss(animated: true, completion: nil) } // MARK: - Gesture methods to dismiss this with tap outside func handleTapBehind(sender: UITapGestureRecognizer) { if (sender.state == UIGestureRecognizerState.ended) { let location: CGPoint = sender.location(in: nil) if (!self.view.point(inside: self.view.convert(location, from: self.view.window), with: nil)) { self.view.window?.removeGestureRecognizer(sender) self.close(sender: sender) } } } func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true }

}

Presento un controlador de vista modal como una hoja de formulario y lo descarto cuando se hace clic en el botón de cancelación, que es un elemento del botón de barra. Necesito descartarlo cuando hago tapping fuera de esa vista. Por favor ayúdame con una referencia. Nota: mi controlador de vista modal se presenta con un controlador de navegación.

@cli_hlt, @Bill Brasky gracias por tu respuesta. Necesito descartarlo cuando el toque se produce fuera de la vista modal, que es una hoja de formulario. Estoy pegando mi código a continuación.

-(void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index { if(adminMode) { CHEditEmployeeViewController *editVC = [[CHEditEmployeeViewController alloc] initWithNibName:@"CHEditEmployeeViewController" bundle:nil]; editVC.delegate = self; editVC.pickedEmployee = employee; editVC.edit = TRUE; editVC.delegate = self; UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:editVC]; navigationController.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:navigationController animated:YES]; return; } //the above code is from the view controller which presents the modal view. Please look at the below code too which is from my modal view controller. Please guide me in a proper way. -(void)tapGestureRecognizer { UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)]; [recognizer setNumberOfTapsRequired:1]; recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view [self.view addGestureRecognizer:recognizer]; } - (void)handleTapBehind:(UITapGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window //Then we convert the tap''s location into the local view''s coordinate system, and test to see if it''s in or outside. If outside, dismiss the view. if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) { [self dismissModalViewControllerAnimated:YES]; [self.view.window removeGestureRecognizer:sender]; } } }


Ah ok Entonces, me temo que no es posible usar el método presentModalViewController :. Toda la idea de una vista / ventana / cuadro de mensaje "modal" / etc. pp. es que el usuario no puede hacer nada más que procesar lo que sea la vista / ventana / cuadro de mensaje / etc. pp. quiere que él / ella haga.

Lo que desea hacer en su lugar no es presentar un controlador de vista modal, sino cargar y mostrar su controlador de vista de formulario de manera regular. Tenga en cuenta en su controlador maestro que el formulario solo se muestra, por ejemplo, con una variable BOOL y luego maneje allí los toques que puedan ocurrir. Si su formulario se está mostrando, deséchelo.


Aquí está mi versión que funciona para iOS 7 y iOS 8 y no requiere intercambio condicional de coordenadas:

- (void)handleTapBehind:(UITapGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { CGPoint location = [sender locationInView:self.view]; if (![self.view pointInside:location withEvent:nil]) { [self.view.window removeGestureRecognizer:self.recognizer]; [self dismissViewControllerAnimated:YES completion:nil]; } } }


Basado en la respuesta de Bart van Kuik y NavAutoDismiss y otros grandes fragmentos aquí.

class DismissableNavigationController: UINavigationController, UIGestureRecognizerDelegate { private var tapOutsideRecognizer: UITapGestureRecognizer! override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) if tapOutsideRecognizer == nil { tapOutsideRecognizer = UITapGestureRecognizer(target: self, action: #selector(DismissableNavigationController.handleTapBehind)) tapOutsideRecognizer.numberOfTapsRequired = 1 tapOutsideRecognizer.cancelsTouchesInView = false tapOutsideRecognizer.delegate = self view.window?.addGestureRecognizer(tapOutsideRecognizer) } } override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) if tapOutsideRecognizer != nil { view.window?.removeGestureRecognizer(tapOutsideRecognizer) tapOutsideRecognizer = nil } } func close(sender: AnyObject) { dismissViewControllerAnimated(true, completion: nil) } func handleTapBehind(sender: UITapGestureRecognizer) { if sender.state == UIGestureRecognizerState.Ended { var location: CGPoint = sender.locationInView(nil) if UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) { location = CGPoint(x: location.y, y: location.x) } if !view.pointInside(view.convertPoint(location, fromView: view.window), withEvent: nil) { view.window?.removeGestureRecognizer(sender) close(sender) } } } func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } }

Uso:

let vc = MyViewController() let nc = DismissableNavigationController(rootViewController: vc) nc.modalPresentationStyle = UIModalPresentationStyle.FormSheet presentViewController(nc, animated: true, completion: nil)


Hice un controlador de navegación que se apaga automáticamente para iPad

NavAutoDismiss

Es exactamente el mismo código que el anterior, trabajando en iOS 8.

Por lo tanto, todos los créditos van a las personas de arriba.

Simplemente lo hice para ser más genérico y más fácil de usar.

Solo necesitas copiar AMBOS archivos a tu proyecto

Importar el archivo de encabezado (.h)

Y use su viewcontroller (que desea mostrar) como rootViewController.

Cómo usarlo:

//Import the .h file #import "NavDismissViewController.h" //Instanciate your view controller (the view you want to present) YourViewController * yourVC = [YourViewController new]; //Instanciate the NavDismissViewController with your view controller as the rootViewController NavDismissViewController *nav = [[NavDismissViewController alloc] initWithRootViewController:yourVC]; //if you want to change the navigationBar translucent behaviour [nav.navigationBar setTranslucent:NO]; //Choose the Modal style nav.modalPresentationStyle=UIModalPresentationFormSheet; //present your controller [self presentViewController:nav animated:YES completion:nil]; //Done


Lo uso de esta forma sin ningún problema, ni en iOS 7.1 ni en iOS 8.3.

- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.view.window addGestureRecognizer:self.tapBehindGesture]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.view.window removeGestureRecognizer:self.tapBehindGesture]; } - (UITapGestureRecognizer*)tapBehindGesture { if (_tapBehindGesture == nil) { _tapBehindGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBehindRecognized:)]; _tapBehindGesture.numberOfTapsRequired = 1; _tapBehindGesture.cancelsTouchesInView = NO; _tapBehindGesture.delegate = self; } return _tapBehindGesture; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }


Para iOS 8, debe implementar UIGestureRecognizer según la respuesta de Martino e intercambiar las coordenadas (x, y) de la ubicación marcada cuando está en orientación horizontal. No estoy seguro si esto se debe a un error de iOS 8.

- (void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // add gesture recognizer to window UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)]; [recognizer setNumberOfTapsRequired:1]; recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view [self.view.window addGestureRecognizer:recognizer]; recognizer.delegate = self; } - (void)handleTapBehind:(UITapGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { // passing nil gives us coordinates in the window CGPoint location = [sender locationInView:nil]; // swap (x,y) on iOS 8 in landscape if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) { location = CGPointMake(location.y, location.x); } } // convert the tap''s location into the local view''s coordinate system, and test to see if it''s in or outside. If outside, dismiss the view. if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) { // remove the recognizer first so it''s view.window is valid [self.view.window removeGestureRecognizer:sender]; [self dismissViewControllerAnimated:YES completion:nil]; } } } #pragma mark - UIGestureRecognizer Delegate - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return YES; }


Por lo que sé, ninguna de las respuestas parece estar funcionando de inmediato en todas las condiciones.

Mi solución (ya sea heredarla o pegarla):

@interface MyViewController () <UIGestureRecognizerDelegate> @property (strong, nonatomic) UITapGestureRecognizer *tapOutsideRecognizer; @end -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (!self.tapOutsideRecognizer) { self.tapOutsideRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)]; self.tapOutsideRecognizer.numberOfTapsRequired = 1; self.tapOutsideRecognizer.cancelsTouchesInView = NO; self.tapOutsideRecognizer.delegate = self; [self.view.window addGestureRecognizer:self.tapOutsideRecognizer]; } } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // to avoid nasty crashes if (self.tapOutsideRecognizer) { [self.view.window removeGestureRecognizer:self.tapOutsideRecognizer]; self.tapOutsideRecognizer = nil; } } #pragma mark - Actions - (IBAction)close:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; } - (void)handleTapBehind:(UITapGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window //Then we convert the tap''s location into the local view''s coordinate system, and test to see if it''s in or outside. If outside, dismiss the view. if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) { // Remove the recognizer first so it''s view.window is valid. [self.view.window removeGestureRecognizer:sender]; [self close:sender]; } } } #pragma mark - Gesture Recognizer // because of iOS8 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }


Resolví el problema de iOS 8 agregando delegado al reconocedor de gestos

[taprecognizer setDelegate:self];

con estas respuestas

#pragma mark - UIGestureRecognizer Delegate - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return YES; }

eso me funciona con iOS 8 GM


Sé que esta es una pregunta antigua, pero esto ES posible, a pesar de lo que dice la respuesta "correcta". Como este fue el primer resultado cuando estaba buscando esto, decidí elaborar:

Así es como lo haces:

Debe agregar una propiedad al Controlador de vista desde donde desea presentar de manera modal, en mi caso "tapBehindGesture".

entonces en viewDidAppear

if(!tapBehindGesture) { tapBehindGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBehindDetected:)]; [tapBehindGesture setNumberOfTapsRequired:1]; [tapBehindGesture setCancelsTouchesInView:NO]; //So the user can still interact with controls in the modal view } [self.view.window addGestureRecognizer:tapBehindGesture];

Y aquí está la implementación para tapBehindDetected

- (void)tapBehindDetected:(UITapGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { //(edited) not working for ios8 above //CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window CGPoint location = [sender locationInView: self.presentingViewController.view]; //Convert tap location into the local view''s coordinate system. If outside, dismiss the view. if (![self.presentedViewController.view pointInside:[self.presentedViewController.view convertPoint:location fromView:self.view.window] withEvent:nil]) { if(self.presentedViewController) { [self dismissViewControllerAnimated:YES completion:nil]; } } } }

Solo recuerde eliminar tapBehindGesture de view.window on viewWillDisappear para evitar disparar handleTapBehind en un objeto no asignado.


Versión Swift 4 que funciona tanto en vertical como en horizontal: no se requiere intercambio de x, y.

class TapBehindModalViewController: UIViewController, UIGestureRecognizerDelegate { private var tapOutsideRecognizer: UITapGestureRecognizer! override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if (self.tapOutsideRecognizer == nil) { self.tapOutsideRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTapBehind)) self.tapOutsideRecognizer.numberOfTapsRequired = 1 self.tapOutsideRecognizer.cancelsTouchesInView = false self.tapOutsideRecognizer.delegate = self self.view.window?.addGestureRecognizer(self.tapOutsideRecognizer) } } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) if(self.tapOutsideRecognizer != nil) { self.view.window?.removeGestureRecognizer(self.tapOutsideRecognizer) self.tapOutsideRecognizer = nil } } func close(sender: AnyObject) { self.dismiss(animated: true, completion: nil) } // MARK: - Gesture methods to dismiss this with tap outside @objc func handleTapBehind(sender: UITapGestureRecognizer) { if (sender.state == UIGestureRecognizerState.ended) { let location: CGPoint = sender.location(in: self.view) if (!self.view.point(inside: location, with: nil)) { self.view.window?.removeGestureRecognizer(sender) self.close(sender: sender) } } } func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true }

}


La respuesta de @ yershuachu , en Swift 2:

class ModalParentViewController: UIViewController, UIGestureRecognizerDelegate { private var tapOutsideRecognizer: UITapGestureRecognizer! override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) if(self.tapOutsideRecognizer == nil) { self.tapOutsideRecognizer = UITapGestureRecognizer(target: self, action: "handleTapBehind:") self.tapOutsideRecognizer.numberOfTapsRequired = 1 self.tapOutsideRecognizer.cancelsTouchesInView = false self.tapOutsideRecognizer.delegate = self self.view.window?.addGestureRecognizer(self.tapOutsideRecognizer) } } override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) if(self.tapOutsideRecognizer != nil) { self.view.window?.removeGestureRecognizer(self.tapOutsideRecognizer) self.tapOutsideRecognizer = nil } } func close(sender: AnyObject) { self.dismissViewControllerAnimated(true, completion: nil) } func handleTapBehind(sender: UITapGestureRecognizer) { if (sender.state == UIGestureRecognizerState.Ended) { let location: CGPoint = sender.locationInView(nil) if (!self.view.pointInside(self.view.convertPoint(location, fromView: self.view.window), withEvent: nil)) { self.view.window?.removeGestureRecognizer(sender) self.close(sender) } } } func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } }


extension CGPoint { mutating func correctOrientation() { let screenSize = UIScreen.mainScreen().bounds switch(UIDevice.currentDevice().orientation) { case .Portrait: break case .PortraitUpsideDown: y = screenSize.height - y break case .LandscapeLeft: swap(&y, &x) y = screenSize.height - y break case .LandscapeRight: swap(&y, &x) x = screenSize.width - x break default: break } } }