【问题标题】:Passing UITextView values to ModalViewController from parent View Controller将 UITextView 值从父视图控制器传递给 ModalViewController
【发布时间】:2011-05-26 01:50:34
【问题描述】:

我一直在为 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,但我不确定我是否正确传递它。我的代码一开始有意义吗?

无论如何。请帮我完成这个项目。我真的必须在一周内完成它。再次感谢。

【问题讨论】:

    标签: objective-c xcode ipad modalviewcontroller


    【解决方案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];
    }
    

    【讨论】:

    • 嗨,理查德!感谢您提供您的代码。你真是太慷慨了。如果我无法修复我的代码,我会按照你的方式进行。
    【解决方案2】:

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

    【讨论】:

    • 嗨迪帕克!非常感谢您对我的问题的快速回复。我根据您的回答编辑了我的代码。它似乎有效,但并不完全正确。我尝试将子视图添加到我的控制器视图中,但我不确定在哪里添加它。所以,我只是将它添加到我的父视图控制器中。您能否在父视图控制器中查看我的代码并告诉我我做错了什么?提前致谢。
    • 当你以模态方式呈现它时,你不需要做[self.view addSubview:[storyView04 view]];
    • 你好。所以,我删除了 [self.view addSubview:[storyView04 view]];并编辑了 ArticleViewController.m 类的 viewDidLoad 部分。现在,在控制台上,我得到了字符串“这是一个测试!”这就是我想要的:) ModalView 也正常运行。现在,我需要传递已解析文章中的文本值,而不是固定字符串。有什么简单的方法可以做到这一点吗?谢谢。 PS:我还有一个关于我的项目的问题,我在这里发布:stackoverflow.com/questions/6164777/…
    • 简单的方法是什么?我假设父视图控制器将有权访问文章数组,因为它显示某种文章标识。如果您使用过按钮,则必须使用匹配的 id 标记它们。
    • 也就是说,您不需要为每个按钮使用不同的方法。将它们映射到我在编辑中显示的单个方法。
    【解决方案3】:

    您的属性 articleTextView 正在获取字符串“TESTING!TESTING!”。如果您在屏幕上的 UITextview 上看不到它,那是因为您将它连接到同名的实例变量 (articleTextView)。您可能希望从实例变量中删除 IBOutlet 并将其放在属性上。 @property(非原子,保留)IBOutlet UITextView *articleTextView;您可能不需要再次连接它。希望这能消除您在 ivars 和属性之间可能存在的误解。

    【讨论】:

      猜你喜欢
      • 2012-12-15
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      相关资源
      最近更新 更多