Change UILabel text in a preview from the main view

Ok, so I am a relative noob with Objective-C / iOS programming, so hopefully someone with more knowledge here can help me.
I have an iPad application using the SplitViewController template (with master data). I created another UIViewController(with xib file) called PlayerViewController. This view has several components UILabel.

I have a list of players that appear in RootViewController ( UITableView), and when you select a player, I programmatically create a PlayerViewController (in DetailViewController), pass it to it NSManagedObject, which was passed to DetailViewController, try setting the text of one of the labels in the PlayerViewController, and then add its as a sub-view in DetailViewController.

All this works fine except for setting the shortcut text in the PlayerViewController. I'm not sure what I'm doing wrong. I used NSLogto confirm that NSManagedObjectit is not equal to zero and that the property NSManagedObjectthat I am trying to use has the correct text.

I don’t get it here. Any help would be greatly appreciated. (Code follows):

This method is located in the file DetailViewController.m:

- (void)configureView {
    // Update the user interface for the detail item.
    PlayerViewController *player = [[PlayerViewController alloc] init];
    player.player = detailItem;
    [self.view addSubview:player.view];
}

, RootViewController ( , configureView, , ).

PlayerViewController detailItem setPlayer .

- (void)setPlayer:(NSManagedObject *)managedObject {

    if (player != managedObject) {
        [player release];
        player = [managedObject retain];

        // Update the view.
        [self configureView];
    }
}

configureView PlayerViewController, :

- (void)configureView {
    nickName.text = [[player valueForKey:@"Nickname"] description];
    NSLog(@"Nickname %@", [[player valueForKey:@"Nickname"] description]);
    NSLog(@"Nickname %@", nickName.text);
}   

, NSLog , UILabel ( nickName) nil.

PlayerViewController.h .m:

PlayerViewController.h:

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>


@interface PlayerViewController : UIViewController {

    NSManagedObject *player;

    IBOutlet UILabel *nickName;
    IBOutlet UILabel *goalCount;
    IBOutlet UILabel *assistCount;
    IBOutlet UILabel *timeInGame;
}

@property (nonatomic, retain) IBOutlet UILabel *nickName;
@property (nonatomic, retain) IBOutlet UILabel *goalCount;
@property (nonatomic, retain) IBOutlet UILabel *assistCount;
@property (nonatomic, retain) IBOutlet UILabel *timeInGame;

@property (nonatomic, retain) NSManagedObject *player;

@end

PlayerViewController.m:

#import "PlayerViewController.h"


@implementation PlayerViewController

@synthesize nickName, goalCount, assistCount, timeInGame, player;

#pragma mark -
#pragma mark Managing the detail item

/*
 When setting the player item, update the view 
 */
- (void)setPlayer:(NSManagedObject *)managedObject {

    if (player != managedObject) {
        [player release];
        player = [managedObject retain];

        // Update the view.
        [self configureView];
    }
}       

- (void)configureView {
    nickName.text = [[player valueForKey:@"Nickname"] description];
    NSLog(@"Nickname %@", [[player valueForKey:@"Nickname"] description]);
    NSLog(@"Nickname %@", nickName.text);
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

, - , .

!

+3
3

, , , , . , , , , . configureView PlayerViewController.m viewDidLoad() setPlayer(). .

+4

configureView :

- (void)configureView {
    nickName.text = (NSString*)[player valueForKey:@"Nickname"];
} 
0

Yes, it is better to use the call method

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configureView]; 
}
  • (void) setPlayer: (NSManagedObject *) managedObject is called before loading nib files.
0
source

Source: https://habr.com/ru/post/1774983/


All Articles