Set UILabel Text From Plist?

会有一股神秘感。 提交于 2019-12-23 18:33:47

问题


I successfully got my plist arrays and dictionaries loaded into my tables. I am trying to set the exerciseName value to a UILabel in my exercise's detail view.

<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>

The exercise Detail is blank right now but will eventually load a UILabel or UITextView in the same exercises's detail view.

Im guessing I need to do some thing like

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

EDIT:

For my exerciseViewController didSelectIndexAtRow I have:

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

and for ViewDidLoad for detailViewController i have: viewDidLoad for 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"];

Console Output:

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

Edit with MaserView NSDefault code:

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

Edit with detailView NSUserDefaults code:

-(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:

Looking at your code I suppose that your labels are under your tableView as you are using indexPath.row for indexing the array

Your Idea looks Correct.

STEP-1:

Retrieve Data from plist into your Array (Plist -> Array)

Let us say if your plist name is Exercise.plist, then you can use the following code to retrieve your data into plist

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

STEP-2:

Get Data from Array into label's text (Array -> Label Text)

Then set the UILabel's text from the array values using below code

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

NOTE:

For using the same array between you masterView and detailView, you can set your array in NSUserDefaults on didSelectRowAtIndexPath in your masterView and retrieve the same array in detailView.

You can do this using following code:

In masterView on didSelectRowAtIndexPath method:

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

You can retrieve that array in detailView using below code:

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

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

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

OR

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

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

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

Hope this helps you :)

FINAL EDIT:

Replace your didSelectRowAtIndexPath: method with below method in 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];

}

This would surely work :)



来源:https://stackoverflow.com/questions/5366842/set-uilabel-text-from-plist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!