【发布时间】:2013-03-25 16:31:12
【问题描述】:
我试图在两个视图控制器之间移动一个 NSString,在搜索了所有复杂的方法之后,我想习惯的最简单和最直接的方法是在接收 VC 中编写一个 initWithName 函数并调用它在发送 VC 中。它确实成功地移动了它,但我希望它在 ViewDidLoad 加载 textViewer 之前执行,以便在按下选项卡按钮后立即显示。这是来自发送 VC 的代码:
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString: @""]) {
textView.text = @"*Paste the machine code in question here*";
}
SecondViewController *theVCMover = [[SecondViewController alloc] initWithName: textView.text];
[self.navigationController pushViewController:theVCMover animated:YES]; //Is this really necessary if I'm not going to segue it directly, I'm just waiting for the user to press the next tab
gotItLabel.text = @"Got it! Ready for action...";
}
这是接收 VC 上的代码:
- (id)initWithName:(NSString *)theVCMovee {
self = [super initWithNibName:@"SecondViewController" bundle:nil];
if (self) {
rawUserInput = theVCMovee;
CleanerText.text = rawUserInput;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
CleanerText.text = rawUserInput;
NSLog(@"Got the other tab's text and it's %@ ", rawUserInput);
}
【问题讨论】:
-
viewDidLoad在视图加载后被调用,并且视图在第一次尝试访问它时被加载,例如调用viewController.view,或者让父视图控制器布置其子视图(或显示其根视图控制器视图的窗口)。因此,只要您不尝试自己访问view属性,并且屏幕上还没有发生布局,您应该可以安全地在调用viewDidLoad之前设置您想要的所有参数(即在 @987654327 之间@ 和代码中的push…。 -
您是否使用自动引用计数 (ARC),
rawUserInput是如何定义的? (我们能看到 .h 吗?) -
是的,使用 ARC,你去:“@property (strong, nonatomic) NSString *rawUserInput;”
-
我还尝试使用“theVCMover.CleanerText.text = textView.text;”直接移动字符串但同样,什么都没有。这看起来很正常还是我的代码有问题?
标签: iphone objective-c viewcontroller viewdidload