Question

I got a little problem. Here's my source code:

The .h:

#import <UIKit/UIKit.h>

@interface TBNoteViewController : UIViewController <UITextViewDelegate>
@property (nonatomic, copy) NSString *noteKey;
- (IBAction)dismissKeyboard:(id)sender;
- (id)initWithNoteIndex:(int)index;
@end

The .m:

#import "TBNoteViewController.h"
#import "PSPDFTextView.h"
#include <tgmath.h>

@interface TBNoteViewController ()
{
CGRect _keyboardRect;
BOOL _keyboardVisible;
}

@property (nonatomic, strong) UIBarButtonItem *rightBarButton;
@property (nonatomic, strong) PSPDFTextView *textView;

@end

@implementation TBNoteViewController
@synthesize noteKey;

- (id)initWithNoteIndex:(int)index
{
self = [super init];

if (self) {
    NSMutableArray *data = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults   standardUserDefaults] objectForKey:@"Notes"] copyItems:YES];
    self.noteKey = [NSString stringWithFormat:@"%@", [data objectAtIndex:index]];

    NSLog(@"1: %@", self.noteKey);
}

return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{

NSLog(@"2: %@", self.noteKey);

// Register notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];

_rightBarButton = [self.navigationItem rightBarButtonItem];

self.navigationItem.rightBarButtonItem = nil;

PSPDFTextView *textView = [[PSPDFTextView alloc] initWithFrame:self.view.bounds];
textView.delegate = self;
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
textView.font = [UIFont systemFontOfSize:17.f];

[self.view addSubview:textView];

self.textView = textView;

[self.textView setTextContainerInset:UIEdgeInsetsMake(5, 12, 5, 12)];
self.textView.alwaysBounceVertical = YES;
self.textView.keyboardAppearance = UIKeyboardAppearanceDark;

textView.text = self.noteKey;

[super viewDidLoad];
// Do any additional setup after loading the view.
}

So my problem is that the property noteKey has a string, but in viewDidLoad it is null.

So, you can see the NSLogs, the output would be (just copied that):

2014-02-13 23:07:21.197 TaskBeater[3205:70b] 1: Hoho.

2014-02-13 23:07:21.199 TaskBeater[3205:70b] 2: (null)

I would highly appreciate if you could help me, I have no idea what's wrong there.

Was it helpful?

Solution

It looks to me like you're instantiating two different "TBNoteViewController" instances.

You're doing one programatically (in code), which is how you're getting the "noteKey" property to appear properly.

Then the second one is created because you loaded it from a XIB file, which is why "viewDidLoad:" gets called.

After getting rid of the extra one that you're calling in code, create an outlet to the one you created via the XIB file, and then you can set properties on that and you should be all set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top