iPhone - 如何正确更改标签中的字符串?
我目前正在学习在 iPhone 上编程。
我目前有一个带有模拟键盘的小型测试应用程序,因此我有一些按钮,按下这些按钮后应在标签中放置一些文本。
然后,我获取标签中的内容并使用它打开 ABUNKnownPersonControllerView 以便在那里使用。只是一个基本的应用程序,用于习惯 iPhone 中的一些功能。
我遇到麻烦的地方似乎是使用用于在标签中显示我想要的内容的字符串。我收到 EXC_BAD_ACCESS 错误,因此我重新阅读了内存管理文档,但我找不到任何应该存在内存泄漏的地方。
所以我认为我的问题是我错误地使用了字符串。我的代码如下:
//头文件
@interface KeyPadViewController : UIViewController <ABUnknownPersonViewControllerDelegate>{
IBOutlet UILabel *phoneNumber;
//NSString *number;
}
@property (nonatomic, retain) UILabel *phoneNumber;
//@property (nonatomic, retain) NSString *number;
-(IBAction)addNum1;
-(IBAction)addNum2;
-(IBAction)showUnknownPersonViewController;
//实现文件
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(@"Keypad",@"Keypad");
//self.number = @"";
self.phoneNumber.text = @"";
}
-(IBAction)addNum1{
//self.number = [self.number stringByAppendingString:@"1"];
self.phoneNumber.text = [self.phoneNumber.text stringByAppendingString:@"1"];
}
-(IBAction)addNum2{
//self.number = [self.number stringByAppendingString:@"2"];
self.phoneNumber.text = [self.phoneNumber.text stringByAppendingString:@"2"];
}
以及实现文件中的showUnknownPerson视图:
-(void)showUnknownPersonViewController
{
//NSString *contactNum = self.phoneNumber.text;
ABRecordRef aContact = ABPersonCreate();
CFErrorRef anError = NULL;
ABMultiValueRef email = ABMultiValueCreateMutable(kABPersonPhoneProperty);
bool didAdd = ABMultiValueAddValueAndLabel(email, self.phoneNumber.text, kABPersonPhoneMobileLabel, NULL);
if (didAdd == YES)
{
ABRecordSetValue(aContact, kABPersonPhoneProperty, email, &anError);
if (anError == NULL)
{
ABUnknownPersonViewController *picker = [[ABUnknownPersonViewController alloc] init];
picker.unknownPersonViewDelegate = self;
picker.displayedPerson = aContact;
picker.allowsAddingToAddressBook = YES;
picker.allowsActions = YES;
[self.navigationController pushViewController:picker animated:YES];
[picker release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Could not create unknown user"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
CFRelease(email);
CFRelease(aContact);
}
以及dealloc:
- (void)dealloc
{
[phoneNumber release]; //@synthesize
//[numberrelease]; //@合成 [超级释放]; 我尝试过使用可变字符串
,但遇到了同样的错误,我也尝试过不释放 UILabel 和 NSString,但也遇到了同样的问题。
当我的应用程序加载时,如果我按一个按钮并且标签中出现一个字符,如果我然后使用 showUnknowPersonViewController 一切正常,如果我按按钮 1,然后按按钮 2,这样我就知道 UILabel 中有两个字符(“12 ") 并选择使用 showUnknownPersonViewcontroller 然后我崩溃了。
*** -[CFString release]: message sent to deallocated instance 0x1226a0
但我不明白它可以在哪里被释放?
这可能是我在教程中错过的基本内容,但我无法弄清楚,有人能看到我可能做错了什么吗?
编辑:只是添加,如果 self.phoneNumber.text (UILabel) 返回空或一个字符,它工作正常,但如果它是 2 个或更多,我就会崩溃。
编辑2:我已经从代码中完全删除了 NSString,但仍然遇到相同的问题,所以看起来是 UILabel 导致了问题?请参阅上面更改的代码。
I am currently learning to program on iPhone.
I currently have a small test app with a mock up keypad, so I have a few buttons that when pressed should place some text in a label.
I then take whats in the label and use it to open an ABUNKnownPersonControllerView for use there. Just a basic app to get used to a few things in the iPhone.
Where I am having trouble seems to be with working with the string used to display what I want in the label. I get the EXC_BAD_ACCESS error so I've re-read the memory management docs but I cant find anywhere that there should be a memory leak.
So I think my problem is I am incorrectly working with the String. My code is as follows:
//Header file
@interface KeyPadViewController : UIViewController <ABUnknownPersonViewControllerDelegate>{
IBOutlet UILabel *phoneNumber;
//NSString *number;
}
@property (nonatomic, retain) UILabel *phoneNumber;
//@property (nonatomic, retain) NSString *number;
-(IBAction)addNum1;
-(IBAction)addNum2;
-(IBAction)showUnknownPersonViewController;
//Implementation file
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(@"Keypad",@"Keypad");
//self.number = @"";
self.phoneNumber.text = @"";
}
-(IBAction)addNum1{
//self.number = [self.number stringByAppendingString:@"1"];
self.phoneNumber.text = [self.phoneNumber.text stringByAppendingString:@"1"];
}
-(IBAction)addNum2{
//self.number = [self.number stringByAppendingString:@"2"];
self.phoneNumber.text = [self.phoneNumber.text stringByAppendingString:@"2"];
}
And the showUnknownPerson view in the implementation file:
-(void)showUnknownPersonViewController
{
//NSString *contactNum = self.phoneNumber.text;
ABRecordRef aContact = ABPersonCreate();
CFErrorRef anError = NULL;
ABMultiValueRef email = ABMultiValueCreateMutable(kABPersonPhoneProperty);
bool didAdd = ABMultiValueAddValueAndLabel(email, self.phoneNumber.text, kABPersonPhoneMobileLabel, NULL);
if (didAdd == YES)
{
ABRecordSetValue(aContact, kABPersonPhoneProperty, email, &anError);
if (anError == NULL)
{
ABUnknownPersonViewController *picker = [[ABUnknownPersonViewController alloc] init];
picker.unknownPersonViewDelegate = self;
picker.displayedPerson = aContact;
picker.allowsAddingToAddressBook = YES;
picker.allowsActions = YES;
[self.navigationController pushViewController:picker animated:YES];
[picker release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Could not create unknown user"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
CFRelease(email);
CFRelease(aContact);
}
And the dealloc:
- (void)dealloc
{
[phoneNumber release]; //@synthesize
// [number release]; //@synthesize
[super dealloc];
}
I have tried using a Mutable string but have the same error, I've also tried not releasing the UILabel and the NSString and have the same issue.
When my app loads if I press one button and one character appears in the label, if I then use the showUnknowPersonViewController everything works fine, have if I press button 1 and then button 2 so that I know have two characters in the UILabel ("12") and select to use the showUnknownPersonViewcontroller then I get the crash.
*** -[CFString release]: message sent to deallocated instance 0x1226a0
However I don't understand where it could be getting deallocated?
This is probably something basic I've missed in the tutorials but I cant figure it out, can anyone see what I might be doing wrong?
EDIT: Just to add if the self.phoneNumber.text (UILabel) returns either empty or one character it works fine however if its 2 or more I get the crash.
EDIT 2: I have removed my NSString from the code completely and I still have the same issue, so it looks like its the UILabel thats causing the issue? See code changed above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为问题可能出在这一行:
您将 self.phoneNumber.text 设置为与数字相同的字符串,而不是副本或新创建的具有相同内容的字符串。因此,在 addNum 方法中,一个属性的 setter 在重置其值时会释放该字符串。第二个属性仍然指向已释放的字符串。当第二个属性的 setter 被调用时,它会尝试再次释放该字符串。因此,出现错误。
如果这是问题所在,可以通过将该行重写为以下方式来解决:
尽管,即使这样也可能没有必要。所有实例变量都初始化为零,并且没有必要为其指定初始值。
I think the problem may be in the line:
You are setting self.phoneNumber.text to the same string as number, not to a copy or to a newly created string with the same content. Thus, in your addNum methods, the setter of one property releases the string when it resets its value. The second property is still pointing to the released string. When the setter of the second property is called, it tries to release that string again. Hence, the error.
If this is the problem, it could be solved by rewriting the line as:
Although, even this is probably not necessary. All instance variables are initiated to nil, and it would not be necessary to give it an initial value.
我猜你的 NSString 变量 number 导致了问题。看来您分配给该变量的值不会在任何地方保留。当您使用self来访问变量phoneNumber时,您也需要使用它来访问number。使用self调用实际保留该属性的特定属性的setter。
I guess your NSString variable number is causing the problem. It seems the values you assign to that variable are not retained anywhere. As you are using self to access the variable phoneNumber, you need use that for accessing the number too. Using self calls the setter for the particular property where the property is actually retained.
我相信该属性可能还需要声明为 IBOutlet,如果是这样,这可能会导致问题。
I believe the property might also need to be declared as an IBOutlet, and, if so, that could be causing the problem.
看来你从来不分配号码。数字的初始值是多少?
在我看来,你应该在 viewDidLoad 中分配一个字符串
It seems you never allocate number. What is the initial value of number?
To my mind, you should allocate a string in your viewDidLoad