应用程序崩溃。 :(

时间:2010-12-08 03:48:15

标签: iphone objective-c xcode login

我正在创建一个带登录窗口的应用。应用程序构建成功但是只要我触摸登录按钮,应用程序就会崩溃。 :(

我已粘贴下面的.m文件。

我在哪里弄错了? :(

提前致谢:)

#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

标题/ .h文件:

#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

3 个答案:

答案 0 :(得分:4)

您的错误在这里:

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

您忘记调用NSString的{​​{1}}方法,您的代码应如下所示:

此外,如果stringWithFormat:不是实例变量,那么您还需要通过在alertWithOkButton前添加前缀来声明类型。

UIAlertView *

答案 1 :(得分:2)

您已编写错误警告分配的语法

我已经在jacobs中做了一些改变回答试试这个

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

快乐的编码......

答案 2 :(得分:1)

雅各

始终与自前缀一起使用的Synthesize变量。 即

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