2010-04-14 77 views
0

我有以下代码:从UIActionSheet代表访问视图控制器的变量

@implementation SendMapViewController 

NSMutableArray *emails; 

在这个方法我创建电子邮件数组和我添加一些NSString的:

- (BOOL) peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker 
     shouldContinueAfterSelectingPerson: (ABRecordRef)person { 

    ABMultiValueRef emailInfo = ABRecordCopyValue(person, kABPersonEmailProperty); 

    NSUInteger emailCount = ABMultiValueGetCount(emailInfo); 

    if (emailCount > 1) { 

     UIActionSheet *emailsAlert = [[UIActionSheet alloc] 
             initWithTitle:@"Select an email" 
             delegate:self 
             cancelButtonTitle:nil 
             destructiveButtonTitle:nil 
             otherButtonTitles:nil]; 

     emails = [NSMutableArray arrayWithCapacity: emailCount]; 

     for (NSUInteger i = 0; i < emailCount; i++) { 
      NSString *emailFromContact = (NSString *)ABMultiValueCopyValueAtIndex(emailInfo, i); 

      [emails addObject: emailFromContact]; 

      [emailsAlert addButtonWithTitle:emailFromContact]; 

      [emailFromContact release]; 
     } 

     [emailsAlert addButtonWithTitle:@"Cancel"]; 

     [emailsAlert showInView:self.view]; 

     [emailsAlert release]; 
    } 
    else { 
     ... 
    } 

    CFRelease(emailInfo); 

    [self dismissModalViewControllerAnimated:YES]; 

    return NO; 
} 

正如你可以在代码中看到,如果我有多个电子邮件和UIActionSheet。当用户点击代表一个按钮和电子邮件,我想执行以下代码:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if ([emails count] >= buttonIndex) { 
     NSString *contactEmail = (NSString *)[emails objectAtIndex:buttonIndex]; 

     ... 
    } 
} 

但电子邮件阵还没有得到任何电子邮件。我做错了什么?

我正在为iPhone开发。

回答

0

您的emails对象可能在您未查找时自动释放。将行:

emails = [NSMutableArray arrayWithCapacity: emailCount]; 

有:

[emails release]; 
emails = [[NSMutableArray alloc] initWithCapacity:emailCount]; 

使emails不会自动释放。 (记住,你自己通过init返回任何东西,但对象通过方便的构造返回像arrayWithCapacity:前来自动释放。)

最好的解决办法是声明一个属性:

@property (retain) NSMutableArray* emails; 

,然后使用:

[self setEmails:[NSMutableArray arrayWithCapacity: emailCount]]; 

第二种使用属性的方法确实是最好的,因为它更灵活e并且清楚。这样,酒店的访问者(您使用@synthesize创建的)将为您处理呼叫retain

+0

我认为这是与电子邮件变量相关的事情,它在另一个执行线程中声明(如果您想从另一个不是已创建的执行线程更新接口,则与.NET一样)。 – VansFannel 2010-04-16 10:30:37

相关问题