Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSString属性在其他方法中的init方法之后变为null?_Ios_Objective C_Properties_Nsstring_Viewdidload - Fatal编程技术网

Ios NSString属性在其他方法中的init方法之后变为null?

Ios NSString属性在其他方法中的init方法之后变为null?,ios,objective-c,properties,nsstring,viewdidload,Ios,Objective C,Properties,Nsstring,Viewdidload,我有点小问题。以下是我的源代码: A.h: #import <UIKit/UIKit.h> @interface TBNoteViewController : UIViewController <UITextViewDelegate> @property (nonatomic, copy) NSString *noteKey; - (IBAction)dismissKeyboard:(id)sender; - (id)initWithNoteIndex:(int)ind

我有点小问题。以下是我的源代码:

A.h:

#import <UIKit/UIKit.h>

@interface TBNoteViewController : UIViewController <UITextViewDelegate>
@property (nonatomic, copy) NSString *noteKey;
- (IBAction)dismissKeyboard:(id)sender;
- (id)initWithNoteIndex:(int)index;
@end
#导入
@接口TBNoteViewController:UIViewController
@属性(非原子,复制)NSString*noteKey;
-(iAction)键盘:(id)发送方;
-(id)initWithNoteIndex:(int)索引;
@结束
主席先生:

#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.
}
#导入“TBNoteViewController.h”
#导入“PSPDFTextView.h”
#包括
@接口TBNoteViewController()
{
CGRECTU键盘RECT;
BOOL_键盘可见;
}
@属性(非原子,强)uibarbuttonite*rightbutton;
@属性(非原子,强)PSPDFTextView*textView;
@结束
@TBNoteViewController的实现
@综合注释键;
-(id)initWithNoteIndex:(int)索引
{
self=[super init];
如果(自我){
NSMutableArray*数据=[[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@“注意事项”]copyItems:是];
self.noteKey=[NSString stringWithFormat:@“%@,[data objectAtIndex:index]];
NSLog(@“1:%@”,self.noteKey);
}
回归自我;
}
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
NSLog(@“2:%@”,self.noteKey);
//注册通知
[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(keyboardWillShowNotification:)名称:UIKeyboardWillShowNotification对象:nil];
[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(keyboardWillHideNotification:)名称:UIKeyboardWillHideNotification对象: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 settExtContainerSet:UIEdgeInsetsMake(5,12,5,12)];
self.textView.alwaysBounceVertical=是;
self.textView.keyboardAppearance=UIKeyboardAppearanceDark;
textView.text=self.noteKey;
[超级视图下载];
//加载视图后执行任何其他设置。
}
所以我的问题是属性noteKey有一个字符串,但在viewDidLoad中它是空的

因此,您可以看到NSLogs,输出将是(仅复制该文件):

2014-02-13 23:07:21.197打手[3205:70b]1:Hoho

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


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

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

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

然后创建第二个,因为您从XIB文件加载了它,这就是为什么会调用“
viewDidLoad:


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

我这样创建它:
TBNoteViewController*con=[[TBNoteViewController alloc]initWithNoteIndex:0];con=[self.storyboard实例化eviewcontrollerwhiteIdentifier:@“Note”];[self.navigationController-pushViewController:con-animated:YES]那么,您刚刚向我确认您将创建它两次。第二次调用“
con=
”将释放在上面一行中创建的第一个“
TBNoteViewController
”实例。将“
noteKey
”设置为属性,然后在从情节提要实例化视图控制器后可以设置noteKey。好的,谢谢。有没有办法使用my storyboard view和my initWithNoteIndex方法只创建一次?因为您已经在storyboard中创建了TBNoteViewController,所以不应该使用“
initWithNoteIndex:
”方法来创建无用的第二个实例。