alertas alerta ios objective-c iphone uitableview pasteboard

ios - alerta - uialertcontroller swift 4



Una forma simple de mostrar la ventana emergente ''Copiar'' en UITableViewCells como la aplicaciĆ³n de la libreta de direcciones (5)

¿Hay una manera simple para que las subclases de UITableViewCell muestren la ventana emergente ''Copiar'' UIMenuController como en la aplicación de la libreta de direcciones (ver captura de pantalla), después de que la selección se mantiene por un tiempo?


Ahora hay una interfaz oficial para mostrar los menús de la célula UITableView en iOS 5. Ejemplo (desde el delegado de la mesa):

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { return (action == @selector(copy:)); } - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { if (action == @selector(copy:)){ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [[UIPasteboard generalPasteboard] setString:cell.textLabel.text]; } }

Traté de modificar el controlador compartido de UIMenuController para agregar mi propio elemento de menú, y pude agregarlo y obtener el mensaje canPerformAction para él, pero devolver SÍ no me ayudó; No pude hacer aparecer mi elemento de menú personalizado. Según mis experimentos, parece que solo se admiten Copiar, Cortar y Pegar. [ EDITAR Desde que se publicó esta publicación, aprendí cómo agregar elementos de menú personalizados.]

Tenga en cuenta que esto solo funciona si se implementan los tres métodos de delegado.


Aquí está la sintaxis de Swift para copiar detailTextLabel .

override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool { return (tableView.cellForRow(at: indexPath)?.detailTextLabel?.text) != nil } override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool { return action == #selector(copy(_:)) } override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) { if action == #selector(copy(_:)) { let cell = tableView.cellForRow(at: indexPath) let pasteboard = UIPasteboard.general pasteboard.string = cell?.detailTextLabel?.text } }


Su subclase UITableViewCell puede verse así

@interface MenuTableViewCell : UITableViewCell { } - (IBAction)copy:(id)sender; - (void)showMenu; @end @implementation MenuTableViewCell - (BOOL)canBecomeFirstResponder { return YES; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(copy:)) { return YES; } return NO; } - (IBAction)copy:(id)sender { } - (void)showMenu { [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES]; [self becomeFirstResponder]; [[UIMenuController sharedMenuController] update]; [[UIMenuController sharedMenuController] setTargetRect:CGRectZero inView:self]; [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES]; } @end

Y los métodos delegados UITableView son como

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; MenuTableViewCell *cell = (MenuTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[MenuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell. return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { MenuTableViewCell *cell = (MenuTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; [cell showMenu]; }


El método anterior a iOS 5 es obtener la instancia compartida de UIMenuController, establecer el objetivo rect y ver y llamar a -setMenuVisible:animated: Recuerde implementar -canPerformAction:withSender: en su respondedor.

El método después de iOS 5 (anteriormente disponible como característica no documentada) es implementar estos 3 métodos en su fuente de datos (consulte https://developer.apple.com/reference/uikit/uitableviewdelegate#1653389 ).

-(void)tableView:(UITableView*)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender; -(BOOL)tableView:(UITableView*)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender; -(BOOL)tableView:(UITableView*)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath*)indexPath;


#pragma mark - COPY/PASTE Cell Text via Menu - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { return (action == @selector(copy:)); } - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { if (action == @selector(copy:)) { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; [pasteBoard setString:cell.textLabel.text]; } }