PDA

View Full Version : How to do if using nib file?




mikezang
Jul 10, 2010, 05:31 AM
I followed a book to make a application based app with code as below, then I tried use textView in NIB file, but I could"t show text, what can I do?

// SourceReaderAppDelegate.h
#import <UIKit/UIKit.h>
@class SourceReaderViewController;
@interface SourceReaderAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
SourceReaderViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet SourceReaderViewController *viewController;
@end


// SourceReaderAppDelegate.m
#import "SourceReaderAppDelegate.h"
#import "SourceReaderViewController.h"
@implementation SourceReaderAppDelegate
@synthesize window;
@synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end

// SourceReaderViewController.h
#import <UIKit/UIKit.h>
@interface SourceReaderViewController : UIViewController {
UITextView *textView;
}
@end


// SourceReaderViewController.m
#import "SourceReaderViewController.h"
@implementation SourceReaderViewController
@synthesize textView;
using a nib.
- (void)loadView {
CGRect bounds = [[UIScreen mainScreen] applicationFrame];
[super loadView];
textView = [[UITextView alloc] initWithFrame:bounds];
UIColor *myBlue = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
textView.textColor = myBlue;
UIFont *myFixed = [UIFont fontWithName:@"Courier New" size:24.0];
textView.font = myFixed;
textView.editable = NO;
NSURL *url = [NSURL URLWithString:@"http://yahoo.co.jp"];
NSString *pageData = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:nil];
textView.text = pageData;
self.view = textView;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[textView dealloc];
[super dealloc];
}
@end



forum user
Jul 10, 2010, 07:10 AM
I think your
// SourceReaderViewController.m
self.view = textView;
should be a
[self addSubview:textView];

mikezang
Jul 10, 2010, 07:43 AM
I did it as below

- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://yahoo.co.jp"];
NSString *pageData = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
textView.text = pageData;
}