2012-03-15 10 views
17

estoy usando guión para una aplicación iOS, mi guión gráfico se ve así: http://d.pr/7yAY (url Droplr)selector no reconocido enviado a la instancia con guiones gráficos

El problema es cuando hago clic en el botón de inicio de sesión, el nombre de usuario que envío capturado al controlador de vista de tabla de eventos. Para hacer eso uso la función prepareForSegue, pero aparentemente cuando trato de configurar el nombre de usuario arroja una excepción.

Mi código es el siguiente:

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

- (IBAction) logintButton:(id)sender; 
@property (weak, nonatomic) IBOutlet UITextField *username_tf; // textfield 

@end 

ViewController.m

#import "ViewController.h" 
#import "EventsTableViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize username_tf; 


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([[segue identifier] isEqualToString:@"ToMainApp"]) 
    { 
     EventsTableViewController * dest = (EventsTableViewController *)[segue destinationViewController]; 
     NSString * username = [[NSString alloc] initWithFormat:@"%@", username_tf.text]; 
     [dest setUsername:username]; 
    } 
} 


- (IBAction) logintButton:(id)sender 
{ 
    //NSLog(@"Logint button pressed"); 
    [self performSegueWithIdentifier:@"ToMainApp" sender:sender]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [self setUsername_tf:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

EventsTableViewController.h

#import <UIKit/UIKit.h> 

@interface EventsTableViewController : UITableViewController 
{ 
    NSString * username; 
} 

@property (nonatomic, retain) NSString * username; 

@end 

EventsTableViewController.m

#import "EventsTableViewController.h" 

@interface EventsTableViewController() 
@end 

@implementation EventsTableViewController 

@synthesize username; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

... 

@end 

La excepción es Throwed:

2012-03-15 14:19:27.304 StoryboardAssistance[30989:f803] 
-[UINavigationController setUsername:]: unrecognized selector sent to instance 0x68abf60 2012-03-15 14:19:27.306 
StoryboardAssistance[30989:f803] *** Terminating app due to uncaught 
exception 'NSInvalidArgumentException', reason: 
'-[UINavigationController setUsername:]: unrecognized selector sent to 
instance 0x68abf60' 
*** First throw call stack: (0x13c9022 0x155acd6 0x13cacbd 0x132fed0 0x132fcb2 0x28e6 0x43e4be 0xdb5ab 0x2974 0x13cae99 0x1614e 0x160e6 
0xbcade 0xbcfa7 0xbc266 0x3b3c0 0x3b5e6 0x21dc4 0x15634 0x12b3ef5 
0x139d195 0x1301ff2 0x13008da 0x12ffd84 0x12ffc9b 0x12b27d8 0x12b288a 
0x13626 0x253d 0x24a5) terminate called throwing an exception(lldb) 

¿Alguna sugerencia?

+0

¿Puedes hacer una depuración paso a paso y averiguar en qué línea se está bloqueando realmente? También 'NSString * username = [[NSString alloc] initWithFormat: @"% @ ", username_tf.text];' puede ser 'NSString * username = username_tf.text'. – Gobot

+0

¿Se puede actualizar la imagen de su guión gráfico? Actualmente está 404ing. Gracias – cwiggo

Respuesta

34

El controlador de vista de destino de su segue es su controlador de navegación, no su controlador de vista de tabla de eventos.

Puede obtener el controlador de eventos accediendo a la propiedad topViewController del controlador de navegación.

Prueba esto:

UINavigationController *navController = (UINavigationController*)[segue destinationViewController]; 
EventsTableViewController *eventsController = [navController topViewController]; 

NSString * username = [[NSString alloc] initWithFormat:@"%@", username_tf.text]; 
[eventsController setUsername:username]; 
+1

Eso fue todo, gracias – JohnnyAce

+0

¡Esto es exactamente lo que estaba buscando! – josh123a123

16

Alternativamente, intente lo siguiente: -

(Se hace lo mismo que la respuesta de jonkroll pero en una línea y elimina el aviso de "tipos de puntero incompatible inicializar 'ViewController * __ fuerte' con una expresión de tipo UIViewController * "

NameOfViewController *vc = (NameOfViewController *)[[segue destinationViewController] topViewController]; 
+1

Excelente solución de una línea. ¡Trabajó para mi! – Groot

4

Otra cosa a comprobar es que se ha asignado correctamente a su destino ViewCo ntroller como la clase adecuada en Interface Builder. Por defecto será UIViewController o UITableViewController etc. Si tiene una clase personalizada con getters/setters, debe recordar cambiar la clase en el Interface Builder para reflejarla en consecuencia; de lo contrario, recibirá un mensaje de error de selector no válido.

+0

Sí +1, gracias. – PeterPurple

+0

Me siento estúpido porque olvidé hacer esto, gracias por recordarme. –

Cuestiones relacionadas