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