我们如何处理向导航栏标题显示http发布的用户名?以下是一种方法,该方法成功显示UIScrollView*listview;
-(id)initWithIndex:(int)i andData:(NSDictionary*)data {
self = [super init];
if (self !=nil) {
//initialize
self.tag = [[data objectForKey:@"IdPhoto"] intValue];
int row = i/3;
int col = i % 3;
self.frame = CGRectMake(1.5*kPadding+col*(kThumbSide+kPadding), 1.5*kPadding+row*(kThumbSide+kPadding), kThumbSide, kThumbSide);
self.backgroundColor = [UIColor grayColor];
//add the photo caption
UILabel* caption = [[UILabel alloc] initWithFrame:CGRectMake(0, kThumbSide-16, kThumbSide, 16)];
caption.backgroundColor = [UIColor blackColor];
caption.textColor = [UIColor whiteColor];
caption.textAlignment = UITextAlignmentCenter;
caption.font = [UIFont systemFontOfSize:12];
caption.text = [NSString stringWithFormat:@"@%@",[data objectForKey:@"username"]];
[self addSubview: caption];
//add touch event
[self addTarget:delegate action:@selector(didSelectPhoto:) forControlEvents:UIControlEventTouchUpInside];
//load the image
API* api = [API sharedInstance];
int IdPhoto = [[data objectForKey:@"IdPhoto"] intValue];
NSURL* imageURL = [api urlForImageWithId:[NSNumber numberWithInt: IdPhoto] isThumb:YES];
AFImageRequestOperation* imageOperation = [AFImageRequestOperation imageRequestOperationWithRequest: [NSURLRequest requestWithURL:imageURL] success:^(UIImage *image) {
//create an image view, add it to the view
UIImageView* thumbView = [[UIImageView alloc] initWithImage: image];
thumbView.frame = CGRectMake(0,0,90,90);
thumbView.contentMode = UIViewContentModeScaleAspectFit;
[self insertSubview: thumbView belowSubview: caption];
}];
NSOperationQueue* queue = [[NSOperationQueue alloc] init];
[queue addOperation:imageOperation];
}
return self;
}
答案 0 :(得分:0)
在标签收到信息后,将self.navigationItem.title = label
放在与-(void)initWithIndex:(int)i andData:(NSDictionary*)data
方法相同的方法中。
-(void)initWithIndex:(int)i andData:(NSDictionary*)data {
//remove the "@" from before the %@ that you have below
label = [NSString stringWithFormat:@"@%@",[data objectForKey:@"username"]];
self.navigationItem.title = label;
}
根据是否需要立即加载,您应该使用-(void)initWithIndex:(int)i andData:(NSDictionary*)data
或viewDidLoad
方法调用viewWillAppear
。
<强>更新强>
尝试使用:
[self.navigationItem setTitle: label];
出于某种原因,这种方法效果更好。我在测试中试过了两个,这个对我有用。
更新2
使用值/对象初始化标签。
-(void)viewDidLoad{
label = @"";//initialises the value to be a blank string enabling use
//call your method that assigns the title now.
}
现在调用方法-(void)initWithIndex:(int)i andData:(NSDictionary*)data
时,标题应该在标签初始化时更新,现在将更新它的值。
更新3 - 根据用户要求提供
位于.m文件的顶部
@interface YourClassName ()
{
NSString * label;
}
@end
@implementation YourClassName
-(void)viewDidLoad{
label = @"";//initialises the value to be a blank string enabling use
}
-(void)initWithIndex:(int)i andData:(NSDictionary*)data {
//remove the "@" from before the %@ that you have below
label = [NSString stringWithFormat:@"@%@",[data objectForKey:@"username"]];
//the label now has a new value so use it for title
self.navigationItem.title = label;
//test to see that there is a value available in log
NSLog(@"What is the name: %@", label);
}