UILabel not updating when called from delegate method

139 views Asked by At

I'm using a contact picker to grab a string, then pass that string to another view controller, however the UILabel is not updating with the data (or any other string).

In the SlingViewController.m logs below, _taggedFriendsNames is being passed successfully.

Perhaps the issue is because the receiving view controller is trying to update the label on another (SlingshotView) view? I don't think that's the case as I've been updating labels in this way in other methods.

The answer is likely related to updating UILabels in general, but I've had no luck after searching.

Things I've checked with no success:

  1. Updating from the main thread asynchronously
  2. @synthesize the label in SlingshotView
  3. calling setDisplay

Included potentially relevant code below. Thanks in advance for any tips!

SlingViewController.m

-(void)updateFriendsPickedLabel:(id)sender {
    NSLog(@"updateFriendsPickedLabel: %@", _taggedFriendsNames); // i see this
    slingshotView.friendsPickedLabel.text = @"any string"; // i don't see this
}

SlingViewController.h

@class TBMultiselectController;
@class SlingshotView;


@interface SlingViewController : UIViewController

@property (nonatomic, readonly) SlingshotView *slingshotView;

@property(nonatomic) NSString *taggedFriendsNames;


//for friend picker
-(void)updateFriendsPickedLabel:(id)sender;

@end

MultiSelectViewController.m

- (IBAction) sendButton: (id) sender {

NSMutableString *myString = [[NSMutableString alloc]initWithString:@""];

for (int i=0; i < self.selectedContacts.count; i++) {
    Contact *myContact = self.selectedContacts[i];
    [myString appendString:[NSString stringWithFormat:@"%@ %@ ", myContact.firstName, myContact.lastName]];
}

SlingViewController *svc = [[SlingViewController alloc] init];
svc.taggedFriendsNames = myString;
[svc updateFriendsPickedLabel:self];

[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

MultiSelectViewController.h

@protocol TBMultiselectControllerDelegate;

@class SlingViewController;

@interface TBMultiselectController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, TBContactsGrabberDelegate>

@property (nonatomic, assign) id<TBMultiselectControllerDelegate> delegate;

- (IBAction)sendButton: (id) sender;

@end

@protocol TBMultiselectControllerDelegate <NSObject>

-(void)updateFriendsPickedLabel:(id)sender;

@end

SlingshotView.h

@property (strong, nonatomic) UILabel *friendsPickedLabel;

SlingshotView.m

@synthesize friendsPickedLabel;

...
- (id)initWithFrame:(CGRect)frame {

if ((self = [super initWithFrame:frame])) {

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGRect imageFrame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);

    contentView = [[UIView alloc] initWithFrame:frame];
    [contentView setBackgroundColor:[UIColor whiteColor]];
    [contentView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
    [self addSubview:contentView];


    self.friendsPickedLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, screenRect.size.height/2-100, screenRect.size.width-20, 200)];
    self.friendsPickedLabel.shadowColor = [UIColor darkGrayColor];
    self.friendsPickedLabel.numberOfLines =  0;
    self.friendsPickedLabel.shadowOffset = CGSizeMake(0.5, 0.5);
    self.friendsPickedLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    [self.friendsPickedLabel setTextAlignment:NSTextAlignmentLeft];
    self.friendsPickedLabel.textColor = [UIColor whiteColor];
    self.friendsPickedLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:24];
    [contentView addSubview:self.friendsPickedLabel];
1

There are 1 answers

0
0yeoj On

You are reallocating this..

SlingViewController *svc = [[SlingViewController alloc] init];
svc.taggedFriendsNames = myString;
[svc updateFriendsPickedLabel:self];

Meaning your slingshotView.friendsPickedLabel becomes nil..

And you are calling/using the delegate the wrong way, i think it suppose to be

[self.delegate updateFriendsPickedLabel:@"YourData To be Passed"];


From your code you are using the -(void)updateFriendsPickedLabel:(id)sender; inside SlingViewController and not the delegate, you are not implementing the delegate either..

Yes the -(void)updateFriendsPickedLabel:(id)sender; method is called, bacause you are calling it directly from the class..

SlingViewController.h

@interface SlingViewController : UIViewController < TBMultiselectControllerDelegate > // for delegate implementation

@property (nonatomic, readonly) SlingshotView *slingshotView;

@property(nonatomic) NSString *taggedFriendsNames;


//for friend picker
//-(void)updateFriendsPickedLabel:(id)sender;

@end

MultiSelectViewController.m

- (IBAction) sendButton: (id) sender {

    NSMutableString *myString = [[NSMutableString alloc]initWithString:@""];

    for (int i=0; i < self.selectedContacts.count; i++) {
        Contact *myContact = self.selectedContacts[i];
        [myString appendString:[NSString stringWithFormat:@"%@ %@ ", myContact.firstName, myContact.lastName]];
    }

    /*
    SlingViewController *svc = [[SlingViewController alloc] init];
    svc.taggedFriendsNames = myString;
    [svc updateFriendsPickedLabel:self];
    */

     [self.delegate updateFriendsPickedLabel:@"YourString"]; 
     // this will call the method in your implementation class

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

Hmm.. I Think you have implemented the delegates the wrong way.

This is suppose to be a comment but its too long..