0

I have a NSObject called GettingHere which has a NSString *content.

I then have a UIViewController on which I create a button programatically as follows (this button working as intended):

byAirButton = [UIButton buttonWithType:UIButtonTypeCustom];
byAirButton.tag = 1;
byAirButton.frame = CGRectMake(25, 140, 280.f, 40.f);
UIImage *airButton = [UIImage imageNamed:@"gettingHereByAirButton.png"];
[byAirButton setBackgroundImage:airButton forState:UIControlStateNormal];
[self.view addSubview:byAirButton];
[byAirButton addTarget:self action:@selector(byAirButtonClicked) forControlEvents:UIControlEventTouchUpInside];

For the action:@selector(byAirButtonClicked), I do the following. gettingHere is an instance of the GettingHere object.

- (void) byAirButtonClicked
{
    gettingHere.content = @"This is how to get here by Air";
    NSLog(@"Content: %@", gettingHere.content);
    [self performSegueWithIdentifier:@"gettingHereSegue" sender:self];
}

The idea is to set the content for my GettingHere object and then just call that from the next view (GettingHereViewController) when the user clicks the byAirButton. This NSLog shows that content is being set.

In my prepareForSegue, I do the following:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"gettingHereSegue"])
    {
        NSLog(@"Content to be passed: %@", gettingHere.content);

        GettingHereViewController *vc = (GettingHereViewController *)segue.destinationViewController;
        vc.gettingHere.content = gettingHere.content;
    }
}

The segue works fine, but the NSLog shows my gettingHere object values as being (null).

Can anyone tell me where I am going wrong please? I have stepped through it several times but can't figure out where I am going wrong.

EDIT: Here is how I instantiate the GettingHere Object.

In the SubNavViewController.h

#import "GettingHereContent.h"

@interface SubNavViewController : UIViewController
@property GettingHereContent *gettingHere;

In the SubNavViewController.m

#import "SubNavViewController.h"
#import "GettingHereViewController.h"

#import "GettingHereContent.h"

@interface SubNavViewController ()
@end

@implementation SubNavViewController
@synthesize gettingHere;

And here is how I create the GettingHere Object: GettingHere.h

#import <Foundation/Foundation.h>
@interface GettingHereContent : NSObject
@property (nonatomic, strong) NSString *content;
@end

GettingHere.m

#import "GettingHereContent.h"
@implementation GettingHereContent
@synthesize content;
@end
heyred
  • 2,031
  • 8
  • 44
  • 89
  • Where is gettingHere defined? Is it a property of the VC? Try sender.gettingHere.content in the prepareForSegue: – Evan Layman Nov 02 '13 at 17:22
  • Nope, sender.gettingHere.content does nothing. sender. has no options. And yes I define getting here in this UIViewController .h file: (HAD TO OMMIT AT SIGNS AS STACKOVERFLOW DO NOT ALLOW THESE IN COMMENTS - BUT THEY ARE ALL PRESENT WHERE NEEDED) #import "GettingHereContent.h" interface SubNavViewController : UIViewController property GettingHereContent *gettingHere; and then I synthesise it in the .m as follow: #import "GettingHereContent.h" interface SubNavViewController () end implementation SubNavViewController @ynthesize gettingHere; – heyred Nov 02 '13 at 17:27
  • Can you add the code snippet where gettingHere is created? – Evan Layman Nov 02 '13 at 17:28
  • Also comment out the performSegueWithIdentifier line in the buAirButtonClicked method to confirm the prepareForSegue method is not being called by another path. – Evan Layman Nov 02 '13 at 17:32
  • Hmm if it's a synthesized property self.gettingHere or in this case sender.gettingHere should be defined. Can you break point in prepareForSegue and verify the stack trace came from the byAirButtonClicked: method – Evan Layman Nov 02 '13 at 17:36
  • Ok I have stepped through and have taken this screen shot. Might help a bit more. http://imm.io/1jGcu – heyred Nov 02 '13 at 17:51
  • In the screenshot the log shows the content is null at your byAirButtonClicked: method as well. Take a look at that code again to make sure it's setting the content correctly. – Evan Layman Nov 02 '13 at 17:54
  • Yeah just stepped through the byAirButton() method now and seen that content is nil even after I set it. Im a bit new to Objective-C, so is there another way I need to set the content? Other than gettingHere.content = @"This is how to get here by Air"; – heyred Nov 02 '13 at 17:58

1 Answers1

0

You never alloc init your gettingHere property. Try this in your init method of your VC

gettingHere = [[GettingHereContent alloc] init];

Also don't forget to release it: answer from here: alloc + init with synthesized property - does it cause retain count to increase by two? @interface Foo : Bar { SomeClass* bla; } @property (nonatomic, retain) SomeClass* bla; @end

@implementation Foo @synthesize bla; -(id)init { ... bla = [[SomeClass alloc] init]; ... } -(void)dealloc { [bla release]; ... [super dealloc]; }

Community
  • 1
  • 1
Evan Layman
  • 3,691
  • 9
  • 31
  • 48
  • Just alloc init'ed my gettingHere and also released it but still nothing. Ill step through it again and try and figure out where the issue is. – heyred Nov 02 '13 at 19:20