从父View Controller将UITextView值传递给ModalViewController [英] Passing UITextView values to ModalViewController from parent View Controller

查看:105
本文介绍了从父View Controller将UITextView值传递给ModalViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为iPad的学校报纸应用程序工作。我终于能够解析我需要的文章了。我使用UITextView将每篇文章摘要放在父视图控制器上。现在我要做的是当按下UITextView顶部的自定义按钮时,用ModalViewController显示每篇文章。我在StackOverFlow中查看了几个QA,但无法使我的项目工作。我将把一些代码放在这里,一些日志放在控制台上。任何帮助将不胜感激。在此先感谢。

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.

在我的ArticleViewController.h中(与ModalViewController.h相同)

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

在我的ArticleViewController.m中(与ModalViewController相同。 m)

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);
}

在我的父视图控制器中

-(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!

我知道我必须将文本值从父视图传递给ModalViewController,但我不确定我是否我正确地传递了它。我的代码一开始是否有意义?

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.

推荐答案

我猜您已经使用IB来创建视图。在这种情况下 [[[ArticleViewController alloc] init] autorelease]; 将不会为您加载该接口。你将不得不使用 [[ArticleViewController alloc] initWithNibName:@ArticleViewControllerbundle:nil withText:@Test !!]; 来获得所需的结果。

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

首先,XIB将创建文本视图并链接到 articleTextView 财产。所以不需要再创建它。此外,此视图尚未作为子视图添加到控制器的视图属性中,因此它不会显示在屏幕上。 IB创建的文本视图将保留在屏幕上但没有引用。你应该删除第一行。其次,文本视图中的 text 属性是复制的属性。所以你的保留可以被认为是泄密。

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;

其他一切似乎没问题。

编辑

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

这篇关于从父View Controller将UITextView值传递给ModalViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆