I am attempting to compare two strings using “isEqualToString” and i cant seem to get it to work. Once it begins to run the if statement it quits the app. I’m quite sure that the strings are in fact equal to each other and despite my best attempts I’m out of ideas. Any help would be appreciated.
-(void)createTextfields{
name = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 140, 25)];
name.borderStyle = UITextBorderStyleRoundedRect;
name.textColor = [UIColor blackColor];
name.placeholder = @"Password";
name.textAlignment = UITextAlignmentCenter;
[self addSubview:name];
entry = [[UITextField alloc] initWithFrame:CGRectMake(170, 0, 140, 25)];
entry.borderStyle = UITextBorderStyleLine;
entry.textColor = [UIColor blueColor];
entry.textAlignment = UITextAlignmentCenter;
[self addSubview:entry];
}
-(void)createSubmitButton{
UIButton *submit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
submit.titleLabel.textAlignment = UITextAlignmentCenter;
[submit setTitle:@"Submit" forState:UIControlStateNormal];
submit.frame = CGRectMake(90, 50, 60, 30);
[submit addTarget:self action:@selector(runSubmit) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:submit];
UIButton *savePass = [UIButton buttonWithType:UIButtonTypeRoundedRect];
savePass.titleLabel.textAlignment = UITextAlignmentCenter;
[savePass setTitle:@"Save" forState:UIControlStateNormal];
savePass.frame = CGRectMake(20, 50, 60, 30);
[savePass addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:savePass];
}
-(void)save{
tempPass = name.text;
name.text = @"";
entry.text = tempPass;
}
-(void)runSubmit{
// password = name.text;
if ([tempPass isEqualToString:name.text]) {
//[viewController displayAlertFromViewControl];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct" message:@"Alert" delegate: self cancelButtonTitle:@"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
If you run your program with this menu item: Run -> Run with Performance Tool -> Zombies
then Instruments will do all the hard work of tracking down the problem for you. It will show you which object was released early and where it was allocated.
It is probably tempPass. Once you determine if that is the case, it would be best to make tempPass a property with a copy attribute and then use self.tempPass=name.text instead of just tempPass=name.text.