自古以來,不管是在那個作業系統平台,應用軟體如果要讓畫面與眾不同,那就別指望光靠標準開發工具提供的元件就達到目的,幾乎都是要由開發人員使用較原始的程式庫來『客製』,我以這個方向來作第一個練習的目標。
環境:Xcode 4.2 + iOS SDK 5
目標:不使用 Interface Builder 建立 ViewController,並放進 一個 Label
- 『New Group』rename 成為『Classes』
- 將 AppDelegate.h AppDelegate.m 兩個檔案移到 Classes 這個資料夾裏
接下來,建立 ViewController:
- 在 Classes(右鍵) -> New File, ios Cocoa Touch, UIViewController subclass
- Class: HomeViewController, 確認 『With XIB for user interface』沒有被選取
- 選擇存放程式專案的資料夾(跟前面『建立專案』同一個資料夾)->(creat)
以下是我的 code
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "HomeViewController.h"
@implementation AppDelegate
@synthesize window = _window;
HomeViewController *homeViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//=========================================================================
[self.window addSubview:homeViewController.view];
//=========================================================================
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
@end
HomeViewController.h
#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController{
}
@property (nonatomic, strong) UILabel * label;
@end
HomeViewController.m
#import "HomeViewController.h"
@implementation HomeViewController
@synthesize label ;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
- (void)viewDidLoad
{
//=========================================================================
//加一個 Label View
CGRect frame = CGRectMake(20, 10, 280, 50);
label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont fontWithName:@"Verdana" size:20];
label.text = @"Hello World !!!!!!!!";
[self.view addSubview:label];
//=========================================================================
[super viewDidLoad];
}
- (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);
}
沒有留言:
張貼留言