从父视图控制器传递UITextView值到ModalViewController
我一直在为iPad的校报应用程序工作。我终于能够解析我需要的文章。我用UITextView将每篇文章摘要放在父视图控制器上。现在我想要做的是当按下UITextView顶部的自定义按钮时,用ModalViewController显示每篇文章。我通过StackOverFlow中的几个QA来查看,但无法让我的项目工作。我将在这里放置一些代码,并在控制台上添加一些日志。任何帮助将不胜感激。提前致谢。从父视图控制器传递UITextView值到ModalViewController
以我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传递文本值,但我不知道如果我正确地传递。我的代码起初是否有意义?
反正。请帮助我完成这个项目。我真的必须在一周内完成。再次感谢。
这里是我会做什么(从我目前的项目采取):
#import <UIKit/UIKit.h>
@interface DictionaryViewController : UIViewController {
NSString *word;
NSString *definition;
IBOutlet UIWebView *webView;
IBOutlet UINavigationItem *navItem;
}
@property(nonatomic, copy) NSString *word;
@property(nonatomic, copy) NSString *definition;
@property(nonatomic, retain) IBOutlet UIWebView *webView;
@property(nonatomic, retain) IBOutlet UINavigationItem *navItem;
-(IBAction) done;
-(IBAction) pronounce;
@end
下面是执行:
#import "DictionaryViewController.h"
#import "TBXML.h"
#import "Lexicontext.h"
#import "Lexicontext+Pronounce.h"
@implementation DictionaryViewController
@synthesize word, definition, webView, navItem;
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"word"])
{
definition = [[Lexicontext sharedDictionary] definitionAsHTMLFor:word];
[[self webView] loadHTMLString:definition baseURL:nil];
navItem.title = word;
}
}
-(void) done
{
[self dismissModalViewControllerAnimated:YES];
}
-(void) pronounce
{
[[Lexicontext sharedDictionary] pronounce:word];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self addObserver:self forKeyPath:@"word" options:0 context:nil];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[self webView] loadHTMLString:definition baseURL:nil];
navItem.title = word;
UIScrollView *scrollview = [[webView subviews] objectAtIndex:0];
scrollview.bounces = NO;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
你会如何使用:
-(void) somethingHappened:(id) sender
{
DictionaryViewController *dvc = [[DictionaryViewController alloc] initWithNibName:@"DictionaryViewController" bundle:[NSBundle mainBundle]];
dvc.word = @"Some word to look up";
[self presentModalViewController:dvc];
[dvc release];
}
我猜你已经用IB来创建视图。在这种情况下,[[[ArticleViewController alloc] init] autorelease];
将不会为您加载该接口。您将不得不使用[[ArticleViewController alloc] initWithNibName:@"ArticleViewController" bundle:nil withText:@"Test!!"];
来获得所需的结果。
但是,有一些关于你实现初始化器的方式是不正确的。
self.articleTextView = [[UITextView alloc] init];
self.articleTextView.text = [text retain];
首先,XIB将创建文本视图并链接到articleTextView
属性。所以不需要再创建它。此外,这个视图还没有作为子视图添加到你的控制器的view
属性中,所以它不会出现在屏幕上。 IB创建的文本视图将保留在屏幕上,但没有参考。你应该删除第一行。其次,文本视图中的text
属性是copied
属性。所以你的保留可以被认为是泄漏。
self.articleTextView.text = text;
其他一切似乎都没问题。
编辑
-(IBAction)showArticleView:(UIButton *)sender{
storyView = [[ArticleViewController alloc] initWithNibName:@"ArticleViewController" bundle:[NSBundle mainBundle]];
storyView.articleTextView.text = [articles objectAtIndex:[sender tag]];
[storyView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
storyView.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:storyView animated:YES];
[storyView release];
}
嗨迪帕克!非常感谢soooo对我的问题作出快速回应。我根据你的回答编辑我的代码。它似乎工作,但不完全。我试图添加子视图到我的控制器的视图,但我不知道在哪里添加它。所以,我只是将它添加到我的父视图控制器。你可以看看我的代码在父视图控制器,并告诉我我在哪里做错了?提前致谢。 – SerPiero 2011-05-28 05:35:05
你不需要做'[self.view addSubview:[storyView04 view]];'当你以模态方式呈现它时。 – 2011-05-28 06:28:21
再次嗨。所以,我删除了[self.view addSubview:[storyView04 view]];并编辑了ArticleViewController.m类的viewDidLoad部分。现在,在控制台上,我得到字符串“This is a test!”这是我想要的:) ModalView也正常工作。现在,我需要传递解析文章中的文本值,而不是固定字符串。有没有简单的方法来做到这一点?谢谢。 PS:我也有关于我的项目的另一个问题,我在这里发布:http://stackoverflow.com/questions/6164777/how-should-i-parse-html-iteratively-from-stored-link-using-hpple-解析器 – SerPiero 2011-05-28 22:52:17
你的财产articleTextView越来越字符串 “测试!测试!”。如果你在屏幕上的UITextview上看不到它,那是因为你将它挂接到一个具有相同名称的实例变量(articleTextView)。您可能想从实例变量中删除IBOutlet并将其放置在属性中。 @属性(非原子,保留)IBOutlet UITextView * articleTextView;你可能不需要再把它连接起来。希望这清除了你可能在ivars和属性之间产生的误解。
嗨理查!感谢您提供您的代码。这是非常慷慨的你。如果我无法修复我的代码,我会按照自己的方式行事。 – SerPiero 2011-05-28 05:37:48