我真的把头发拉了出来,这一定是一个简单的问题,但我看不出来。
我只是想从文本字段为变量赋值。
在我的文件中,我有
NSString *currentPass;
@property (nonatomic, retain) NSString *currentPass;
我的档案。
@synthesize currentPass;
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:
(NSInteger)buttonIndex{
if (alertView.tag == AlertPasswordAsk) {
UITextField* theTextField = ((UITextField*)[alertView viewWithTag: 5]);
currentPass = [NSString stringWithFormat:@"%@", theTextField.text];
if ([theTextField isEditing]) {
[theTextField resignFirstResponder];
}
}
}
- (void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"didDismissWithButtonIndex tag=%i", alertView.tag);
if (alertView.tag == AlertPasswordAsk) {
if(buttonIndex == 1){
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
NSString *strPassword = [NSString alloc];
strPassword = [myDefaults stringForKey:@"pass"];
// ######### ERROR OCCURS HERE #########
NSLog(@"currentPass=%@ strPassword=%@", currentPass, strPassword);
if (![currentPass isEqualToString:strPassword]) {
[6337:207] didDismissWithButtonIndex tag=10
Current language: auto; currently objective-c
(gdb) continue
Program received signal: “EXC_BAD_ACCESS”.
(gdb) bt
#0 0x02894903 in objc_msgSend ()
#1 0x00000000 in ?? ()
答案 0 :(得分:6)
您需要保留指定给currentPass
的对象:
self.currentPass = [NSString stringWithFormat:@"%@", theTextField.text];
答案 1 :(得分:1)
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:
(NSInteger)buttonIndex{
if (alertView.tag == AlertPasswordAsk) {
UITextField* theTextField = ((UITextField*)[alertView viewWithTag: 5]);
//assigning to member variable will not retain your object.
// current address is just pointing to auto released object not retaining it.
// currentPass = [NSString stringWithFormat:@"%@", theTextField.text];
// use currentPass as with accessor:
self.currentPass = [NSString stringWithFormat:@"%@", theTextField.text];
if ([theTextField isEditing]) {
[theTextField resignFirstResponder];
}
}
}