开发者

Help using a view controllers methods before the view is loaded

I have a UIViewController called DebugViewController that contains a UITextView, and a public method called debugPrint which is used to write an NSString into the UITextView and display it.

Is it possible to write into the UITextView before I open the UIViewController, so that when I open it, the text previously written into it is displayed?

In my parent view controllers viewDidLoad method, I'm calling initWithNibName on the DebugViewController as follows

debugViewController = [[DebugViewController alloc] initWithNibName:@"DebugView" bundle:nil];

I then call debugPrint as follows

[debugViewController debugPrint:@"viewDidLoad"];

And some time later I call th开发者_如何学编程e following to open the debugViewController

debugViewController.delegate = self;
debugViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:debugViewController animated:YES];

However all the text previously written is missing.

Please let me know how I can use a view controllers methods before the view controller displayed to the user.

Thanks,

JustinP


What you are doing is a little non-standard. The danger with that as always is that if you don't really have an expert grasp on what you're doing, you can quickly find yourself in difficulty.

If you want something set before the view is displayed to the user, then the best way to do that is to do it in the viewWillAppear method. Put it there rather than in viewDidLoad because a view might loaded once but appear many times. Where you place it depends on whether the data changes from appearance to appearance.

So, if your data is pretty static and won't change, use the viewDidLoad method.

Assuming that you'll go for the viewWillAppear option, let's do the first step by having an ivar in the view controller:

NSString *myText;

set that after init:

debugViewController = [[DebugViewController alloc] initWithNibName:@"DebugView" bundle:nil];
debugViewController.myText = @"My text here";

then, in debugViewController's viewWillAppear method:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    myTextView.text = myText;   
}

The view controller life cycle is complex as you can see from the View Controller Programming Guide for iOS. So I'd say best not stray from the path of least resistance unless you have good reason. That said sometimes the best way to learn is by experimentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜