gpt4 book ai didi

ios - NSString 属性在其他方法中的 init 方法后为空?

转载 作者:行者123 更新时间:2023-12-01 19:02:47 25 4
gpt4 key购买 nike

我有一个小问题。这是我的源代码:

.h:

#import <UIKit/UIKit.h>

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

他们:
#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:呵呵。

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

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

最佳答案

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

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

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

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

关于ios - NSString 属性在其他方法中的 init 方法后为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21766798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com