【问题标题】:Access NSMutableDictionary created in one class from another ARC iPhone从另一台 ARC iPhone 访问在一个类中创建的 NSMutableDictionary
【发布时间】:2012-02-27 21:41:54
【问题描述】:

如果用户检查 UITableViewCell 中的某些内容,我会使用此方法检查 NSMutableDictionary *checkDict:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[checkDict objectForKey:[array objectAtIndex:indexPath.row]] isEqualToString:@"Check"]) {
        [checkDict setObject:@"NOCheck" forKey:[array objectAtIndex:indexPath.row]];
        [table reloadData];
    }
    else 
    {
        [checkDict setObject:@"Check" forKey:[array objectAtIndex:indexPath.row]];
        [table reloadData];
    }
}

在这个类中我设置了属性:

@property (nonatomic, strong) NSMutableDictionary *checkDict;

我想用“Check”作为在 checkDict MutableDictionary 中设置的对象的信息填充下一个视图。如何从下一堂课访问这个 MutableDict?如果我实例化包含上述代码的前一个类,我会为 checkDict 得到 null。我正在使用 ARC 和 StoryBoard。除了:

[[NSUserDefaults standardUserDefaults]setObject:checkDict forKey:@"CheckDict"];

【问题讨论】:

    标签: iphone uitableview automatic-ref-counting nsmutabledictionary


    【解决方案1】:

    有多种方式:

    1. 您可以使用create a common live data manager,即a singleton class,它将保存此字典,您可以使用此单例类在任何地方访问它。

    2. 在上述方法中,您 can use App Delegate to store the dictionary(使用 setter/getter)并使用 [[UIApplication sharedAppliation] delegate] 对象访问它。

    3. 您在创建(分配/初始化)视图时是 can pass this dictionary object to the other view 对象。但是对于这种方法,我们应该从我们拥有这个字典的同一个类中创建视图。

    4. 由于您将此字典声明为属性,因此您 can create the reference of this class in the view class 要访问此字典,然后使用类引用访问它。

    谢谢,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多