比较objective-c中的两个字符串会导致app退出

时间:2011-01-26 23:53:41

标签: iphone objective-c xcode

我试图使用“isEqualToString”比较两个字符串,我似乎无法使其工作。一旦它开始运行if语句,它就会退出应用程序。我很确定这些琴弦实际上是相互平等的,尽管我最好的尝试,我仍然没有想法。任何帮助将不胜感激。

-(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];
    }


}

3 个答案:

答案 0 :(得分:5)

可能你的一个字符串(很确定它是tempPass)是自动释放的

答案 1 :(得分:2)

tempPass在哪里宣布?它是否可能没有被保留足够长的时间以便在if中引用?

答案 2 :(得分:1)

如果您使用此菜单项运行程序:运行 - >使用Performance Tool运行 - >植物大战僵尸 然后乐器将为您追踪问题所做的所有艰苦工作。它将向您显示早期发布的对象以及分配的对象。

可能是tempPass。一旦确定是否是这种情况,最好使tempPass成为具有copy属性的属性,然后使用self.tempPass = name.text而不仅仅是tempPass = name.text。