如何从View1更改View2的UITextField文本?

时间:2013-02-19 05:09:14

标签: iphone objective-c ios6

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

SVC.string = @"test";

问题在于:

  • messageBox.text不会更改
  • SVC.messageBox.text返回(null)

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";
}

如何克服这个?

5 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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

希望这会有所帮助......

答案 2 :(得分: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)

与上面提到的一样。

答案 3 :(得分:0)

只是属性 - 合成字符串andf只需将此字符串分配给yourTextView.text。例如见下文..

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

@implementation SecondViewController
@synthesize strMsgText;

之后当你出现SecondViewController时,将你的值分配给bellow ..

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


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

并在viewDidLoad:的{​​{1}}方法中将该字符串分配给SecondViewController,如下所示......

messageBox(TextView)

答案 4 :(得分:0)

您的代码存在的问题是您访问尚未在内存中创建的uicontrol。 你必须像这样改变你的陈述执行的顺序......

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

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


    SVC.messageBox.text = @"changed";
    NSLog(@"SVC: %@", SVC.messageBox.text);