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.
Here is what I would do (taken from my current project):
#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
Here is the implementation:
#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
How you would use this:
-(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];
}
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.
self.articleTextView = [[UITextView alloc] init];
self.articleTextView.text = [text retain];
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's view
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, the text
property in the text view is a copied
property. So your retain can be considered a leak.
self.articleTextView.text = text;
Everything else seems to be ok.
EDIT
-(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];
}
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.