如何访问另一个ViewController上的UITextView
我试图从anotherViewController的UITextView到MainViewController的string。 我的意思是用户在UITextView中从另一个控制器中写入东西,当触摸donebutton时,UITextView中的string应该等于MainViewController中的另一个string。 我怎样才能访问UITextView?
我做了这样的事情,但没有得到任何结果!
#import "AnotherViewController" @class AnotherViewController; @interface MainViewContoller : UIViewController { NSString *main_string; AnotherViewController *textEntered; } -(void)viewDidLoad { textEntered.TEXT = main_string }
*** TEXT是AnotherViewController上我的UITextView的名称。
编辑:
@interface AnotherViewController : UIViewController { UITextView *TEXT; id delegate; } @property (nonatomic, retain) IBOutlet UITextView *TEXT; @property (nonatomic ,retain) id delegate; @end @implementation AnotherViewController @synthesize TEXT ,delegate; - (IBAction)doneButton { //!!! problem with compare ! the compiler got me some warning [self dismissModalViewControllerAnimated:YES]; }
主视图控制器
#import "AnotherViewController.h" @class Another...; @interface MainViewControlelr : UIViewController { NSString *main_string; TextViewController *textEntered; } - (void)viewDidLoad { textEntered.delegate = self; } - (void) compareText { [textEntered.TEXT isEqual:main_string]; }
将mainViewController对象设置为anotherViewControllertypes的对象的委托。 并使用AnotherViewController的委托将消息传递给MainViewController。
在MainViewController里面,当你创build你的AnotherViewController对象的时候:
anotherVC.delegate = self;
而在AnotherViewController中,当你的button动作完成,你想传回文本时,说一个string存储。 然后将其传递给MainViewController:
[delegate compareText:textView.text];
compareText与NSString参数将是您的MainViewController类中的方法。
编辑:
在你的AnotherViewController.h接口中,声明一个ivar:
id delegate;
然后使用@property(in .h)和@synthesize(in .m)作为getter和setter。
然后做上面的。
我想你创build了AnotherViewController指针,但没有设置它。
在你访问textEntered.TEXT之前,你应该把你的TextEntered指针指向你的AnotherViewController对象。
这里是如何:
在iGhalamViewController类上,在接口声明中创build一个像这样设置textEntered对象的属性(可能在iGhalamViewController.h中)
@property (assign) AnotherViewController *textEntered;
所以,在创build这个viewcontroller对象的根源地方都是这样。 在创buildAnotherViewController和iGhalamViewController后,像这样设置你的textEntered指针。
AnotherViewController* a = [[AnotherViewController alloc] initWithBlah:blah]; iGhalamViewController* b = [[iGhalamViewController alloc] initWithBlah:blah]; b.textEntered = a;
那么它应该像你期望的那样工作。 但是在viewDidLoad之外的其他方法中使用它。 viewDidLoad probaby被调用,然后才能设置textEntered。 button按下事件的方法应该没问题。