开发者

Application Crashes. :(

I'm creating an app with login window. The application builds successfully but as soon as I touch the login button, the application crashes. :(

I've pasted the .m file below.

Where am I making the mistake? :(

Thanks in Advance:)

#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 us开发者_如何学运维ername 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

The header/.h File:

#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


Your error is here:

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

You forgot to invoke NSString's stringWithFormat: method, your code should look like this:

Also, if alertWithOkButton is not an instance variable, then you will need to declare the type as well by prefixing it with UIAlertView *.

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


You have written the syntax of alert allocation wrong

I have done some changes in jacobs answer try this out

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

hAPPY cODING...


Jacob

Synthesize variable always used with self prefix. i.e

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜