名前の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
2番目のコントローラー(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]
}
これで、最初に行を選択すると、名前が送信されます。テーブルビューに戻って別の名前を選択すると、古い名前が表示されます。
削除[username retain]
しdidSelectRowAtIndexPath:
てもnil
、nameString
didSelectRowAtIndexPath:
行`にブレークポイントを設定したとき
userName=[NSString stringWithFormat:@"%@",[arrayOfNames objectAtIndex:indexPath.row]]`
userName = @"Adam Dart"
どちらが正しいかわかります
行の2番目のブレークポイントfriendController.nameString=userName;
でnameString =nil
、userName = Variable is not CFString
ARCはNOに設定されています