【问题标题】:NSString property gets null after init method in other methods?NSString 属性在其他方法中的 init 方法后为空?
【发布时间】:2014-02-13 22:21:34
【问题描述】:

我遇到了一个小问题。这是我的源代码:

.h:

#import <UIKit/UIKit.h>

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

.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.
}

所以我的问题是属性 noteKey 有一个字符串,但是在 viewDidLoad 中它是空的。

所以,你可以看到 NSLogs,输出将是(只是复制的):

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

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

如果你能帮助我,我将不胜感激,我不知道那里出了什么问题。

【问题讨论】:

    标签: ios objective-c properties nsstring viewdidload


    【解决方案1】:

    在我看来,您正在实例化两个不同的“TBNoteViewController”实例。

    您正在以编程方式(在代码中)执行一项操作,这就是让“noteKey”属性正确显示的方式。

    然后创建第二个,因为您从 XIB 文件加载了它,这就是调用“viewDidLoad:”的原因。

    在去掉你在代码中调用的额外的之后,为你通过 XIB 文件创建的那个创建一个出口,然后你可以在上面设置属性,你应该已经准备好了。

    【讨论】:

    • 我这样创建它:TBNoteViewController *con = [[TBNoteViewController alloc] initWithNoteIndex:0]; con = [self.storyboard instantiateViewControllerWithIdentifier:@"Note"]; [self.navigationController pushViewController:con animated:YES];
    • 所以,您刚刚向我确认您正在创建它两次。第二次调用“con =”会释放您在上面一行中创建的第一个“TBNoteViewController”实例。将“noteKey”设为属性,然后您可以在从情节提要中实例化视图控制器后设置 noteKey。
    • 好的,谢谢。有没有办法用我的故事板视图和我的 initWithNoteIndex 方法只创建一次?
    • 既然你已经在你的故事板中创建了 TBNoteViewController,你不应该使用你的 "initWithNoteIndex:" 方法来创建一个无用的第二个实例。
    猜你喜欢
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多