[英]iOS nav bar with username
我們如何處理向導航欄標題顯示http發布的用戶名? 這是一個成功顯示UIScrollView*listview;
人們拍攝的jpeg代表照片中的用戶名的方法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;
}
在標簽接收信息后,將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;
}
根據是否需要立即加載,您應該在viewDidLoad
或viewWillAppear
方法中調用-(void)initWithIndex:(int)i andData:(NSDictionary*)data
。
UPDATE
試試這個:
[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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.