微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从父级View Controller传递UITextView值到ModalViewController

如何解决从父级View Controller传递UITextView值到ModalViewController

|| 我一直在为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,但是我不确定是否正确传递了它。一开始我的代码有意义吗? 无论如何。请帮我这个项目。我真的必须在一周内把它包装好。再次感谢。     

解决方法

这是我会做的(摘自当前项目):
#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];
}
    ,我猜您已经使用IB来创建视图。在这种情况下,“ 7”将不会为您加载该接口。您将必须使用
[[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];
}
    ,您的属性articleTextView正在获得字符串“ TESTING!TESTING!\”。如果您无法在屏幕上的UITextview上看到它,那是因为您将其连接到了具有相同名称的实例变量(articleTextView)。您可能要从实例变量中删除IBOutlet并将其放在属性上。 @属性(非原子性,保留)IBOutlet UITextView * articleTextView;您可能不需要再次将其挂接。希望这可以消除您可能在误码率和属性之间存在的误解。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。