如何访问另一个 ViewController 上的 UITextView
我试图将 UITextView 的字符串从 anotherViewController 等于 MainViewController 。我的意思是用户从 anotherViewController 在 UITextView 中写入一些内容,当触摸完成按钮时, UITextView 中的字符串应该等于 MainViewController 中的另一个字符串。我如何访问 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];
}
I am trying to equal the string of an UITextView from anotherViewController to the MainViewController . I mean users write something in UITextView from anotherViewController and when touch the done button the string in UITextView should equal to another string in MainViewController . How can I access UITextView ???
I did something like this but did not get any result !
#import "AnotherViewController"
@class AnotherViewController;
@interface MainViewContoller : UIViewController {
NSString *main_string;
AnotherViewController *textEntered;
}
-(void)viewDidLoad {
textEntered.TEXT = main_string
}
***TEXT is the name my UITextView on AnotherViewController .
EDITED :
@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];
}
MAIN VIEW CONTROLLER
#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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 mainViewController 对象设置为 anotherViewController 类型的对象的委托。并使用 AnotherViewController 中的委托将消息传递给 MainViewController。
在 MainViewController 中,当您创建 AnotherViewController 对象时,请执行以下操作:
在 AnotherViewController 中,当按钮操作完成并且您想要将文本传递回来时,请存储在字符串中。然后将其传递给 MainViewController,如下所示:
带有 NSString 参数的compareText 将是 MainViewController 类中的一个方法。
编辑:
在 AnotherViewController.h 接口中,声明一个 ivar:
然后使用 @property (在 .h 中)和 @synthesize (在 .m 中)作为 getter 和 setter。
然后执行上述操作。
Set mainViewController object as the delegate of an object of anotherViewController type. And use that delegate from AnotherViewController to pass messages to MainViewController.
Inside MainViewController, when you create your AnotherViewController object do:
And in AnotherViewController, when your button action is done and you want to pass the text back, say store in a string. Then pass it to MainViewController as:
compareText with a NSString argument would be a method in your MainViewController class.
EDIT:
In your AnotherViewController.h interface, declare an ivar:
Then use @property (in .h) and @synthesize (in .m) for getter and setter.
Then do the above.
我认为您创建了 AnotherViewController 指针但没有设置它。
在访问 textEntered.TEXT 之前,您应该将 textEntered 指针设置为 AnotherViewController 对象。
方法如下:
在 iGhalamViewController 类上,在接口声明中创建一个用于设置 textEntered 对象的属性(可能在 iGhalamViewController.h 中)。
因此,在根位置创建此视图控制器对象。创建 AnotherViewController 和 iGhalamViewController 后,像这样设置你的 textEntered 指针。
然后它应该像你期望的那样工作。但在 viewDidLoad 之外的其他方法中使用它。在设置 textEntered 之前,viewDidLoad 可能会被调用。按钮按下事件方法应该没问题。
I think you created an AnotherViewController pointer but did not set it.
before you can access textEntered.TEXT, you should set you textEntered pointer to your AnotherViewController object.
Here is how:
on iGhalamViewController class, create a property for setting textEntered object like this in your interface declaration (probably in iGhalamViewController.h)
So, at the root place where do you create this viewcontroller objects both. After you create AnotherViewController and iGhalamViewController, set your textEntered pointer like this.
Then it should work like you expected. But use it in other method than viewDidLoad. viewDidLoad probaby gets called before you can set textEntered. A button press event method should be fine.