2

我有一个 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:nilnameString

didSelectRowAtIndexPath:当我在 ` 行设置断点时

userName=[NSString stringWithFormat:@"%@",[arrayOfNames objectAtIndex:indexPath.row]]`

我可以看到userName = @"Adam Dart"哪个是正确的

在我的第二个断点中,friendController.nameString=userName;我看到nameString =niluserName = Variable is not CFString

ARC 设置为 NO

4

4 回答 4

2

该值为 nil 因为您没有在request:didLoad:函数中传递该值。

在 functiondidSelectRowAtIndexPath中,您创建了另一个 ViewController 的本地实例并设置了 nameString 的值,但您没有呈现视图并立即释放 ViewController。你实际上在这几行代码中什么都不做:

FriendDetail *profileDetailName = [[FriendDetail alloc] initWithNibName: @"FriendDetail" bundle: nil];
profileDetailName.nameString = userName;
[profileDetailName release];

在 functionrequest:didLoad:中,您再次使用图像创建另一个 ViewController 的本地实例。但是这个实例只是这个函数的本地实例,这意味着与创建的那个没有关系didSelectRowAtIndexPath

您需要做的是,首先记住单击行的名称didSelectRowAtIndexPath,这里您不必创建 ViewController 实例。请求完成后,将图像和名称都设置给控制器,然后呈现。但是您应该避免用户同时单击不同的行,因为您不知道请求何时完成。

于 2012-06-25T03:22:13.390 回答
1

You have two instances of FriendDetail called profileDetailPicture. Both of theses profileDetailPicture are not the same. So in your didSelectRowAtIndexPath method, the value that you assigned to the nameString will not be visible/available to the nameString of the profileDetailPicture In the request:(FBRequest *)request didLoad method.

Edit for solution:

  1. Create an iVar or property (profileDetailPicture) in the FriendController.
  2. Only do one allocation in the request:(...) method.
  3. Remove the allocation statement in the didSelectRowAtIndexPath.
于 2012-06-25T03:17:28.723 回答
0

任何机会与您分配profileDetailName然后立即释放它的事实有关吗?

profileDetailName.nameString=userName;
[profileDetailName release];
于 2012-06-25T02:59:46.873 回答
0

您必须在“second_controller”中分配“first_controller”来传递字符串等对象。你会以不同的方式调用 nameString 。

例子:

second_controller.h

#import "first_controller.h"
...
@interface second_controller : UIViewController{
   first_controller* firstController;
}

second_controller.m

- (void)viewDidLoad {
    [super viewDidLoad];
    firstController = [[first_controller alloc] init];
    profileName.text = firstController.nameString;
}

您必须正确初始化它,因为它的两个视图共享信息。

于 2012-06-25T03:01:30.060 回答