2014-10-03 56 views
0

我试图在我的应用程序中创建某种会员卡,基本上当输入正确的密码时,我希望它更改UIImageView的图像,但我无法使其工作,这里是部分代码:使用UIAlertViewStyleSecureTextinput更改UIImage

@synthesize imageView; 

- (IBAction)Stamp:(id)sender 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SECRET CODE" message:@"Please hand your device to the business representative who will stamp your card" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"STAMP", nil]; 
alert.alertViewStyle = UIAlertViewStyleSecureTextInput; 
[alert show]; 

} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if (buttonIndex == alertView.firstOtherButtonIndex) 
{ 
    UITextField *textfield = [alertView textFieldAtIndex:0]; 

    NSString *s1 = @"stamp"; 

    if (textfield.text == s1) 
    { 
     UIImage * Stampit = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"[email protected]" ofType:@"png"]]; 
     [imageView setImage:Stampit]; 

    } 

    } 
} 
+0

使用'testfield.text isEqual:@“stamp”'或'isEqualToString:S1' – 2014-10-03 17:30:15

回答

1

您不应该使用引用相等(==)运算符来比较字符串。相反,使用isEqualToString:方法的NSString像这样:

NSString *s1 = @"stamp"; 
if ([textfield.text isEqualToString:s1]) { 
    ...    

使用==将确定由textfield.text提到的NSString实例和由s1提到的NSString实例是否是同一对象,这意味着这两个变量指向内存中的地址相同。

这是要如何比较字符串 - 相反,你想有个别人物进行比较,确保它们是相同的情况下,等等,这是什么isEqualToString:会为你做。

+0

谢谢!我总是搞砸最简单的事情 – Swifter 2014-10-03 18:41:28