0

我整个下午都在忙着想弄清楚为什么下面的代码不起作用。我要做的就是将一个字符串从一个类传递到另一个类。

在我的 FirstDetailViewController.h 文件中,我声明了 NSString

@property(nonatomic, retain) NSString *infoForArray;

然后在我的 Grinding01_DetailViewController.m 我尝试为字符串设置一个值

#import "Grinding01_DetailViewController.h"
#import "FirstDetailViewController.h"

@implementation Grinding01_DetailViewController


...

    NSString *didLoadMessage = @"Grinding01 Loaded";

    FirstDetailViewController *temp = [[FirstDetailViewController alloc] initWithNibName:@"FirstDetailView" bundle:nil];

    temp.infoForArray = didLoadMessage;

    [self.navigationController pushViewController:temp animated:YES];

}

当我从 FirstDetailViewController.h 输出 infoForArray 时,它为空。

任何帮助将不胜感激,我认为我缺少一个简单的步骤,但我只是看不到它。


编辑:这是来自 FirstDetailViewController 的代码

FirstDetailViewController.h

#import <UIKit/UIKit.h>
#import "Protocols.h"

@interface FirstDetailViewController : UIViewController <SubstitutableDetailViewController> {

    //for the output
    IBOutlet UITextView *outputView;

    UIToolbar *navigationBar;

    NSMutableArray *logMessages;

}

@property (nonatomic, retain) IBOutlet UIToolbar *navigationBar;

//for incoming messages
@property(nonatomic, retain) NSString *infoForArray;


@end

FirstDetailViewController.m

#import "FirstDetailViewController.h"

@implementation FirstDetailViewController

@synthesize navigationBar, infoForArray;

-(void)viewDidLoad{

    [super viewDidLoad];

    //The log cannot be changed
    outputView.editable = NO;    

}


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidUnload {

    [super viewDidUnload];

    self.navigationBar = nil;

}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];

    NSLog(@"message: %@", infoForArray);

    outputView.text = infoForArray;

}

#pragma mark -
#pragma mark Managing the popover

- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {

    // Add the popover button to the toolbar.
    NSMutableArray *itemsArray = [navigationBar.items mutableCopy];
    [itemsArray insertObject:barButtonItem atIndex:0];
    [navigationBar setItems:itemsArray animated:NO];
    [itemsArray release];
}


- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {

    // Remove the popover button from the toolbar.
    NSMutableArray *itemsArray = [navigationBar.items mutableCopy];
    [itemsArray removeObject:barButtonItem];
    [navigationBar setItems:itemsArray animated:NO];
    [itemsArray release];
}


#pragma mark -
#pragma mark Rotation support

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
    [navigationBar release];
    [super dealloc];
}   


@end
4

2 回答 2

0

我的猜测是您正在 FirstDetailViewController 的实例化过程中的某处检查 infoForArray,该过程发生在您设置temp.infoForArray = didLoadMessage.

仅供参考,如果您签infoForArrayviewDidLoad那将太早了。viewDidLoad当视图被放入内存时触发。你想要的是viewDidAppear,你可能需要自己添加

于 2012-07-10T22:43:38.407 回答
0

听起来麻烦区域在 FirstDetailViewController 中。我建议发布代码,这样我们就可以看到发生了什么。

于 2012-07-10T23:21:49.857 回答