开发者

Set UILabel Text From Plist?

开发者 https://www.devze.com 2023-02-17 18:26 出处:网络
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.

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 stringWithF开发者_如何学编程ormat:@"%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"];
}


@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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号