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):
Here is the implementation:
How you would use this:
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.
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