使用从根视图传递的参数从nib初始化Detail View

时间:2010-06-13 21:41:22

标签: iphone objective-c iphone-sdk-3.0

我有一个带有许多注释的地图视图...一旦点击了标注,我需要将几个参数传递给DetailViewController,所以我一直试图通过构造函数来做到这一点。我调试了一下,发现参数正在正确传递并在构造函数中按预期接收,但出于某种原因,每当我尝试更改我在笔尖中定位的IBOutlets的值时,它从未有过效果。

这是我传递的内容(顺便说一下,我在这一行得到了“没有initWithNibName:bundle:header'方法发现警告”):

DetailViewController *dvc = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil header:headerText];

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

现在是我的构造函数:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil header:(UILabel*)headerLabel {
 if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
     self.headerTextView = headerLabel;
     NSLog(@"header:%@", headerLabel.text);
 }
 return self;
 }

再一次,问题是headerLabel.text在控制台中正确打印,但行self.headerTextView = headerLabel;似乎没有做我想做的事。

谢谢! 修改

DetailViewController标题:

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

@interface DetailViewController : UIViewController
{
    IBOutlet UITextView *informationTextView;
    IBOutlet UIImageView *imageView;
    IBOutlet UIWebView *twitterFeedView;
    IBOutlet UILabel *headerTextView;
    NSString *imageURL;
    }

@property (nonatomic, retain) NSString *imageURL;
@property (nonatomic, retain) UILabel *headerTextView;
@property (nonatomic, retain) UITextView *informationTextView;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIWebView *twitterFeedView;

-(DetailViewController*)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil header:(UILabel*)headerLabel;

@end

相关的DetailViewController实现:

@synthesize imageView, informationTextView, twitterFeedView, headerTextView, imageURL;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil header:(UILabel*)headerLabel {
 if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
     self.headerTextView = headerLabel;
     NSLog(@"r%@", headerLabel.text);
 }
 return self;
 }

2 个答案:

答案 0 :(得分:1)

要消除警告,您需要确保在头文件中定义方法。接下来,我不太清楚你想要实现什么     self.headerTextView = headerLabel; 你是如何定义属性headertextview的?你能告诉我们物业代码吗? 你@synthesised吗?实际上,如果我们能够看到整个头文件,它可能会有所帮助。

答案 1 :(得分:1)

以下是我如何使用它:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil header:(NSString*)headerString {
 if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
     NSLog(@"test1:%@", self.headerTextView.text);
     self.headerTextView=[[UILabel alloc] init];
     [self.headerTextView setText:headerString];
     NSLog(@"test2:%@", self.headerTextView.text);
     globalHeaderText=headerString;
 }
 return self;
 }


- (void) viewDidLoad
{   
    [super viewDidLoad];
    NSLog(@"label.text upon creation:%@", self.headerTextView.text);
    [self.headerTextView setText:globalHeaderText];
}

以下是日志的结果:

2010-06-13 22:31:21.591 MyApp[29141:207] test1:(null)
2010-06-13 22:31:21.592 MyAppn[29141:207] test2:mandolinegrill
2010-06-13 22:31:21.600 MyApp[29141:207] label.text upon creation:Label

换句话说,[self.headerTextView setText:globalHeaderText]; DOES 在viewDidLoad中工作,但在initWithNibName中不起作用。有谁知道如何解决这个问题?