2010-12-08 6 views
1

Je crée une application avec fenêtre de connexion. L'application construit avec succès, mais dès que je touche le bouton de connexion, l'application se bloque. :(L'application se bloque. :(

J'ai collé le fichier .m ci-dessous.

Où suis-je faire l'erreur :(

Merci à l'avance :)

#import "AuthViewController.h" 

@implementation AuthViewController 

@synthesize userName; 
@synthesize password; 
@synthesize loginbutton; 
@synthesize indicator; 

- (IBAction) loginButton: (id) sender 
{ 
indicator.hidden = NO; 
[indicator startAnimating]; 
loginbutton.enabled = NO; 

// Create the username and password string. 
// username and password are the username and password to login with 
NSString *postString = [[NSString alloc] initWithFormat:@"username=%@&password=%@",userName, password]; 
// Package the string in an NSData object 
NSData *requestData = [NSData dataWithBytes: [postString UTF8String] length: [postString length]]; 

// Create the URL request 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://localhost/dologin.php"]]; // create the URL request 
[request setHTTPMethod: @"POST"]; // you're sending POST data 
[request setHTTPBody: requestData]; // apply the post data to be sent 

// Call the URL 
NSURLResponse *response; // holds the response from the server 
NSError *error; // holds any errors 
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error]; // call the URL 

/* If the response from the server is a web page, dataReturned will hold the string of the HTML returned. */ 
NSString *dataReturned = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding]; 

alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:@"%@",dataReturned cancelButtonTitle:@"Okay", nil]; 
[alertWithOkButton show]; 
[alertWithOkButton release]; 
} 

-(IBAction)dismissKeyboard: (id)sender { 
[sender resignFirstResponder]; 
} 
/* 
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
    // Custom initialization 
} 
return self; 
} 
*/ 

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

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
// Releases the view if it doesn't have a superview. 
[super didReceiveMemoryWarning]; 

// Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 


- (void)dealloc 
{ 
[super dealloc]; 
} 


@end 

l'en-tête/.h fichier:

#import <UIKit/UIKit.h> 

@interface AuthViewController : UIViewController { 
IBOutlet UITextField *userName; 
IBOutlet UITextField *password; 
IBOutlet UIButton *loginbutton; 
IBOutlet UIActivityIndicatorView *indicator; 
UIAlertView *alertWithOkButton; 
UIAlertView *alertWithYesNoButtons; 
} 
@property (nonatomic, retain) UITextField *userName; 
@property (nonatomic, retain) UITextField *password; 
@property (nonatomic, retain) UIButton *loginbutton; 
@property (nonatomic, retain) UIActivityIndicatorView *indicator; 

- (IBAction)dismissKeyboard: (id)sender; 
- (IBAction) loginButton: (id) sender; 

@end 
+0

Quelle est la Message d'erreur? Où se bloque-t-il? Votre alertWithOkButton est-il déclaré en tant que variable? Sinon, vous devez changer cette ligne de code pour UIAlertView * alertWithOkButton = ... – Rog

+0

Le simulateur fonctionne bien mais dès que je clique sur le bouton de connexion, la barre d'état affiche 'Signal reçu du programme GDB:' SIGABRT '. –

+0

@Aniruddh Oui, cette erreur semble correspondre au problème décrit dans ma réponse. –

Répondre

2

Vous avez écrit la syntaxe de l'allocation d'alerte mauvaise

Je l'ai fait quelques changements dans la réponse jacobs essayer cela

alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:[NSString stringWithFormat:@"%@",dataReturned] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil]; 

...

CODAGE HEUREUX
+0

Cool, cela a fonctionné. : D Merci. :) –

+0

obtenir un vote et d'accepter la réponse est la meilleure façon de remercier: P –

+0

Je ne peux pas voter parce que ma réputation est inférieure à 15, mais j'ai déjà accepté votre réponse. :) –

4

Votre erreur est ici:

alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:@"%@",dataReturned cancelButtonTitle:@"Okay" ]; 

Vous avez oublié d'invoquer la méthode de stringWithFormat:NSString, votre code devrait ressembler à ceci:

De plus, si alertWithOkButton n'est pas une variable d'instance, alors vous besoin de déclarer le type aussi en le préfixant avec UIAlertView *.

alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:[NSString stringWithFormat:@"%@",dataReturned] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil]; 
+0

hey jacob encore manquant [.... cancelButtonTitle: @ "Ok" otherButtonTitles: nul] cancelButtonTitle ne peut pas prendre plus de 1 argument .. donc il va sûrement tomber en panne –

+0

@Suriya merci - fixé.P –

+0

Bienvenue ... obtenir un vote est le meilleur merci ..: P –

1

Jacob

variable Synthétiser toujours utilisé avec préfixe auto. -à-dire

UITextField *username; 
@property(nonatomic, retain) UITextField *username; 
@synthesize username 
self.username.text = [NSString stringWithFormat:@"%@",stringTypeVariable];