I am trying to create UIViewController programmatically for ModalView presentation on the main window of the application and in this UIViewController on top i have is Navigation bar with done button to dismiss the view and below the navigation bar it has UITextView.
So my question is that the UITextview which i want to create below the navigation bar will come in the ViewdidLoad Method or i m doing right to show separate medthod to setup textview .
In the Infoviewcontroller.h file have following code:
#import <UIKit/UIKit.h>
@interface Infoviewcontroller : UIViewController <UITextViewDelegate>{
UITextView *textView;
}
@property (nonatomic, retain) UITextView *textView;
@property (nonatomic, assign) UINavigationBar *navBar;
@end
Then in the infoviewcontroller.m file have the following code:
#import "Infoviewcontroller.h"
@implementation Infoviewcontroller
@synthesize textView;
@synthesize navBar;
-(void)dealloc{
[textView release];
[navBar release];
[super dealloc];
}
-(void)setupTextView {
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor redColor];
self.textView.font = [UIFont fontWithName:@"System Bold" size:13];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = @"This is UITextView\nThis is UITextView\nThis is UITextView\nThis is UITextView";
[self.view addSubview: self.textView];
}
- (void)viewDidLoad {
[super viewDidLoad];
navBar = [[UINavigationBar alloc] init];
UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle:@"ModalViewControllerTest"] autorelease];
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView:)] autorelease];
navItem.rightBarButtonItem = done;
navBar.items = [NSArray arrayWithObject:navItem];
[self.view addSubview:navBar];
}
Regarding creation of UINavigationBar programmatically, I found following blog post helpful http://cps.liridesce.com/?p=100. Example overrides loadView rather than viewDidLoad method, but I think in your case using viewDidLoad should have the same effect.
I would also add viewDidUnload where I would set to nil navBar and textView properties.
I’m not quite sure why you need separate setupTextView method, unless you are calling it from different places of your UIViewController. I would just keep everything together in viewDidUnload.