0

ViewController の SecondViewController から UITextView.text を変更しようとしています。SecondViewController の NSString タイプであれば、次のようにして実行できます。

SVC.string = @"test";

問題はそれです:

  • messageBox.text は変更されません
  • SVC.messageBox.text が返されます (null)

ViewController.m のメソッドの 1 つ:

    SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
    SVC.messageBox.text = @"changed";
    NSLog(@"SVC: %@", SVC.messageBox.text); // result = (null)

    [self presentViewController:SVC animated:YES completion:NULL];

SecondViewController.h:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (nonatomic) IBOutlet UITextView *messageBox;


@end

SecondViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.messageBox.text = @"first";
}

これをどうやって乗り越えますか?

4

5 に答える 5

2

これを試してください、ViewController.m のメソッドの 1 つ:

SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
SVC.strMessage= @"changed";
[self presentViewController:SVC animated:YES completion:NULL];

SecondViewController.h:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
{
  IBOutlet UITextView *messageBox;
}
@property (nonatomic) NSString *strMessage;
@end

SecondViewController.m: @synthesize strMessage;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

self.messageBox.text = strMessage;
}
于 2013-02-19T05:34:22.240 に答える
1

デリゲートを使用してそれを行うこともできます。

例:

Viewcontroller2nd.h

@protocol SecondViewControllerDelegate <NSObject>
@optional
-(void) changeLabel:(NSString*)str;
@end

@interface ViewController2nd : UIViewController{

    IBOutlet UIButton *bttn;
    id <SecondViewControllerDelegate> delegate;

}

@property (retain) id delegate;

@end

Viewcontrollerone.h

@interface ViewController : UIViewController <SecondViewControllerDelegate>
{
    IBOutlet UILabel *lbl;

}
-(IBAction)passdata:(id)sender;

@end

Viewcontrollerone.m

-(void) changeLabel:(NSString*)str{
    lbl.text = str;
}

デリゲートの詳細: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

お役に立てれば...

于 2013-02-19T05:41:30.447 に答える
0

に設定@property (nonatomic) IBOutlet UITextView *messageBox;@property (nonatomic, strong) IBOutlet UITextView *messageBox;、に接続しているかどうかを確認しUItextViewます。そして、でリセットしないでviewDidLoadください。あなたが試すことができるもう一つのことは:

SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[self presentViewController:SVC animated:YES completion:NULL];
SVC.messageBox.text = @"changed";
NSLog(@"SVC: %@", SVC.messageBox.text); // result = (null)

上記のものと一緒に。

于 2013-02-19T05:16:01.810 に答える
0

文字列をプロパティ合成し、この文字列を に割り当てるだけyourTextView.textです。たとえば、以下を参照してください。

@interface SecondViewController : UIViewController{
       NSString *strMsgText;
}
@property(nonatomic, retain)    NSString *strMsgText;
@end

@implementation SecondViewController
@synthesize strMsgText;

その後、その時に SecondViewController を提示すると、次のように値を割り当てます。

    SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
    SVC.strMsgText = @"changed";
    [SVC.strMsgText retain];


    [self presentViewController:SVC animated:YES completion:NULL];

そして、次のように、その文字列をに割り当てるだけviewDidLoad:の方法で...SecondViewControllermessageBox(TextView)

- (void)viewDidLoad
{
     messageBox.text = strMsgText;
}
于 2013-02-19T05:44:42.570 に答える
0

コードの問題は、メモリ内にまだ作成されていない uicontrols にアクセスすることです。このようにステートメントの実行順序を変更する必要があります...

 SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

    [self presentViewController:SVC animated:YES completion:NULL];


    SVC.messageBox.text = @"changed";
    NSLog(@"SVC: %@", SVC.messageBox.text);
于 2013-02-19T05:44:50.293 に答える