我有一个 NSMutableArray 的名字。我希望将 NSMutableArray 内的数据(选定名称)作为文本传递给另一个视图的标签。
FriendsController.m:
- (void)viewDidLoad {
[super viewDidLoad];
arrayOfNames=[[NSMutableArray alloc] init];
arrayOfIDs=[[NSMutableArray alloc] init];
userName=[[NSString alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
long long fbid = [[arrayOfIDs objectAtIndex:indexPath.row]longLongValue];
NSString *user=[NSString stringWithFormat:@"%llu/picture",fbid];
[facebook requestWithGraphPath:user andDelegate:self];
userName=[NSString stringWithFormat:@"%@",[arrayOfNames objectAtIndex:indexPath.row]];
FriendDetail *profileDetailName = [[FriendDetail alloc] initWithNibName: @"FriendDetail" bundle: nil];
profileDetailName.nameString=userName;
[profileDetailName release];
}
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSData class]]) {
transferImage = [[UIImage alloc] initWithData: result];
FriendDetail *profileDetailPicture = [[FriendDetail alloc] initWithNibName: @"FriendDetail" bundle: nil];
[profileDetailPicture view];
profileDetailPicture.profileImage.image= transferImage;
profileDetailPicture.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:profileDetailPicture animated:YES];
[profileDetailPicture release];
}
}
在 FriendDetail.h 中
NSString nameString;
IBOutlet UILabel *profileName;
@property (nonatomic, retain) UILabel *profileName;
@property (nonatomic, retain) NSString *nameString;
在 FriendDetail.m 中
- (void)viewDidLoad
{
[super viewDidLoad];
profileName.text=nameString;
}
nameString
在第二个控制器(FriendDetail)返回nil
。当我在 firstcontroller 中设置断点时,我看到里面的字符串nameString
是正确的,但之后它会以nil
某种方式返回。
- - - - - - - - - - - -编辑 - - - - - - - - - - - - - --------------
根据答案,我已经稍微改进了我的代码 FriendsController.h
FriendDetail *friendController;
@property (strong, nonatomic) FriendDetail *friendController;
FriendsController.m
- (void)viewDidLoad
{
[super viewDidLoad];
arrayOfNames=[[NSMutableArray alloc] init];
arrayOfIDs=[[NSMutableArray alloc] init];
arrayOfThumbnails=[[NSMutableArray alloc] init];
userName=[[NSString alloc] init];
friendController= [[FriendDetail alloc] initWithNibName: @"FriendDetail" bundle: nil];
}
-(void)request:(FBRequest *)request didLoad:(id)result{
if ([result isKindOfClass:[NSData class]])
{
transferImage = [[UIImage alloc] initWithData: result];
friendController.nameString=userName;
[friendController view];
friendController.profileImage.image= transferImage;
friendController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:friendController animated:YES];
}
//this is how i take facebook friends list
if ([result isKindOfClass:[NSDictionary class]]){
items = [[(NSDictionary *)result objectForKey:@"data"]retain];
for (int i=0; i<[items count]; i++) {
NSDictionary *friend = [items objectAtIndex:i];
long long fbid = [[friend objectForKey:@"id"]longLongValue];
NSString *name = [friend objectForKey:@"name"];
NSLog(@"id: %lld - Name: %@", fbid, name);
[arrayOfNames addObject:[NSString stringWithFormat:@"%@", name]];
[arrayOfIDs addObject:[NSNumber numberWithLongLong:fbid]];
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
long long fbid = [[arrayOfIDs objectAtIndex:indexPath.row]longLongValue];
NSString *user=[NSString stringWithFormat:@"%llu/picture",fbid];
userName=[NSString stringWithFormat:@"%@",[arrayOfNames objectAtIndex:indexPath.row]];
[facebook requestWithGraphPath:user andDelegate:self];
[username retain]
}
现在,当我第一次选择行时,它会发送名称。当我回到 tableview 并选择另一个名称时,它会显示旧名称。
如果我删除 它仍然发送[username retain]
到didSelectRowAtIndexPath:
nil
nameString
didSelectRowAtIndexPath:
当我在 ` 行设置断点时
userName=[NSString stringWithFormat:@"%@",[arrayOfNames objectAtIndex:indexPath.row]]`
我可以看到userName = @"Adam Dart"
哪个是正确的
在我的第二个断点中,friendController.nameString=userName;
我看到nameString =nil
了userName = Variable is not CFString
ARC 设置为 NO