iPhone在浏览视图后认为文本字段为空(仅在8C148a上)

iPhone在浏览视图后认为文本字段为空(仅在8C148a上),iphone,objective-c,memory,memory-leaks,textfield,Iphone,Objective C,Memory,Memory Leaks,Textfield,嘿,伙计们,我正在开发的应用程序中有一个最奇怪的错误 问题是,我有一个登录屏幕,其中有两个文本字段,我创建,添加为子视图和发布,所有这些都是在viewDidLoad中完成的。 然后,当用户登录并再次注销时,文本字段显示正常,但当调用login方法时,它认为文本字段为空 我也只在iOS 4.2.1(8C148a)上发现了这个bug 此外,日志显示“收到内存警告。级别=1”,有时显示“级别=2”,这可能与此相关,但我不知道 有人能帮我吗?我完全迷路了 我的视图加载: userNameTextFiel

嘿,伙计们,我正在开发的应用程序中有一个最奇怪的错误

问题是,我有一个登录屏幕,其中有两个文本字段,我创建,添加为子视图和发布,所有这些都是在viewDidLoad中完成的。 然后,当用户登录并再次注销时,文本字段显示正常,但当调用login方法时,它认为文本字段为空

我也只在iOS 4.2.1(8C148a)上发现了这个bug

此外,日志显示“收到内存警告。级别=1”,有时显示“级别=2”,这可能与此相关,但我不知道

有人能帮我吗?我完全迷路了

我的视图加载:

userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 85, 280, 30)];
userNameTextField.returnKeyType = UIReturnKeyNext;
userNameTextField.placeholder = @"Användarnamn:";
userNameTextField.layer.cornerRadius = 8;
userNameTextField.alpha = 0.9;
userNameTextField.opaque = YES;
userNameTextField.tag = 0;
userNameTextField.backgroundColor = [UIColor whiteColor];
userNameTextField.textAlignment = UITextAlignmentCenter;
userNameTextField.textAlignment = UIBaselineAdjustmentAlignBaselines;
userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
userNameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
userNameTextField.adjustsFontSizeToFitWidth = FALSE;
[userNameTextField addTarget:self 
                      action:@selector(textFieldDidReturn:) 
            forControlEvents:UIControlEventEditingDidEndOnExit];

passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 119, 280, 30)];
passwordTextField.returnKeyType = UIReturnKeyDone;
passwordTextField.placeholder = @"Lösenord:";
passwordTextField.secureTextEntry = YES;
passwordTextField.layer.cornerRadius = 8;
passwordTextField.alpha = 0.9;
passwordTextField.opaque = NO;
passwordTextField.tag = 1;
passwordTextField.backgroundColor = [UIColor whiteColor];
passwordTextField.textAlignment = UITextAlignmentCenter;
passwordTextField.textAlignment = UIBaselineAdjustmentAlignBaselines;
passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
passwordTextField.adjustsFontSizeToFitWidth = TRUE;
[passwordTextField addTarget:self 
                      action:@selector(textFieldDidReturn:) 
            forControlEvents:UIControlEventEditingDidEndOnExit];

[self.view addSubview:userNameTextField];
[self.view addSubview:passwordTextField];

userNameTextField.delegate = self;
passwordTextField.delegate = self;

[userNameTextField release];
[passwordTextField release];
我还使用以下方法:

-(void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *)event {

[userNameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];

[super touchesBegan:touches withEvent:event ];}

-(IBAction)textFieldDone:(id)sender {
[sender resignFirstResponder];}

-(BOOL)textFieldDidReturn:(UITextField *)textfield {
NSInteger nextTextFieldTag = textfield.tag + 1;
// Find next responding textfield
UIResponder *nextRespondingTextField = [textfield.superview viewWithTag:nextTextFieldTag];
if (nextRespondingTextField) {
    [nextRespondingTextField becomeFirstResponder];
} else {
    [self logInUser];
    [textfield resignFirstResponder];
}
return NO;}

-(IBAction) logInUser {
// Popup alert
if (userNameTextField.text.length == 0 && passwordTextField.text.length == 0) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Vänligen ange användarnamn och lösenord." delegate:nil cancelButtonTitle:@"Stäng." otherButtonTitles:nil];
    [alert show];
    [alert release];
} else if (passwordTextField.text.length == 0) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Vänligen ange lösenord." delegate:nil cancelButtonTitle:@"Stäng." otherButtonTitles:nil];
    [alert show];
    [alert release];
} else if (userNameTextField.text.length == 0) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Vänligen ange användarnamn." delegate:nil cancelButtonTitle:@"Stäng." otherButtonTitles:nil];
    [alert show];
    [alert release];
} else {

    PreschoolAppDelegate *globalAppDelegate = [[UIApplication sharedApplication] delegate];

    // Set global username and password
    globalAppDelegate.globalUserName = @"";
    globalAppDelegate.globalPassword = @"";
    globalAppDelegate.globalUserName = userNameTextField.text;
    globalAppDelegate.globalPassword = passwordTextField.text;




    // Clear variables


    globalAppDelegate.globalFirstname = [[NSMutableString alloc] init];
    globalAppDelegate.globalLastname = [[NSMutableString alloc] init];
    globalAppDelegate.globalChildFirstname = [[NSMutableString alloc] init];
    globalAppDelegate.globalChildSurname = [[NSMutableString alloc] init];
    globalAppDelegate.globalChildFullname = [[NSMutableString alloc] init];
    globalAppDelegate.globalSelectedChild = [[NSMutableString alloc] init];
    globalAppDelegate.globalIdString = [[NSMutableString alloc] init];
    globalAppDelegate.globalChildIdString = [[NSMutableString alloc] init];

    globalAppDelegate.globalSelectedChildId = [[NSString alloc] init];

    globalAppDelegate.globalWipChildList = [[NSMutableArray alloc] init];
    globalAppDelegate.globalChildIdList = [[NSMutableArray alloc] init];
    globalAppDelegate.globalChildList = [[NSMutableArray alloc] init];
    globalAppDelegate.globalWipChildArray = [[NSMutableArray alloc] init];
    globalAppDelegate.tempGlobalAbsenceArray = [[NSMutableArray alloc] init];
    globalAppDelegate.tempGlobalChildArray = [[NSMutableArray alloc] init];
    globalAppDelegate.globalAbsenceDescription = [[NSMutableArray alloc] init];
    globalAppDelegate.globalVacationDescription = [[NSMutableArray alloc] init];
    globalAppDelegate.tempGlobalVacationArray = [[NSMutableArray alloc] init];
    globalAppDelegate.globalVacationWipChildArray = [[NSMutableArray alloc] init];
    globalAppDelegate.globalVacationStartDate = [[NSMutableArray alloc] init];
    globalAppDelegate.globalVacationEndDate = [[NSMutableArray alloc] init];
    globalAppDelegate.globalVacationStartDates = [[NSMutableArray alloc] init];
    globalAppDelegate.globalVacationEndDates = [[NSMutableArray alloc] init];
    //
    // Start the fetch of the data from the rest service
    //

    userClient = [[GetUserListRestClient alloc] init];
    [userClient getUsers:self];

    [pendingLogin startAnimating];

    [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector (checkIfConnFinished:) userInfo:nil repeats:YES];
}}

我认为物业转让中存在的问题如下:

globalAppDelegate.globalFirstname = [[NSMutableString alloc] init];
globalAppDelegate.globalWipChildList = [[NSMutableArray alloc] init];
这样,您创建的字符串和数组就永远不会被释放。这导致了内存警告。好办法是:

NSMutableString *str = [[NSMutableString alloc] init];
globalAppDelegate.globalFirstname = str;
[str release];
或者是一个较短但不是很好的解决方案:

globalAppDelegate.globalWipChildList = [[[NSMutableArray alloc] init] autorelease];

我希望我能帮上忙。

我想你可能想简化你的设计:p我想如果你没有所有这些数组,你就不会丢失文本字段。