将 UITextView 值从父视图控制器传递到 ModalViewController
我一直在为 iPad 开发校报应用程序。我终于能够解析我需要的文章了。我使用 UITextView 将每篇文章的摘要放在父视图控制器上。现在我想做的是当按下 UITextView 顶部的自定义按钮时使用 ModalViewController 显示每篇文章。我在 StackOverFlow 中查看了几个 QA,但无法使我的项目正常运行。我将把一些代码放在这里,并将一些日志放在控制台上。任何帮助将不胜感激。提前致谢。
在我的 ArticleViewController.h (与 ModalViewController.h 相同)
@interface ArticleViewController : UIViewController {
IBOutlet UIButton *doneReadingButton;
IBOutlet UITextView *articleTextView;
}
@property (nonatomic, retain) UIButton *doneReadingButton;
@property (nonatomic, retain) UITextView *articleTextView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withText:(NSString *)text;
-(IBAction)doneReadingArticle:(id)sender;
//-(IBAction)displayArticleView:(id)sender;
@end
在我的 ArticleViewController.m (与 ModalViewController.m 相同)
#import "ArticleViewController.h"
@implementation ArticleViewController
@synthesize doneReadingButton;
@synthesize articleTextView;
- (IBAction)doneReadingArticle:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
//This is where I need to display main article in modal view
/*
- (IBAction)displayArticleView:(id)sender {
[self presentModalViewController:articleTextView animated:YES];
}
*/
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withText:(NSString *)text {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
//self.articleTextView = [[UITextView alloc] init];
//self.articleTextView.text = [text retain];
self.articleTextView.text = text;
}
NSLog(@"********text in modal init******** >> %@",articleTextView.text);
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//I guess, this is where I have to load main article
self.articleTextView.text = @"This is a test!";
NSLog(@"********text in modal******** >> %@",articleTextView.text);
}
在我的父视图控制器
-(IBAction)articleView04:(id)sender{
storyView04 = [[ArticleViewController alloc] initWithNibName:@"ArticleViewController" bundle:[NSBundle mainBundle]];
storyView04.articleTextView.text =[NSString stringWithString:@"TESTING! TESTING!"];
[storyView04 setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
storyView04.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:storyView04 animated:YES];
[storyView04 release];
}
中所以,在控制台中,我只是得到这个,这只是一个占位符文本。
2011-05-28 15:44:00.071 TheCalAggie[4179:207] ********text in modal******** >> This is a test!
我知道我必须将文本值从父视图传递到 ModalViewController,但我不确定是否正确传递它。我的代码一开始有意义吗?
无论如何。请帮助我完成这个项目。我真的必须在一周内完成它。再次感谢。
I have been working on school newspaper app for iPad. I was finally able to parse the articles I need. I put each article summary on parent view controller with UITextView. Now what I am trying to do is to display each article with ModalViewController when custom button on top of UITextView is pressed. I looked through couple QA's here in StackOverFlow, but couldn't make my project work. I am going to put some of my codes here and some logs on console. Any help will be greatly appreciated. Thanks in advance.
In my ArticleViewController.h (which is the same as ModalViewController.h)
@interface ArticleViewController : UIViewController {
IBOutlet UIButton *doneReadingButton;
IBOutlet UITextView *articleTextView;
}
@property (nonatomic, retain) UIButton *doneReadingButton;
@property (nonatomic, retain) UITextView *articleTextView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withText:(NSString *)text;
-(IBAction)doneReadingArticle:(id)sender;
//-(IBAction)displayArticleView:(id)sender;
@end
In my ArticleViewController.m (the same as ModalViewController.m)
#import "ArticleViewController.h"
@implementation ArticleViewController
@synthesize doneReadingButton;
@synthesize articleTextView;
- (IBAction)doneReadingArticle:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
//This is where I need to display main article in modal view
/*
- (IBAction)displayArticleView:(id)sender {
[self presentModalViewController:articleTextView animated:YES];
}
*/
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withText:(NSString *)text {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
//self.articleTextView = [[UITextView alloc] init];
//self.articleTextView.text = [text retain];
self.articleTextView.text = text;
}
NSLog(@"********text in modal init******** >> %@",articleTextView.text);
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//I guess, this is where I have to load main article
self.articleTextView.text = @"This is a test!";
NSLog(@"********text in modal******** >> %@",articleTextView.text);
}
In my parent view controller
-(IBAction)articleView04:(id)sender{
storyView04 = [[ArticleViewController alloc] initWithNibName:@"ArticleViewController" bundle:[NSBundle mainBundle]];
storyView04.articleTextView.text =[NSString stringWithString:@"TESTING! TESTING!"];
[storyView04 setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
storyView04.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:storyView04 animated:YES];
[storyView04 release];
}
So, in the console, I just get this, which is just a placeholder text.
2011-05-28 15:44:00.071 TheCalAggie[4179:207] ********text in modal******** >> This is a test!
I know I have to pass text value from parent view to ModalViewController, but I am not sure if I am passing it correctly. Does my code make sense at first?
Anyways. Please, help me with this project. I really have to wrap it up within one week. Thanks again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我要做的(取自我当前的项目):
这是实现:
您将如何使用它:
Here is what I would do (taken from my current project):
Here is the implementation:
How you would use this:
我猜您已经使用 IB 来创建视图。在这种情况下
[[[ArticleViewController alloc] init] autorelease];
不会为您加载该界面。您必须使用[[ArticleViewController alloc] initWithNibName:@"ArticleViewController" bundle:nil withText:@"Test!!"];
才能获得所需的结果。但是,您实现初始化程序的方式有些不正确。
首先,XIB 将创建文本视图并链接到
articleTextView
属性。所以不需要再次创建它。此外,该视图尚未作为子视图添加到控制器的view
属性中,因此它不会出现在屏幕上。 IB 创建的文本视图将保留在屏幕上,但没有参考。您应该删除第一行。其次,文本视图中的text
属性是一个copied
属性。所以你的保留可以被认为是泄漏。其他一切似乎都还好。
编辑
I am guessing you've used IB to create the views. In which case
[[[ArticleViewController alloc] init] autorelease];
won't load that interface for you. You will have to use[[ArticleViewController alloc] initWithNibName:@"ArticleViewController" bundle:nil withText:@"Test!!"];
to get the desired result.But then there is something that isn't right about the way you've implemented your initializer.
Firstly XIB will have create the text view and linked to the
articleTextView
property. So no need to create it again. Moreover, this view hasn't been added as a subview to your controller'sview
property so it won't appear on screen. The IB created text view will remain on screen but without a reference. You should remove the first line. Secondly, thetext
property in the text view is acopied
property. So your retain can be considered a leak.Everything else seems to be ok.
EDIT
您的属性articleTextView正在获取字符串“TESTING!TESTING!”。如果您在屏幕上的 UITextview 上看不到它,那是因为您将其连接到具有相同名称的实例变量 (articleTextView)。您可能想要从实例变量中删除 IBOutlet 并将其放在属性上。 @property(非原子,保留)IBOutlet UITextView *articleTextView;您可能不需要再次连接它。希望这能消除您对 ivars 和属性之间可能存在的误解。
your property articleTextView is getting the string "TESTING! TESTING!". If you can't see it on the UITextview you have on screen, it is because you hooked it up to an instance variable with the same name (articleTextView). You might want to remove the IBOutlet from the instance variable and put it on the property. @property (nonatomic, retain) IBOutlet UITextView *articleTextView; you might not need to hook it up again. Hope this clears up a misunderstanding you probably have between ivars and properties.