将 UITextView 值从父视图控制器传递到 ModalViewController

发布于 2024-11-09 11:56:28 字数 3086 浏览 0 评论 0原文

我一直在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦魇绽荼蘼 2024-11-16 11:56:28

这是我要做的(取自我当前的项目):

#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];
}

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];
}
十级心震 2024-11-16 11:56:28

我猜您已经使用 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];
}

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];
}
凤舞天涯 2024-11-16 11:56:28

您的属性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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文