0

我正在尝试从 ViewController 中的 SecondViewController 更改 UITextView.text。如果它是 SecondViewController 中的 NSString 类型,我可以这样做:

SVC.string = @"test";

问题是:

  • messageBox.text 没有改变
  • SVC.messageBox.text 返回(空)

ViewController.m 中的方法之一:

    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 中的方法之一:

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

视图控制器.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

just property-synthesize string andf 只是将此字符串分配给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 回答