[objective-c]从 Plist 设置 UILabel 文本吗?

发布时间: 2017/3/27 2:48:10
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我成功地得到了我的搭档数组和字典加载到我的表。 我试图将 exerciseName 值设置为 UILabel 在我练习的详细信息视图。

<array>
<dict>
    <key>exercises</key>
    <array>
        <dict>
            <key>exerciseDetail</key>
            <string></string>
            <key>exerciseName</key>
            <string>Ab Crunch Machine</string>
        </dict>
        <dict>
            <key>exerciseDetail</key>
            <string></string>
            <key>exerciseName</key>
            <string>Ab Roller</string>
        </dict>
    </array>
    <key>muscleName</key>
    <string>Abdominals</string>
</dict>

运动细节现在是空白,但最终将在同样的练习的详细信息视图中加载 UILabel 或 UITextView。

我猜想我需要做一些事情

label.text =[[self.exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exercises"];

编辑︰

我有我的 exerciseViewController didSelectIndexAtRow:

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:exerciseArray forKey:@"exercises"];
    [def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"exercises"];

和我有的 detailViewController ViewDidLoad: viewDidLoad exerciseDetailView

 self.navigationItem.title = @"Exercise Detail"; 

NSMutableArray *exerciseDetailArray = [[[NSUserDefaults alloc] objectForKey:@"exercises"] mutableCopy];

int indexValue = [[[NSUserDefaults alloc] valueForKey:@"exercises"] intValue];

name.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exerciseName"];

控制台输出︰

2011-03-19 22:52:08.279 Curl[32300:207] exercisedetalarray: (
    {
    exerciseDetail = "";
    exerciseName = "Ab Crunch Machine";
},
    {
    exerciseDetail = "";
    exerciseName = "Ab Roller";
},
    {
    exerciseDetail = "";
    exerciseName = "Advanced Kettlebell Windmill";
},
    {
    exerciseName = "Air Bike";
},
    {
    exerciseName = "Alternate Heel Touchers";
},
    {
    exerciseName = "Barbell Ab Rollout";
},
    {
    exerciseName = "Barbell Side Bend";
},
    {
    exerciseName = "Bent Press";
},
    {
    exerciseName = "Bent-Knee Hip Raise";
},
    {
    exerciseName = "Butt-Ups";
},
    {
    exerciseName = "Cable Crunch";
}

)

2011-03-19 22:52:08.280 Curl[32300:207] index value: 0

用 MaserView NSDefault 代码进行编辑︰

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:exerciseArray forKey:@"InfoArray"];
[def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"indexVal"];

用 detailView NSUserDefaults 代码进行编辑︰

-(void)viewWillAppear:(BOOL)animated
{    
    NSMutableArray *exerciseDetailArray = [[[NSUserDefaults alloc] objectForKey:@"InfoArray"] mutableCopy];

    int indexValue = [[[NSUserDefaults alloc] valueForKey:@"indexVal"] intValue];

    name.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exerciseName"];
}

解决方法 1:

@Faisal:

看你的代码我假设您的标签受到你的表,因为您正在使用 indexPath.row 为对数组进行索引

你的想法看起来正确。

步骤 1:

从搭档到你 (Plist 网站-> 数组) 的数组检索数据

让我们说是否你搭档的名字是 Exercise.plist ,那么你可以使用下面的代码来检索您的数据到 plist

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Exercise" ofType:@"plist"];
exerciseArray = [NSArray arrayWithContentsOfFile:plistPath];

步骤 2:

从数组获取数据,到标签的文本 (数组网站-> 标签文本

然后设置 UILabel 的文本从使用下面代码的数组值

label.text =[[exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exercises"];

注︰

对于使用同一数组你马西德威和 detailView 之间,您可以设置您在 NSUserDefaults 中的阵列上 didSelectRowAtIndexPath 在你的马西德威和检索相同阵列在 detailView。

您可以使用以下代码︰

在马西德威对 didSelectRowAtIndexPath 方法︰

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:exerciseArray forKey:@"InfoArray"];
[def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"indexVal"];

您可以检索该数组在 detailView 使用以下代码︰

NSMutableArray *exerciseDetailArray = [[[NSUserDefaults standardUserDefaults] objectForKey:@"InfoArray"] mutableCopy];

int indexValue = [[[NSUserDefaults standardUserDefaults] valueForKey:@"indexVal"] intValue];

label.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exercises"]; 

NSArray *exerciseDetailArray = [[NSArray alloc] initWithContentsOfArray:[[NSUserDefaults alloc] objectForKey:@"InfoArray"]];

int indexValue = [[[NSUserDefaults standardUserDefaults] valueForKey:@"indexVal"] intValue];

label.text =[[exerciseDetailArray objectAtIndex:indexValue] objectForKey:@"exercises"]; 

希望这有助于您:)

最后编辑︰

取代你 didSelectRowAtIndexPath: 以下方法中的方法SpecificExerciseTableViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.

     DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
     detailViewController.exerciseArray = [[self.exerciseArray objectAtIndex:indexPath.row]objectForKey:@"exercises"];
     // Pass the selected object to the new view controller.


    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:exerciseArray forKey:@"InfoArray"];
    [def setValue:[NSString stringWithFormat:@"%d",indexPath.row] forKey:@"indexVal"];
    NSLog(@"Index inexpath:%d",indexPath.row);
    int indexValue = [[[NSUserDefaults standardUserDefaults] valueForKey:@"indexVal"] intValue];
    NSLog(@"index value master view: %d",indexValue);

    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];

}

这肯定会工作:)

赞助商