【问题标题】:UILabel not updating when called from delegate method从委托方法调用时,UILabel 不更新
【发布时间】:2015-06-13 21:33:30
【问题描述】:

我正在使用联系人选择器来抓取一个字符串,然后将该字符串传递给另一个视图控制器,但是 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];

【问题讨论】:

  • 假定视图或标签为 nil,因为正在调用该方法。尝试记录,看看你得到了什么:NSLog(@"%@ %@", slingshotView, friendsPickedLabel);
  • 您在本地创建的 SlingViewController 的新实例上调用委托方法,然后在方法结束时被释放。它不是你已经拥有的。

标签: ios objective-c iphone delegates uilabel


【解决方案1】:

你正在重新分配这个..

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

意味着你的 slingshotView.friendsPickedLabel 变为 nil..

而且你以错误的方式调用/使用委托,我认为它应该是

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


从您的代码中,您在SlingViewController 中使用-(void)updateFriendsPickedLabel:(id)sender; 而不是委托,您也没有实现委托..

是的,-(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];
}

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

这应该是一个评论,但它太长了..

【讨论】:

    猜你喜欢
    • 2012-09-22
    • 2014-11-21
    • 2017-02-14
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多