views:

62

answers:

2

I'm have a map view with a number of annotations on it... once the callout is clicked, i need to pass several parameters to the DetailViewController, so ive been trying to do this through the constructor. I've debugged a bit and discovered that the arguments are being passed properly and are being received as expected within the constructor, but for some reason whenever I try to change the values of the IBOutlets I've positioned in the nib, it never has an effect.

Here's what im passing (btw, im getting a "No initWithNibName : bundle : header' method found" warning at this line):

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

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

Now heres my constructor:

- (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;
 }

Once again, the problem is that headerLabel.text is printed properly in the console, but the line self.headerTextView = headerLabel; doesnt seem to be doing what I want it to do.

Thanks! edit:

DetailViewController header:

#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

relevant DetailViewController implementation:

@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;
 }
+1  A: 

To get rid of he warning, you need to make sure you define the method in your header file. For the next bit, I'm not quite sure what you want to Achieve with self.headerTextView = headerLabel; How have you defined the property headertextview? Can you give us the property code? Have you @synthesised it? In fact, it might help if we could see the entire header file.

Tom H
I edited and was able to get rid of the warning with your suggestion
culov
If you change NSLog(@"header:%@", headerLabel.text);to NSLog(@"header:%@", headerTextView.text);I presume you get nil output or an error?
Tom H
OK, I'm now officially confused. Why is headerTextView an IBOutlet? If so, I presume you have it hooked up in a nib? headerTextView is just a pointer to the object, and if you write the line you did, you are replacing it with a pointer to headerLabel. After that line, you have no reference to the original label.
Tom H
NSLog(@"header:%@", headerTextView.text); simply prints "Label"Yea, I do have it hooked up in the nib. i understand what youre saying about replacing with pointer to headerTextView. Originally I tried writing self.headerTextView.text = headerLabel.text; but that didnt have the effect I wanted, so the code I posted above was my second attempt at getting it right. shouldnt this have changed the next in the original label? I log the value after assigning it, and its correct, but when I log the value of headerTextView in viewDidLoad, it's still "Label"
culov
What exactly is the intended effect? I see no point in hooking it up in. Nib, then overwriting that. Why does the class need the whole label? Why not just thetext of it in a nsstring? That should be adaquate for most purpousez.
Tom H
The purpose is to display plain, static, text in a view. Yea, I realized that doing it that way I dont need a label, but what I'm having trouble figuring out is how to change the default text in a UILabel that I've already positioned in a .nib.
culov
A: 

Here's how I got it to work:

- (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];
}

Here are the results of the log:

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

In other words, [self.headerTextView setText:globalHeaderText]; DOES work in viewDidLoad, but doesnt in initWithNibName. Anyone know how I can resolve this?

culov
Ahhhh. I see. You can't use methods relating to the view in init method, because it hasn't been created and unarchived from the nib yet...
Tom H