使用@property将值从一个类传递到另一个类

时间:2012-07-10 22:35:09

标签: objective-c class variables

我整个下午一直在拔头发,想弄清楚为什么下面的代码不起作用。我所要做的就是传递一个字符串,从一个类到另一个类。

在我的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时,它为null。

任何帮助都会受到赞赏,我认为这是一个我想念的简单步骤,但我看不到它。


编辑:这是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

2 个答案:

答案 0 :(得分:0)

我的猜测是你在FirstDetailViewController的实例化过程中检查infoForArray,它在你设置temp.infoForArray = didLoadMessage之前发生。

仅供参考,如果您在infoForArray中检查viewDidLoad过早。将视图放入内存时会触发 viewDidLoad。你想要的是viewDidAppear ,你可能需要自己添加

答案 1 :(得分:0)

听起来像First DetailViewController中的麻烦区域。我建议发布代码,以便我们可以看到发生了什么。