2015-06-13 3 views
0

Я использую элемент выбора контактов для захвата строки, а затем передаю эту строку другому контроллеру представления, однако UILabel не обновляет данные (или любую другую строку).UILabel не обновляется при вызове метода делегата

В SlingViewController.m журналы, _taggedFriendsNames успешно передается.

Возможно, проблема связана с тем, что контроллер приемного устройства пытается обновить метку на другом (SlingshotView)? Я не думаю, что это так, так как я обновлял метки таким образом другими способами.

Ответ, вероятно, связан с обновлением UILabels в целом, но мне не повезло после поиска.

Вещи я проверил без успеха:

  1. Обновление от основного потока асинхронно
  2. @synthesize метка в SlingshotView
  3. вызова setDisplay

Включено потенциально соответствующий код ниже. Спасибо заранее за любые советы!

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]; 
+0

бы предположить, либо вид или метка равна нулю, так как этот метод вызывается. Попробуйте выполнить регистрацию и посмотрите, что вы получаете: 'NSLog (@"% @% @ ", slingshotView, friendsPickedLabel);' – JeffN

+0

Вы вызываете метод делегата в новом экземпляре 'SlingViewController', который вы создаете локально, а затем освобождаетесь на конец метода. Это уже не то, что у вас уже есть. – dan

ответ

0

Вы перераспределить это ..

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

Смысл вашего slingshotView.friendsPickedLabel становится nil ..

И вы звоните/с помощью делегата неправильный путь, я думаю, что это, предполагают, чтобы быть

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


из вашего кода вы используете -(void)updateFriendsPickedLabel:(id)sender; внутри SlingViewController и не делегат, вы не выполняют делегата либо.

Да, метод -(void)updateFriendsPickedLabel:(id)sender; называется, потому что вы вызываете его непосредственно из класса.

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]; 
} 

Хм .. Я думаю, что вы реализовали делегатам неправильный путь.

Это, предполагают, чтобы быть комментарием, но его слишком долго ..

Смежные вопросы