我一直在为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,但我不确定我是否正确传递它。我的代码一开始是否有意义?
反正。请帮助我完成这个项目。我真的必须在一周内把它包起来。再次感谢。
答案 0 :(得分:1)
以下是我要做的事(取自我当前的项目):
#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];
}
答案 1 :(得分:1)
我猜你已经用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];
}
答案 2 :(得分:0)
你的属性articleTextView正在获取字符串“TESTING!TESTING!”。如果你在屏幕上的UITextview上看不到它,那是因为你把它连接到一个具有相同名称的实例变量(articleTextView)。您可能希望从实例变量中删除IBOutlet并将其放在属性上。 @property(非原子,保留)IBOutlet UITextView * articleTextView;你可能不需要再把它连接起来。希望这可以解决你在ivars和属性之间可能存在的误解。