我有一个NSString属性self。textFromTextVC,在Viewcontroller中,它的值在IBAction方法中变为null。
- (IBAction)buttonPressed:(id)sender
{
NSLog(@"text before alarm is created: %@", self.textFromTextVC);
}
下面的方法是相同的。
-(void)setPropertyTextToReceivedText:(NSString *)text
{
self.textFromTextVC = text;
NSLog(@"text received from text VC: %@", self.textFromTextVC);
[self doesStringKeepValue]; //I call this method to check and see if the NSString value
//was retained
}
-(void)doesStringKeepValue
{
NSLog(@"keep value: %@", self.textFromTextVC); //NSString value the same from the above
//method
}
下面是我如何声明NSString属性的:
@property (nonatomic, copy) NSString *textFromTextVC;
基本上,我在IBAction方法被调用之前设置了self.textFromTextVC
,这就是为什么我感到困惑。我真的不知道发生了什么事。我选择了ARC。我希望我只是犯了个小错误……救命?谢谢你,
下面是另一个视图控制器中的方法,我调用setPropertyTextToReceivedText:
@implementation TextViewController
@synthesize typedText;
- (IBAction)doneButton:(id)sender {
[self.typedText resignFirstResponder];
AlarmViewController *receiver = [[AlarmViewController alloc]init];
[receiver setPropertyTextToReceivedText:self.typedText.text];
//[self showAlert];
}
您的问题是receiver
与显示(当前/已推送)的VC
不同。
AlarmViewController *receiver = [[AlarmViewController alloc]init];
[receiver setPropertyTextToReceivedText:self.typedText.text];
修改如下:
NSLog(@"text before alarm is created: %@", self.textFromTextVC);
:
NSLog(@"%@: text before alarm is created: %@", self, self.textFromTextVC);
它可能会告诉你,你确实在看同一个类的两个不同的对象实例。
我想你忘记了:
@synthesize textFromTextVC;