【发布时间】:2015-06-13 21:33:30
【问题描述】:
我正在使用联系人选择器来抓取一个字符串,然后将该字符串传递给另一个视图控制器,但是 UILabel 没有使用数据(或任何其他字符串)进行更新。
在下面的SlingViewController.m日志中,_taggedFriendsNames正在被成功传递。
也许问题是因为接收视图控制器试图更新另一个 (SlingshotView) 视图上的标签?我认为情况并非如此,因为我一直在以其他方法以这种方式更新标签。
答案很可能与更新 UILabels 有关,但搜索后我没有运气。
我检查过的事情没有成功:
- 从主线程异步更新
-
@synthesizeSlingshotView 中的标签 - 致电
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