从委托方法调用时,UILabel不会更新

时间:2015-06-13 21:33:30

标签: ios objective-c iphone delegates uilabel

我正在使用联系人选择器来获取字符串,然后将该字符串传递给另一个视图控制器,但是UILabel没有使用数据(或任何其他字符串)进行更新。

在下面的 SlingViewController.m 日志中,成功传递了_taggedFriendsNames

也许问题是因为接收视图控制器正在尝试更新另一个(SlingshotView)视图上的标签?我不认为是这种情况,因为我在其他方法中一直以这种方式更新标签。

答案可能与更新UILabels有关,但搜索后我没有运气。

我检查过的事情没有成功:

  1. 异步更新主线程
  2. @synthesize SlingshotView中的标签
  3. 致电setDisplay
  4. 下面包含可能相关的代码。提前感谢任何提示!

    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 个答案:

答案 0 :(得分:0)

你正在重新分配这个..

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

意思是你的     slingshotView.friendsPickedLabel变为零..

你正在以错误的方式调用/使用委托,我认为它是

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

嗯..我认为你以错误的方式实施了代表。

这可能是一个评论,但它太长了..