2

2 つのビュー コントローラー間で 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);

}
4

2 に答える 2

1

あなたのコードはほとんど問題ありませんが、より複雑なビュー コントローラーがあると、すべてのプロパティ設定を行うためにカスタム初期化子を必ずしも記述したくないことがわかります。が nib からロードしている UI 要素である場合、init メソッドでCleanerText設定しても役に立たないことに注意してください。 が呼び出されるまでロードされません。CleanerText.text-viewDidLoad

rawUserInputただし、設定する変数やその他の変数のプロパティを宣言する場合は、init ですべてを行う必要はありません。その後、次のように移動できます。

SecondViewController *theVCMover = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
theVCMover.rawUserInput = textView.text;
theVCMover.otherProperty = otherValue;
....

そして、コードの残りの部分は同じように機能します。

于 2013-03-25T16:43:03.897 に答える
0

実行が完了するまでインスタンスのメソッドを (確実に) 呼び出すことはできないinitため、このパターンは「安全」であり、本来の動作方法です。

于 2013-03-25T16:36:02.387 に答える